Three Tips For Writing Better Code

Published: Sept. 27, 2019, 10 a.m.

In this episode, we look at a favorite topic.  Here are three ways to write better code.  We create a lot of source code during our career.  Thus, this is an area where the ROI can be huge when you take these steps early on.  These will not only help you write code that is easier to maintain, but it will also reduce your bugs and improve stability.

Use Static Analysis Tools For Better Code

One of the best ways to improve your code quality is to find another set of eyes to provide a sanity check.  This step can avoid blatant bugs, reduce code duplication, and ensure you conform to standards.  The best set of eyes is an automated process like these tools provide.  They often draw from best practices and industry standards.  Therefore, you can also learn a significant amount about the language from these results.  They will point out common flaws and can assist you in creating good code writing habits.

Some Static Code Analysis Tools

Unit Testing

It may seem self-evident that testing improves quality.  However, that is not the focus of this suggestion.  When you spend time walking through how the code should work (aka design), it is always worthwhile.  We learn how to build a better solution during this process as well as often thinking through exceptions in greater detail. The practice of building unit tests pushes us to take each of these design steps thoroughly.  We are forced to consider the "happy path" and the exceptions, as well as expected outcomes, for each of those cases.  When you spend time on this, you will often find yourself creating a more complete solution that is resilient.  Your users will be happier for it.

Code Reviews

Yes, this is very similar to the first item.  However, it shows how important reviewing your work is.  There is a lot to be gained from both static analysis tools and a human review.  The details are more likely to be caught to the automated tools, while style and readability are more often the focus of your team members.  They will also be able to tell you if your solution "makes sense" or is hard to follow.  There will also be business rules and terminology they can catch that is hard to configure into a tool.

This step is one that gives to all involved.  You can learn from reviewing the work of others as much as from their input about yours.  That adds up to a highly valuable team activity.

Learn More: Learning From Code Reviews