We have already mentioned on the blog that some topics are considered classics of software engineering. Examples include UML diagrams (not only class diagrams, but also state diagrams), preventing code smells, refactoring and many other areas. They also include techniques for verifying not just functionality but the quality of the source code itself, and one such method is the code review.
What is a code review?
Most of you probably sense intuitively that a review means reading and analyzing the code behind a given piece of software and finding places that:
- could potentially cause bugs, even if testing did not catch them or they simply have not occurred yet,
- prevent or hinder further development of the software (for example, adding new processing paths would require a major rebuild),
- do not follow the coding standard used by the team,
- introduce security vulnerabilities,
- use an inefficient solution that could be fixed relatively easily.
Of course, these are short-term reasons: they apply right now and concern one specific task (although they are often propagated to other parts of the system). A much more important motivation for code reviews are the long-term factors, which at least partly protect the future development of the software.
The first of these factors is the propagation of changes mentioned above. When a system consists of many features and sets of views, each functional part follows its own rules, yet it still relies on others to some extent. It partly reuses, or even duplicates, fragments of code from other places and adapts them to its own needs. This is often faster and more sensible than writing everything from scratch. But that is exactly why it is so important for the code that other features build on to be of the best possible quality. Why? The argument is simple: copying faulty or imprecise code means that fixes later have to be made in two or more places, and we all know that this slows work down and causes frustration.
The second factor is proper management of the development of juniors, beginners, interns and anyone else whose (commercial) programming experience is not very long yet. As a result, they have not built certain habits yet, and that process is still under way. That is why code reviews are especially important for such people: they reinforce good practices and point out bad ones. As the saying goes, what the shell is soaked in when young, it will smell of in old age. If a young person automatically applies incorrect programming habits, we can expect problems in the future. Of course, not everything can be weeded out, but we can try, and code reviews are a good opportunity for that.
Code reviews themselves belong to the techniques of static code analysis, meaning that the code is verified without running the program. This has its advantages: we can focus on one thing, and it is fairly easy to do, even without a configured project and without downloading it to disk. On the other hand, some bugs are simply easy to miss during a plain read of the code. That is why it is worth combining static verification with dynamic verification and simply running the given piece of software to see how individual elements behave in practice (for example, with a DoD list at hand). Let us remember that the result of a code review should be not only better source code and positive changes spreading through the development team, but above all simply better software.
Is there another way to approach it?
Another code verification technique, which some of you may have heard about in classes, courses or while studying the concept of agile methodologies, is pair programming, also known as extreme programming. Two people work on a given feature at the same time. One of them actually writes the code, while the other sits alongside, supervises the process, offers ideas and evaluates what the first programmer writes as they go (in as neutral a tone as possible: the point is not to take things out on a colleague). After a while the roles are swapped. And the pairs themselves also change from time to time.
In theory it may look like a waste of human resources, since the second person could be doing something else in the meantime. In practice, however, pair programming has several advantages:
- it is in effect a code review, but done continuously: the second person can catch certain programming pitfalls while they are being written, not only after a whole large function exists. This can shorten the time spent on fixes later,
- it spreads knowledge about the code across the team, which is why it is so important to mix the pairs, so that information about the code in a given project naturally "diffuses" through the group of programmers,
- it lets everyone get better, especially when junior programmers are paired with more experienced ones, but also the other way round: experienced developers can see what solution the "young one" has in mind,
- it helps you better understand the problems other programmers face and try to help solve them, so they do not go around in circles. Stand-up meetings will not help with everything, and sometimes you have to see it up close,
- it adds a bit of "positive stress": under the watchful eye of an "audience", programmers focus on the work and improve their craft. It can also prevent procrastination, unless you end up with two "slackers" in a pair,
- it strengthens social bonds in the team.
Of course, it has to be balanced. Not every team will work well in pairs, conditions do not always allow it, and some tasks are unsuitable. For example, purely mechanical requirements (such as yet another CRUD) are so "straightforward" that pairing programmers there can indeed be seen as a waste of resources. Still, it is a technique worth a closer look.
How do we do it at Wilda Software?
We do code reviews, usually after every task. Even when a larger group of changes piles up, each review still covers each modification separately. Besides the static analysis itself, for most tasks we have DoD lists that let us verify the functionality dynamically as well. That is our standard. Of course, depending on the task we can also adopt other ways of verification (including automated tests), which go beyond the topic of this article.
We pay special attention to reviews of code written by new team members. As I described above, building certain habits and reinforcing standards is particularly important in their case, so the code review is much more thorough there. It usually pays off later, because the reviewer has lighter work in subsequent reviews.
And who does the analysis? Most often our lead developers or simply the person who created the task. Sometimes reviews are also done within the team by someone who has not dealt with the given task before. This way, knowledge about the functionality and individual pieces of code can be passed between people faster.
Summary
Code reviews may seem unnecessary because they take time. But that is short-sighted thinking. In practice, without reviews the code deteriorates very quickly, and after some time the software needs a thorough rebuild, so the IT team loses even more time. Code reviews will not prevent that outcome entirely, but they let you keep it under control, reduce it significantly and spot the need to refactor smaller fragments earlier, which has a positive effect on the later development of the code.
Best regards and thank you, Jakub Rojek.


