Reviewing Code A guide to smelling another developer’s source code
How much should you review? Option 1 – All the Code Option 2 – Parts of the Code Read through every single line Attempt to understand the general structure of everything Read a function Understand the function Find other code related to the function E.g. loading data into a global variable that the function uses Repeat with another function
Review Fewer than 400 LoC (per person) Your brain can only process so much information at a time Reviewing more than 400 LoC means you have to keep track of too many variables, functions, classes, etc. Try to limit yourself to 30-60 minutes of defect discovery This is the time you are reading code and writing notes, not writing the actual code review assignment https://smartbear.com/learn/code-review/best-practices-for-peer-code-review/
Identifying Problems by Smelling Them A Code Smell is not a bug The code works and works correctly A Code Smell likely indicates a weakness in program design i.e. The risk of future bugs arising from this smell is high There are many different kinds of code smell Refactoring: Improving the Design of Existing Code, by Martin Folwer
Long Functions Can Be Bad If a function is longer than 10 lines of code, it smells “I just need to add two more lines to this function…” Repeat the above enough times and your function has grown too large The longer a function is, the harder it is to: Read it Understand it Maintain it
Temporary Variables Can Be Bad Created for convenience by the programmer early on in the scope Typically used as in a complex function or algorithm The temporary variable only receives a real value during some circumstance Example: within an if-statement The variables are usually empty (i.e. have no value) or initialized to something meaningless Harder to debug
Example of a Long Function More Code Here
Example of Temporary Variables If possible, initialize a variable when you declare it.
Which scope should variables belong to? The length variable is only used in the if-statement. dist, dist_first, and dist_last are used in the else statement.
Losing Information on Reassignment By reassigning these to intersection1/2, we’ve actually lost information.
Extraneous Code Do we need to assign the result to length? Why not return directly? Do we lose any readability in doing so?
Comments Can Be Bad Commenting can help explain unintuitive code But the code is still unintuitive… can this be fixed? Some programmers comment out code There is no reason for this – subversion has a history of all your code Comments can become out-dated The code and the comments directly contradict each other Comments are good if… They explain why something is coded a certain way They explain a complex algorithm that is being used
Duplicating Code Duplicated Code smells bad because… It can likely be extracted into its own function It was likely copy-pasted by the programmer
Duplicated Code Example
Duplicated Code Example This code has been copy-pasted. It is clearly duplicate code.
Poor Commenting Example These comments tell the what is about to happen. But they don’t tell me why.
Removing Duplication; Adding the Why
Learning more About Code Smells Interactive Website https://sourcemaking.com/refactoring/smells The Book Refactoring: Improving the Design of Existing Code The Taxonomy Mäntylä, M. V. and Lassenius, C. "Subjective Evaluation of Software Evolvability Using Code Smells: An Empirical Study". Journal of Empirical Software Engineering, vol. 11, no. 3, 2006, pp. 395-431.
An (incomplete) C++ Specific Checklist Does the code compile without warnings? Are there any expensive copies being made? Which objects would be expensive to copy? Are the destructors simple and intuitive? Is there an excessive use of pointers? Manual memory management is hard; rely on the STL for most of your dynamic memory allocation Are variables that can be marked const marked const?
A Good Code Review Is specific Cites examples in the code Is not negative Don’t be mean, be constructive Is not opinionated Not everyone agrees on style and formatting