You know how they say in those gangsta movies: Snitches got stitches?
Well, even though I’m not a snitch, I had two stitches that my doctor had to remove a few weeks ago.
During my last annual health exam, the doctor has discovered one unusual mole on my arm and suggested that I remove it.
So I did.
It’s a good practice to think about your health in advance. Before you get sick. This way, you can avoid serious health issues later.
In a way, it’s the same with your codebase.
You need to take care of it. Otherwise, it will grow into an unmanageable mess. You won’t notice it at first, but eventually, the code will slow you down. If you don’t take care of it properly.
So, what’s the solution for bad code?
Bad code gets refactoring
It’s a common misconception that the quickest way to fix bad code is to rewrite it entirely. The truth is that the fastest way to fix bad code is by refactoring it.
What the heck is refactoring? Refactoring is the process of changing an application’s source code without changing its behavior.
Refactoring can involve:
- Restructuring the code by adding or removing fields and variables.
- Adding or removing methods.
- Simply changing the code’s visual design by renaming variables and adding or removing comments.
All that may seem like much work, but it’s not. All of those are small actions that multiply when performed continuously. And they are the best way to improve bad code.
Refactoring a day keeps rewrite away.
Bad code gets unit tests
As a developer, it can be tempting to create some bad code.
Maybe you are in a hurry to finish a project. Maybe you just are not sure how to fix a problem. “Oh, as long as it works, it’s okay.” you think.
Unfortunately, the result can be the code that is difficult to read, doesn’t do what you expect, or has bugs.
What happens, though, if the developer who creates the bad code forgets to include tests? That bad code will go live, and the next person to come along will be responsible for figuring out what the code does to fix it.
And that can result in a lot of wasted hours.
Just like it’s not okay to drink and drive, it’s not okay to code without unit tests. Unit tests are the best way to check that the code works correctly. They are critical to catching bugs as they happen.
Unit tests let you take a step back, poke at the code’s behavior, and see how it responds. They are a health check for your code. With the advantage that you can run them every day. Not just once a year.
A journey of thousand tests begins with a single unit test.
Bad code gets design patterns
There is more than one way to write a code. There are many ways to think about a problem. But the best developers are the ones who write clear code.
Sometimes, the code is not so clear, and it becomes hard to read. It can be difficult to understand what the author was trying to do.
But there is hope.
You need design patterns.
What is a design pattern? A design pattern is a reusable solution to a commonly occurring problem in software development. A design pattern is not a finished design that can be transformed directly into code. It is a description or template of how to solve a problem that can be used in many different situations.
A design pattern in code is worth two on the stack overflow.