Though Its not gangrened to write 100% bug free code, but we can process some below given tips to reduce number of bug in our code.
Writing Bug Free code is depends on various factors, its take lot of knowledge of best practices and some good tool to check quality of code.
Based on article “Research has shown that there is a factor of 20 between the productivity of the best and the worst programmers. That is, top programmers work 20 times faster than the worst ones, and around 5 to 10 times faster than average programmers. This means that a very good developer can do in a day what others may need a month for. There are a couple of ways to increase productivity, but the most important factor is accuracy.”
Know Your Code : Writing a Bug Free code is depends on how much your are good in understanding your code and having good knowledge of your library or API. Some time your don’t need to write many line of code for some of requirement which is already provided by your language’s API.
Like there are many Utility classes in JDK related to collection, array and string manipulation. Knowing to those utility method can save your lot of effort and avoid code which many have bugs. So Rule number 1 is to write Bug Free code is Know your code and know your language utility classes.
Using TDD : Test Driven Development can save you from writing code with Bug . Using TDD You can first focus on main logical implementation of your business requirement and write your Test Cases accordingly. Focused approach of writing TDD is very much required for writing bug free code.
Writing test case also saves your in future from unwanted changes in code. So writing test case with maximum coverage is required for writing quality code and bug free code.
Multi-Environment Testing : Dedicated and multi-environment testing is required for knowing your bug before going to production. You can test or your testing team can test your functionality and business requirement on multiple VMs or different kind of system configuration .
Code Conduct : Following proper coding convention and principal while writing can save you from having hidden bug in your code. So follow best coding rules and adhere “Effective Java”, “Clean Code ” like principal.
Write Short and Focused Code : High Cohesion and Low Coupling always safe guard us from bugs at some extends. Your function should do one thing and should do effective.If your function are doing more than one thing you can further refactor your function into sub-functions. So here rule is don’t put lot of login into single function body,avoid writing long function and enjoy bug free code.
Using Nice Debugging Tool : There are lot of inbuilt tool provided in your IDE, proper use of debugging tool and applying debugging technique is required for having bug free code .
“Zero-Bug” code is near to impossible but as a coder we should follow the best practices and adhere to that.Also knowing language fundamental is very much important for writing Bug Free code.