Like writing or painting, coding is an art. Code can be beautiful, well thought out, and organized but in the real world, we all know that “making something work” comes before clean code. That said, there’s steps you can take to make your code closer to an elegant place, while making your life easier at the same time:
- Think modular. Whether creating a function, object, or block of code, you should always ask yourself when and where (almost never a question of if) this code is going to be reused or could be reused. This allows a programmer to think in more general terms and get to the core of what the function should be or what the object should look like. Do note that sometimes this can lead to making functions or objects that are TOO generalized. If you find yourself writing many case exceptions within your function (or adding classes/style modifiers) go back and rethink what you are doing. Writing 2-3 small functions is better than writing one big one with many exceptions.
- Comments are king. This ensures others (and yourself in six months) know exactly what you were doing. The comment should be informative, vs. just a date and a name, unless that’s all that’s really necessary. Some write a detailed comment about the problem at hand and what I’m trying to accomplish. Others use comments to determine when specific code could be generalized and reused.
- Check yourself before you wreck yourself. Try to go over your own work within 24 hours of completing a piece to test and confirm what you did makes sense (essentially, a sanity check). It’s a good idea to check your work when the solution is still relatively fresh in your head and after you’ve had time to clear your head. It will make more sense than waiting weeks to the beta period of a project, where you might not remember all the moving parts involved and risk breaking something.
- Know your data. Be conscious of what is happening inside of loops. You don’t want to perform slow operation multiple times when you can perform it once outside the loop. When bulk editing data, save once.
- Follow KISS / DRY rules. This principle basically states that the shortest distance between two points is a straight line, and that this should be taken into account when writing code – the simplest solution is usually the best solution (in other words, KISS: “Keep it Short and Simple”). Rails programming follows the DRY rule, or “Don’t Repeat Yourself,” which we try to carry over to PHP.
- Look for patterns. Look for patterns and generalize the code as you go along. If you repeat a piece of code more than a couple of times (particularly lengthier ones), than it’s a prime candidate for being generalized. This counts double for CSS, where file size limits can rule, particularly in Internet Explorer.
- Understand your conditions. When branching, if there are multiple conditions in the branch the first condition will be evaluated before the latter ones. It may be possible to save operations placing the simple (faster) condition before the others. This is true for AND and OR operators.
- Spriting images is always a good idea for reducing the number of calls to the server and speeding up loading. Sprites are also generally smaller in file size than the aggregate of the individual images as well.
- No conflicts? No problem. Make sure you know what JS libraries are being used, and if JQuery no conflict is needed, place it in an optimal place so you won’t have any problems later.
What are your ways for generating clean code? Let us know in the comments!