More Information: Programming is Easy

Programming is pretty easy. You find the function that does what you want, and you stick it in your web page; or you do a web search on “PHP” and whatever it is you want to do, and you find a snippet of code to copy and paste into your web page. Programming is simple; programming well is an art. It’s an art with only a few rules, simple to learn and difficult to master.

The cardinal rule is this: keep your code easy to read. Indent well. Correct indentation will solve bugs before they become bugs. Both your PHP include files and your HTML template files should be well indented, similar to the examples in this tutorial.

Name your functions, methods, variables, classes, and properties well. Their names should be short but should describe what that piece of code does. Do not use abbreviations unless those abbreviations have already become a word. “SQL” is a good abbreviation; “db” is not.

Use comments liberally. Every method and function more complex than two or three lines should have a comment describing what it expects and what it does. Every section of code inside your methods and functions should be commented. And even your properties and variables often need comments to remind you of what they’re meant for later. You can always remove comments if they’re too obvious later; it’s difficult to put them in once you’ve forgotten what the code means.

Keep your methods short. Once a method takes up more than a screen, it’s time to break it into parts, each part performing a discrete and useful function.

Use standard naming schemes. Capitalize your class names. If you use camelCase for methods, properties, variables, and functions, use them consistently. If you use under_scores instead of camelCase, use it consistently.

When you paste code snippets from the web into your own include files and web pages, understand what every part of the code means. If you don’t, your code will build up cruft that probably isn’t necessary but that you’re afraid to remove.

Don’t repeat yourself! Two pieces of code doing the same thing should be combined into a single method or function. Two lines of code not doing the same thing should not be combined into a single method or function.

Lost?

There is no reason in Eugenics, but there is plenty of motive. Its supporters are highly vague about its theory, but they will be painfully practical about its practice. — G. K. Chesterton (Eugenics and Other Evils)