To be a highly effective coder who produces high-quality code consistently, follow these coding principles:
I’ll explain these principles and why they make you an effective coder.
Always test your code. Writing tons of code without verifying it until the very end, if at all, is a recipe for disaster. You’ll have many bugs to deal with, and fixing those bugs will probably uncover more bugs.
Plain and simple, test the code you write.
At a bare minimum, manually test your code. Yes, even if you have testers working on your team (you’re lucky if you do!).
It’s even better to have automated tests though. You can run ALL of the tests repeatedly and get feedback for all the code you already wrote. Nothing beats that. As the code grows, it becomes more and more difficult to manually test every scenario.
Automated unit tests are a good start.
Why? Because they are small, fast, and specifically for coders.
To be able to unit test code, it must be testable. Spaghetti code is incredibly difficult to test because it’s long, does tons of things, and has hardcoded dependencies. Testable code is the opposite. It’s small, solves a single problem, and has dependencies injected. Testable code is good code.
Always be refactoring.
Your code may be working fine and passing all the tests. Its functionality may be perfect. But it could still be full of problems such as duplicate logic, very long methods, monster classes that do everything, magic numbers being used all over the place. The list goes on and on.
Good code is readable and easy to maintain.
That’s where refactoring comes in. When you refactor code, you fix maintainability issues while leaving the functionality intact.
When you don’t refactor, your code tends to start stacking up problems. The longer you wait to refactor, the longer it will take to do the refactoring. It’s a compound effect. Refactoring one issue may reveal several different problems. This is why you should always be refactoring. Deal with problems while they’re small.
Always refactor in small steps, and only refactor code that’s covered by tests.
Refactoring and testing go together. Automated tests act as a safety net that allow you to refactor with confidence. When your refactoring breaks functionality, your tests will catch the problem. You can then revert the small refactoring step you just did and try a different approach. This is why it’s important to do small steps and have tests in place.
Be pragmatic. This means:
- Focus on what matters right now.
- Don’t overcomplicate things.
- Do what works and move on. It doesn’t need to be perfect and you can always refactor.
- Bend the rules when necessary (as a team).
- Don’t waste effort on micro-optimizations.
- Prove it, don’t assume it.
- Test it, don’t assume it works.
- Don’t nitpick in code reviews.
The main objective of coding is to make useful software that solves problems and works well. When you’re pragmatic, you are focused on the end results. You complete tasks correctly and quickly. You never block other coders with pointless nitpicking, arguing about non-issues, or micromanaging. You are capable of adapting to changing circumstances. You’re a highly effective coder that makes useful software.