Coding Friendlier
Programmers are a cryptic bunch, at times looking to solve a major problem as quickly as possible, and other times striving to create an elegant solution to the current project’s needs. One common element that should always prevail with programming in a team environment is the understanding that someone else will eventually work on your source code.
Keep it Neat
Just like your cubicle or desk area being neat encourages you to be organized psychologically, your source code should be equally organized and neat to make reading and understanding it clear and quick.
Commenting the source code, like labeling a folder for a project in your desk drawer, provides tips as to what is happening inside and provides invaluable insight into the thinking of the programmer at the time.
Indentations and spacing are also very important to remember when programming even small projects – it makes reading the code and finding bugs a much more enjoyable task (even though bug hunting is never “fun”).
Take the following snippets of the same PHP programming code:
Example 1:
$a = file('albumlist.txt');
$i=0;
$done=false;
while ($done == false) {
echo($a[$i]);
if ($i>=count($a)) $done = true;
else $i++;
}
Example 2:
// read the contents of the album list into an array to use later
$a = file('albumlist.txt');
// set loop defaults
$i = 0;
$done = false;
// start looping through album titles from the file
while ( $done == false ) {
// display the title to the browser
echo( $a[$i] );
// test if all elements have been processed
// if so, end the loop
// if not, increment the counter
if ( $i >= count($a) ) $done = true;
else $i++;
}
Notice how in the first example it is condensed and hard to read unless you are familiar with PHP programming and think/dream/eat source code. The second example illustrates how commenting the code (the lines starting with // are comments) and spacing things out makes understanding it a breeze, even for non-programmers.
Aren’t there performance issues?
You may be tempted to eliminate these basic tools from a particular program because it will work faster without the extra “fluff” of a comment or without having the interpreter having to parse extra spacing, but it has been tested that these items have minimal effect on processing speed of source code but can save many real hours of time when troubleshooting.
Remember that while you may completely understand the code you create, others don’t have direct access to your brain and often times will interpret your logic differently. Plus, if you come back to a project you haven’t worked on in several months, you’ll be able to quickly remember your logic and thought process at the time. This will save you real time later, which actually increases your overall performance and productivity.
Just Do It
There is no better time than right now to add these practices to your programming toolbox. Every programming language commonly in use today has a way to add commenting within the code – use it today and make it second nature. Usually you don’t need to comment every single line of code – hopefully your replacement or team members will understand the essential gist of your code – but do try to add comments to explain complicated code or extended logic breaks.
Also, many programming languages in use today are also tolerant of spacing and give you relative freedom to make it look however you’d like. Take the extra moment to add the tab and space keys to your repertoire and make your code pretty.
So start today – right now – and you’ll join the ranks of programming leaders inside of 10 seconds flat.
This entry was posted on Monday, June 21st, 2010 and is filed under Paul Hirsch: Development.
Both comments and pings are currently closed.

