Note: This article was kicked off by one I read over at LosTechies.
We write lots of code, statistically speaking some of it is what we’d deem as “crappy.” If you’re doing things right and progressing in your career and your understanding; your definition of “crappy code” should change over time. This is very important concept for a good Software Engineer, its one of the ways we get better. We recognize our past coding mistakes and work to better them.
Dangers of Crappy Code:
- It could hide technical debt. “I don’t really know how or why this works, but here it is. Done.”
- You’ve not taken the time to make what works, right. This potentially leads to bloated code, duplication, and an overall poor product quality.
- Is it robust?
- It is, potentially, not very reusable.
- Can I hand this code off to someone else and they understand it?
Preventing Crappy Code:
The prospect of writing crappy code should not prevent you from getting a project working. However, it is very important to make it as right possible before committing the code changes to the repository.
- Write self-documenting code. Some developers may scoff at this statement, but self documenting code is very possible in just about any language. Remember comments are your failure to communicate.
- Remember to refactor, refactor, and refactor.
- Ask for the opinion of a peer. This should be someone you consider qualified enough to give an opinion.
- Use “TODO” comments so your thought process is not lost and it will serve as a reminder that you must still make something right. This doesn’t prevent crappy code from making it into a project but it documents its existence.
What To Do When You Find Crappy Code:
For the sake of this topic, lets assume you run across some crappy code 2 years into maintenance of a software product.
- Identify why it is “crappy.”
- Does it work or has it unknowingly introduced bugs into the system?
- Perhaps leave a NOTE comment in the code, especially if it can add insight into the function of the crappy code.
- Only change the code if it is within the scope of your current task. Remember that it has worked for 2 years and changing it now could potentially introduce error. If you do end up changing the code, attempt to make it right before you finish. Also make sure to update your unit tests.
Are you Ready? A Simple Test
Review a code base that you wrote 2 years ago. Did you find “crappy code?” The answer should almost certainly be a “Yes.”