Good Code Practice

A lot of errors can be avoided through good code practice. Primarily, this means organizing your code to minimize the chance of errors. In the process, you will find it helps in debugging, too.

Code organization is intensely personal. What works for you might not work for me, etc. All you can do is develop your own style and stick to it. Here are some suggestions that you can take with as many grains of salt as you like while developing your own style.

CONSIDER YOUR SUCCESSORS

Anything that you develop will not be used by you alone. Unless your project will die when you retire, someone will come in after you and work on it. They will undoubtedly have their own style which will conflict with yours.

Bearing this in mind, try to organize your code so as to be easily understood. You are not doing yourself or your employer any favors if you are the only person on the planet who understands how to work your code. Your successor will be forced to trash your work and start over again.

COMMENT YOUR CODE

This goes along with "Consider Your Successors" above, but it is so important, I gave it separate attention.

You will be shocked how little you remember of your own code when you write several programs a year and you revisit one of the earlier ones. You will scratch your head wondering what the heck you were trying to do.

Installing frequent, informational comments makes life good again. Pretend someone else is writing your code, and you are reading it for the first time. What would make it easier for you to understand? Is there a clear flow of logic? Can you tell exactly where you are in the process?

If you run a loop, identify what it is doing. If you define a variable, tell what it is. If you test a conditional, state why. If you define your own function, state how to use it, what the different arguments are, what format the arguments must be in, what values are returned, etc.

I can not emphasize this enough. The extra effort up front to comment your code will pay big.

CHOOSE HELPFUL VARIABLE NAMES

Today, you may know exactly what $a is, but tomorrow your successor won't. You might not even remember it tomorrow yourself. $lastName is much more descriptive and the chances of inadvertently reusing it later in your program is minimized.

COMPLETE THE SYNTAX FIRST

You know you want your next line of code to be an echo. You know the echo statement will use open and closed quotes and a semi-colon. Type all of this first, then fill in whatever you want between the quotes. This assures you the syntax is right from the start. Otherwise you may forget to include the closing quote or semi-colon. Similarly, you know your if statement requires the keyword if followed by a pair of parentheses followed by a pair of curly braces. Type all of this first, then type the condition between the parentheses and the subsequent code between the curly braces. You will prevent numerable syntax errors.

INDENT YOUR CODE

Which of the two snippets below is easier to follow?

<?php if ($num%2!=0) { echo $num." is not divisible by 2 <br />"; if ($num%3!=0) { echo $num." is not divisible by 3 <br />"; if ($num%5!=0) { echo $num." is not divisible by 5 <br />"; } else { echo "but, ".$num." is divisible by 5.<br />"; } } else { echo "but, ".$num." is divisible by 3.<br />"; } } else { echo $num." is divisible by 2.<br />"; } ?>
<?php if ($num%2!=0) { echo $num." is not divisible by 2 <br />"; if ($num%3!=0) { echo $num." is not divisible by 3 <br />"; if ($num%5!=0) { echo $num." is not divisible by 5 <br />"; } else { echo "but, ".$num." is divisible by 5.<br />"; } } else { echo "but, ".$num." is divisible by 3.<br />"; } } else { echo $num." is divisible by 2.<br />"; } ?>

The computer, of course, could care less. Since white space is ignored, all the computer sees is one long line of code. But it sure makes it easier for the programmer to follow.

USE FUNCTIONS AND INCLUDE FILES

If you are going to use the same code more than once somewhere in your project, make it a function, then call it as needed. Now, any modifications to the code only has to be done in one place. It also makes your code cleaner and easier to read.

Include files will be explained in a subsequent lesson, but a brief introduction here is helpful. You can store functions, variables or anything else in an include file, then call the file in each of your pages. That way you don't have to write that code on every page. Again, you write it once, call the file, then everything in that file is available on this page.

TEST YOUR CODE

Besides the obvious test of running the code to see if it works as expected, you should also try to break the code. If your program takes viewer input, then make yourself a mentally-challenged viewer and input all sorts of wierd stuff to see what it does. Whatever your code uses for input, try forcing different values to see what happens.

Reference PHP Coding Standard.


Functions   < <  PREVIOUS   Table of Contents NEXT  > >   Error Handling

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 3,482 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHP08.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 2:27 pm, UTC
This file took 0.03606 seconds to process.