Got Sloppy Code?


Bad code could have been created by hasty programmers, or it may have been inherited. Learn how to reduce bad code and the problems that accompany it.

Bad code can be pretty funny, but it can also be disastrous, especially if errors surface down the road; another team could have taken over the project by then, making things worse. Some errors are not easy to spot and resolving a single issue may take weeks, only to discover that something silly had caused all the trouble, like a misspelled variable. Bad code is surprisingly common and may have nothing to do with programming ability; a good programmer could still write bad code.

Here are some tips and tricks to avoid bad code:

Format your code well so it is easy to be read by others. Well-formatted and modularized programs have a reputation for being easier to debug. It might take a little extra time to code, but is well worth the effort.

Keep modules just long enough. Using deeply nested loops can be very powerful, but it can also pose quite a challenge when it comes to spotting mistakes within or making changes later on. Write modules that you can read and follow from top to bottom.

Agility is important, but so is clarity. Optimizing non-critical parts of your program for speed increases complexity making debugging and maintenance difficult. Put some thought into what parts you want to modify and what you can let be.

Underestimation is a bane. Time and level of effort are usually underestimated during the project planning phase, leading to runaway costs and timelines. In some cases, it is a way to keep management happy, albeit for a short while, but in most cases it happens due to lack of information, thereby forcing teams to rely on unrealistic estimates. Time-stressed projects lead to hasty programmers generating bad code in order to meet impracticable deadlines. Understand your code and systems for improved project management and better code.

Sometimes all your code needs is a different set of eyes. Pair programming is a pretty intense method to review code but peer review could be useful too. Get a colleague or friend (or use a software) to review your code from beginning to end in order to spot mistakes.

Keep it straightforward. Use variable names that are not easy to misspell. Try to choose names that reflect what you assign to them so they are easy to follow.

Shitsuke (sustaining) is not easy. Identifying good code is almost as important as identifying bad code. Once you have identified what is good code, write down what your standards are, so as to avoid inconsistencies and sustain good coding practices. Coding standards will most likely change in a few years, so be sure to update your standards.

If you have inherited bad code, carefully plan out your course of action. Spend time to evaluate the situation. Encapsulate bad code, flag it and proceed to fixing it or deleting it. Delete code with caution as it may have links you do not want to break. It is not unusual for teams to dedicate more than 30% of project time for clean-up, but there are tools out there that can get the job done faster. We hope our tips help you and your team reduce bad code, and avoid problems down the road.


(Comic - property of xkcd)


Tags: Code Review, QA