The Death of Code Review
Code Review is Dead
IMHO code review is dead, in a zombie state, where it happens but not really functional anymore. Here is a set of questions to diagnose if your code review is dead or alive:
- Is code review part of a clear and visible process with defined policies?
- Do you need to be chasing engineers around?
- Does the team and management think code review requires time and is part of the team effort?
- When was the last time you learned something useful in a code review?
- Do you get more than LGTM?
LGTM
LGTM has a simple rule that teams are following secretly, which is,10 lines of changes in the PR you get 10 comments and 10 change requests, 100 lines or 100 files, and zero comments. You only get comments on small PRs. What does that tell you? LGTM means I did not review it, and is your problem if you break it you should fix it. Nothing against people having ownership and responsibility for their acts but the notion of ownership is a bit wrong in this case.
Software is complex and is a team sport, we need to have a high bar for quality and engineering otherwise we are digging our grave. The speed you gain to release today will disappear tomorrow. It's possible to have speed and quality, actually, one thing is not the opposite of the other, and quality enhances speed.
Code Review should be more than just coding style
Unfortunately, code review becomes, when it happens, a fashion show, is all about style. People just care about the name of the variables, having smaller methods, and if the is duplicated code, why API X was used instead of API Z. This is wrong, and a waste of time.
Seasoned teams need to be focusing beyond style. 2020, 3 years ago I was blogging about why we need a clean code DETOX. We still need it, we think we are clean from clean code, but we are not really, we think we learned a better, did we really learn a better way? ...or we think we learned? How do we know?
Code reviews, don't need to happen only when there is a PR, deltas are not the best way to see what is going on in the code base.
Going Beyond Style
Code review is a tool to manage technical debt, and is an opportunity to do refactoring and improve the system. Even if little, the opportunity should be taken and some refactoring should happen. Code review is a tool for us to share knowledge and educate the team.
We can do better code reviews, here are a couple of dimensions that should be explored in code reviews, beyond style.
Corner Cases
Corner cases create bugs, everything has corner cases, here is a list of ideas to check for corner cases:
- If is an integer, did you check for zero? what about negative values? divide by zero?
- If is a string, did you check for empty or null? what about encoding? what is the default?
- If you have a range from 2 to 6, check the upper bound, the lower bound, should be inclusive?
- If is an array, did it check for overflow? what about lower and upper bounds?
- When working with dates, consider timezones, and DST, does the code use the TZ from the server? is the server in sync?
- When working with IFs, did you test all possible branches? did you check for coverage or mutation testing?
- Did you test the failure modes, did you force the exceptions and error scenarios? Are there tests for that?
- Race Conditions: what happens if we have multiple threads on this particular code? would it break?
- Any double values? Like null, does null mean null or also mean nothing or something else?
Tests
When you add a feature, you should have tests, testing diversity is very important, we should have all forms of testing like:
- Unit Testing
- Integration Testing
- Stress Testing / Load Testing
- Chaos Testing (Chaos Engineering)
- Property Based Testing
- Mutation Testing
- Testing your own language and frameworks
- Testing internal libraries
- Make sure the code is being called
- Testing Loggers
- Testing Getters/Setters and Constructors with no code
- Testing Enums (That just have structure and no business logic)
- Testing Persistence
Observability
Decent code review looks for lack of observability like:
- Missing Alerts (which should be actionable, otherwise is just noise).
- Lack of Dashboards
- Lack of custom instrumentation
- Lack of error observability
- Lack of basic OS level and sever metrics like: memory, CPU, io, network, queue length.
- Lack of latency distributions p50,p75,p90,p95 and p99.
- Lack of Tagging
Migrations
Some features and projects require migrations, migration plans should be reviewed as part of the code review, and some PRs need to handle the migration for consumers/users, is important to keep in mind:
- Do we have a solid migration plan?
- Do we have a rollback plan? did we test the rollback?
- Do we have observability, if something goes wrong would we know? Do we publish the right metrics?
- What about failure more, if something goes wrong? would the system be stuck? Can we recover?
- Does the migration need to be online or offline?
Design and Architecture
What is the architecture of the system? Do we have a design document? Are we following the design decisions? Or we don't have a strong design? Are decisions just being made as we go? Do we have any structure we want to enforce or avoid? All good questions for design and architecture review that should be part of the code review.
Architecture and design are about concepts and decisions that are reflected in the code. Having diagrams makes understanding easier and can be a strong tool to help the team understand and improve over time.
Do we need to update any diagrams? Design document? Or a Wiki page?
Attention to Detail
Great code reviews are all about attention to detail. Attention to detail can be hard, due to several internal and external pressures, here are a couple of recommendations to improve your attention to detail:
- Have a checklist, GitHub has a nice feature to automate that.
- Did you run all the tests locally?
- IF is a backend code, did you run it? IF is frontend, did you open the app?
- Review 3 times, have a quick break, and don't rush it.
- Did you read the ticket or use the story again to make sure you delivered all requirements or acceptance criteria as it should?
- Did you run the engineering business review checks?
- Did you ask for the most senior person on the team to take a look?
- Did you look for small details like server boot-up logs, browser logs, build warnings, and IF frontend - is everything rendering properly?
Opportunity to Teach and Learn
Code review is an opportunity. Did you consider running some code reviews in groups? Code review is a good way to learn about decisions, taught processes, and even learn engineering techniques from more seasoned engineers. Do you review your code reviews? That's another opportunity to do better and learn from mistakes from the past. If you did not get a review on your code reviews, how can you do better?
Are code reviews triggering architecture and design conversations? Is the team aware of common anti-patterns on the code base to be avoided? Are the best patterns clear are known by the whole team?
Doing Better Code Reviews
Here are some recommendations, for doing better code reviews:
- Do not fear changing code, if you feat it, lose the fear.
- Stop thinking about style in code reviews.
- Have checklists. Use checklists for CRs.
- Combine code reviews with Business Reviews.
- Go beyond style in code reviews, look for Corner cases, Testing opportunities, Observability, Improve documentation for Architecture and Design, and cover migration strategy.
- Prioritize Code reviews, more than doing new features.
- Code review can be an instrumental tool to break out of the tech debt cycle.
- Review your code reviews, and consider group sessions.
- Retrospectives, or at least call out in 1:1s to management.
- Consider collecting code review metrics and performing analysis to improve your process.
- Spend time, downloading the code in your idea, running the application, giving constructive feedback, sharing links, and explaining why something is wrong or how could be better.
- Use code review to reduce the complexity of the system.
- Never give up, never surrender, and have infinite patience, some problems take longer to fix, but they can be fixed and they will, never give up.
Cheers,
Diego Pacheco