Writing Good Code
Writing outstanding code is hard. Almost by definition you have to do everything right.
Writing good code on the other hand is simple. All you have to do is avoid three major mistakes. If you avoid them, you almost automatically write good code that is easy to read, maintain and extend.
If you commit any of these major mistakes somewhat regularly, your code will be bad. Your colleagues will hate it. And There is a very high chance your code basis will degenerate into a mess sooner or later.
Here are the three major mistakes to avoid.
Step 1: Do Not Repeat Yourself
When solving a problem that has been solved before, one possible approach is to just copy the code, and maybe change the copy slightly. This is called duplication. Why is this such a bad thing?
With every copy you create, you double the amount of code to maintain. So whenever that logic is changed you need to update it in multiple places. Worse, you need to be aware of all the copies. This is not realistic for any non-trivial code base.
Over time the copies will diverge. A reader has no clue what differences are essential to the individual copy and what changes are related to the actual logic.
So if you are solving a problem that you’ve already solved in the code base, simply extract the original code and call it in both places. This also applies if you are solving a very similar problem.
Step 2: Don’t Lie
We’ve all seen it. Code that claims to do one thing but then does something different. And with programming it’s the same thing as with people. If they lie to you once it’s annoying. But if they lie to you several times you cannot trust them any more. You challenge every single statement. And dealing with them usually is a terrible experience.
With source code it is exactly the same experience. Once there are a few lies burred in the code, you cannot possibly trust any part of it. Any you know whatever you implement based on it has to fail. You don’t know where it fails or in what way, but you instinctively know something will always fail. No one want’s to work with code like this.
Fortunately the solution to this is very simple: just don’t lie. Make sure your interface, class, and method etc. names describe the behavior accurately.
I’ve written a separate article on lies in source code here: https://erichschaer.com/dont-lie/.
Step 3: Test Your Code
Not testing your code is basically the little brother of lying to the reads of the code. Software Engineers make mistakes, are humans after all. If you write a hundred methods, a percentage of them will not do exactly what they are intended to do.
Without testing you basically are lying to your callers by omission. Again the result is the same, parts of the code don’t do what they claim that they do, there are bugs everywhere and no one trusts the code.
Again the solution is simple: just test your code. Ensure the code actually does what it should do.
You can find more on how to get started with testing here: https://erichschaer.com/test-your-code/.
Additional Benefit
If you follow the guidelines outlined above, your code will be easy to read and easy refactor. And this enables you to quickly improve the code further. So your code will be easy to maintain and extend.
Conclusion
- It’s simple to write good code.
- Don’t copy code around. Reuse it!
- Don’t lie to the reader.
- Test your code.