13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Writing Maintainable Code<br />

539<br />

Variable names should describe the data they contain. If you are storing somebody’s surname,<br />

call it $surname.You need to find a balance between length <strong>and</strong> readability. For<br />

example, storing the name in $n makes it easy to type, but the code is difficult to underst<strong>and</strong>.<br />

Storing the name in $surname_of_the_current_user is more informative, but it’s<br />

a lot to type (<strong>and</strong> therefore easier to make a typing error) <strong>and</strong> doesn’t really add that<br />

much value.<br />

You need to make a decision on capitalization.Variable names are case sensitive in<br />

<strong>PHP</strong>, as we’ve mentioned previously.You need to decide whether your variable names will<br />

be all lowercase, all uppercase, or a mix—for example, capitalizing the first letters of<br />

words.We tend to use all lowercase because this scheme is the easiest to remember for us.<br />

Distinguishing between variables <strong>and</strong> constants with case is also a good idea. A common<br />

scheme is to use all lowercase for variables (for example, $result) <strong>and</strong> all uppercase<br />

for constants (for example, PI).<br />

One bad practice some programmers use is to have two variables with the same name<br />

but different capitalization just because they can, such as $name <strong>and</strong> $Name.We hope it is<br />

obvious why this practice is a terrible idea.<br />

It is also best to avoid amusing capitalization schemes such as $WaReZ because no one<br />

will be able to remember how it works.<br />

You should also think about what scheme to use for multiword variable names. For<br />

example, we’ve seen all the following schemes:<br />

$username<br />

$user_name<br />

$userName<br />

It doesn’t matter which you opt for, but you should try to be consistent about usage.You<br />

might also want to set a sensible maximum limit of two to three words in a variable<br />

name.<br />

Function names have many of the same considerations, with a couple of extras.<br />

Function names should generally be verb oriented. Consider built-in <strong>PHP</strong> functions<br />

such as addslashes() or mysqli_connect(), which describe what they are going to do<br />

to or with the parameters they are passed.This naming scheme greatly enhances code<br />

readability. Notice that these two functions have a different naming scheme for dealing<br />

with multiword function names. <strong>PHP</strong>’s functions are inconsistent in this regard, partly as<br />

a result of having been written by a large group of people, but mostly because many<br />

function names have been adopted unchanged from various different languages <strong>and</strong><br />

APIs.<br />

Also remember that function names are not case sensitive in <strong>PHP</strong>.You should probably<br />

stick to a particular format anyway, just to avoid confusion.<br />

You might want to consider using the module-naming scheme used in many <strong>PHP</strong><br />

modules—that is, prefixing the name of functions with the module name. For example,<br />

all the improved <strong>MySQL</strong> functions begin with mysqli_, <strong>and</strong> all the IMAP functions<br />

begin with imap_. If, for example, you have a shopping cart module in your code, you<br />

could prefix the function in that module with cart_.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!