8 ways to reduce bugs while coding
There was an interesting discussion at one of our internal mailing lists on how to reduce the number of bugs while coding. I am going to summarize the discussion here. Some of the points that were mentioned.
- Write unit tests & integration tests for your module. Follow the principle of Test-Code-Test, write happy / failure test cases which would help you identify all possible inputs to your function and accordingly handle it.
- Use Tools: Findbugs uses static analysis to find bugs in Java code, GetExceptional tracks errors in Ruby apps and reports them, Selenium helps you to easily check if the web elements are in place in different browsers and many more. It’s good to use these tools which make your life easy.
- Compiler warnings: Don’t ignore compiler warnings. Often, they easily help you identify the bug in your code. Before you start debugging, compile your code once with the highest warning level and see if there are some obvious errors.
- Code Review: Get your peers to look at your code before pushing it to production environment. Code reviews should be taken as a challenge to find out bugs in the other person’s code.
- Logs: Loggers like log4j are easy to setup. Split the type of action into 3 types – error, warning and info and ensure you log a good amount of actions in your code, you should be easily able to trace what has happened by looking at the log file.
- Use existing libraries: Don’t reinvent the wheel. If there is a well tested library that does the same function you were planning to code, use it! The library is used by many developers and it should have been a well tested one.
- Pseudo code: It’s always better to write a pseudo-code of what you are planning to do before you start coding the module.
- Avoid distractions: Distraction is the number one enemy for buggy code – a GTalk ping / twitter update can disturb your thought process. Use techniques like Pomodoro and tools like RescueTime to stay focused.