Why code quality is important? If a piece of code or a function works as expected (generating correct output) then shouldn’t it be considered good?
Well… No! This piece of code might be generating correct output but is it readable, reliable, secure, reusable, portable, maintainable, and optimal?
● If the answer is yes, you are good.
● If the answer is no, then you need to review the code
First and foremost is to develop an attitude or a habit to review the code.
The most important mindset for any development team to have is to think there will be mistakes in the code and hence it should be reviewed. This is the key to producing quality code because it opens up the form to review and discuss what can be improved.
Code review is a great way to analyze the code before moving the code to the “main” branch. The code review step should be mandatory for every “Pull Request” unless it is not evaluated on the defined set of rules it should not be merged. Below is a small depiction of the code review stage.
The reviewer should remember code review is not to find out mistakes in an individual. We all are humans and make mistakes. The crucial part is to ensure the code is reviewed and clear feedback is provided to ensure the developer can easily understand and make the correction.
So as we understand the importance and the agenda let’s understand the code review best practices.
● Define coding standards - There are a lot of blogs for coding standards. But each team has different needs and practices so, define your own coding standards.
● Pull requests should be analyzed no matter who wrote the code - Code review should be a practice and it should be equal for all. Whoever writes the code should be reviewed. Peer review can also be done.
● Use static code analysis tools - Manual code review has its own advantages but using static code analysis tools (like SonarQube etc) is helpful because of the huge code validation rules built in.
● Define code check-in limit - The pull requests should be small so that reviewers can easily review. The limit should be on the lines of code checked in and the time it takes to review the code.
● Feedback should be constructive - Code reviews should foster a positive and collaborative environment. Feedback should focus on the code and not the person. The review also includes suggestions for code improvement.
● Learn from the process - Learn from the code reviews and avoid making the same mistakes
Speak With Expert Engineers