Defects are forever. No matter how good you are, your code will have defects. Learn to live with them.
- Use code analysis tools: Findbugs, Checkstyle, PMD, whatever. Adopt a zero-noise policy: the only acceptable number of warnings is 0. Suppress warnings not important to you.
- Perform code and design reviews.
- Unit test every class and every public method. If you have code that cannot be tested, it is incorrectly designed.
- Measure unit test code coverage. A test may not test what you believe it tests. 90% statement execution coverage is easily attainable.
- Automate your tests. If you cannot automate a test, it is incorrectly designed.
- QA is your friend, not your enemy. Defects are the enemy.
- Catch every exception and log it at ERROR/FATAL level. Monitor the logs.
- Log every successful operation at DEBUG/INFO level. This provides contextual forensic evidence.
- Implement service health checks. You want to be alerted when the heartbeat stops.
- Scope your error handling appropriately. You may not want a 1000-document operation to fail because one document is bad.
- Test your error handling code. You don’t want a minor error to cause a major error because you never tested the catch clause. Error handling code has as many defects as any other kind of code.
If you believe your code has no defects, you are a bozo. Read my upcoming post on the Taxonomy of Bozos to see where you stand.