In the first part of this 2-part mini-series I explained why it is important to make sure you take the time to make sure your code is easy for others to pick up and understand. But how do you best achieve this lofty aim? Below are some general principles that are straightforward to implement and will go a long way to making your code clear, legible and as easy to understand as possible.
Whitespace, whitespace, whitespace
Probably the single most important thing you can do to increase the legibility of your code is to inject a good dose of whitespace into it. Dense, cramped up code is hard to scan and hard to extract information from quickly. Correctly indenting your code, together with liberal use of spaces and new lines results in a much clearer easier to scan and understand script. If you are writing JavaScript or CSS, don’t cut back on whitespace to save you a Kb or two in filesize – instead space everything out nicely and then run it through a compressor when you are ready to go live with it all, keeping the uncompressed version for later editing. Have a peek at the CodeIgniter framework source code for a great example of well formatted code with plenty of whitespace.
Make it self-documenting
Good code doesn’t need pages of comments to tell you what it is doing. Careful choice of variable and function names can go a very long way to making your code speak for itself. Stay away from non-standard abbreviations and generic names (like ‘value’ of ’set_value’) where possible, and try to make sure all of your function names are really representative of what the function actually does, and variable names describe accurately their contents. The CodeBetter site has a good post on self documenting code = check it out for loads of good tips.
Create a set of conventions, and stick to them
Nothing is harder to get your head round than code that doesn’t follow a cohesive set of conventions. It is really, really important that you develop a set of conventions (or adopt ones from somewhere else) for all the code that you write, and then stick to them religiously. The actual conventions don’t even matter as much as the sticking to them; what you are aiming for is uniform, consistent code standards across and within projects. This goes for naming of functions, variables, file names – everything, even the amount of whitespace. Write down your conventions if you need to share it across teams – Zend’s ‘coding standard guide‘ for their Zend Framework is a great example of a comprehensive manual of coding and naming conventions. Obviously for your own personal projects it is unlikely you would take it this far; but however far you go it will make it much easier for people who have looked at one piece of your code to pick up another and immediately feel at home.
Comment enough, but not too much
Comments in code are often invaluable for providing information as to the purpose of a particular line, function or other segment of code. In an ideal world the code itself would be entirely self-documenting (see above) – however in reality this is rarely (if ever) achieved, and a few helpfully placed and concise comments can work wonders. But keep the comments short and sweet! A 30 line comment, unless absolutely necessary, will actually hinder understanding of the code, as it adds to the visual ‘noise’ in the script and most people are highly unlikely to actually read it all! Besides, if a function (or anything else) needs 30 lines of comments to explain what it does it is very likely that the real problem is in the clarity of the code itself.
Don’t get too fancy
Whilst it is tempting to condense a 10 line ‘if’ statement into one line by stringing it together into a triple-nested ternary operator expression, this does not necessarily help with the general maintainability or readability of your code! It might get you top geek marks from the most hardcore of programmers, but generally it is not bad thing to split up complex statements to make it clearer to those charged with understanding your code.
Write better code, and less of it
Okay, so maybe this isn’t so easy to implement. But the amount of code you write, together with the quality of it go a long way to making it easy to understand (or not!). Stick to the DRY (Don’t Repeat Yourself) principle, and look at every piece of code you write to see how you can improve it and cut down on the number of lines it takes achieve your aim. Shorter, more concise code that fulfills the same role will generally be easier to understand than a long, rambling, poorly written script, as long as it doesn’t break any of the principles outline above.
So these are a just few things that you can do to improve your code’s maintainability, and make it easy for others (or even yourself!) to pick up the code later on down the line and easily make sense of it. No-one is perfect, and I certainly deviate from these guidelines form time to time. But following these general principles will go a long way to making your code accessible to others.
No Comments »