How to code review for awesomeness and clarity

hanochaloni 8 views 28 slides Sep 29, 2024
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

How to do a code review?


Slide Content

How to code review

Benefits Better code Better developers Team ownership Healthy debates

Roles

As a reviewee - provide context

As a reviewer Ask don’t tell

Ask , don’t tell

Extract method to reduce duplication

What do you think about Extracting method to reduce duplication ?

Ask, Don’t tell What do you think about… ? Did you consider… ? Can you clarify… ? Why didn’t you… ?

Questions about the code Is the code clean and modular? Can I understand the code easily? Does it use meaningful names and whitespace? Is there duplicated code? Can I provide another layer of abstraction? Is each function and module necessary? Is each function or module too long?

Optimization questions <Beware of premature optimization> Is the code efficient? Are there loops or other steps I can vectorize? Can I use better data structures to optimize any steps? Can I shorten the number of calculations needed for any steps? Can I use generators or multiprocessing to optimize any steps?

Documentation Is the documentation effective? Are inline comments concise and meaningful? Is there complex code that's missing documentation? Do functions use effective docstrings? Is the necessary project documentation provided?

Testing Is the code well tested? Does the code high test coverage? Do tests check for interesting cases? Are the tests readable? Can the tests be made more efficient?

Logging Is the logging effective? Are log messages clear, concise, and professional? Do they include all relevant and useful information? Do they use the appropriate logging level?

Try to avoid using the words "I" and "you" in your comments. You want to avoid comments that sound personal to bring the attention of the review to the code and not to themselves.

Conflict We don’t agree on the issue We don’t agree on the process

What to review? Small changes SRP Naming Complexity Test coverage

What to review? Use a checklist!

Examples?

let a = new Date (); let b = a. getHours (); if (b > 12 ) { console . log ( 'Good day.' ); } Unclear Naming Could you use more descriptive variable names than 'a' and 'b'? JAVASCRIPT

function calculateDiscount (price) { return price * 0.90 ; } function applyDiscount (price) { return price * 0.90 ; } Duplicated Code This functionality appears to be duplicated in two functions. Could we merge them into one to reduce redundancy? JAVASCRIPT

function processData (data) { // processing data data. map (d => d * 2 ); return data; } No Documentation Can you add a comment explaining what 'processData' does and perhaps why it multiplies data items by 2? JAVASCRIPT

function greet (timeOfDay) { const messages = { morning : 'Good morning' , afternoon : 'Good afternoon' , evening : 'Good evening' }; console . log (messages[timeOfDay] || 'Hello' ); } Modular and Clean <Why ask a question?> JAVASCRIPT

public void ProcessData ( int [] data) { foreach ( var item in data) { if (item > 100 ) { Console.WriteLine(item); } else if (item < 50 ) { Console.WriteLine(item * 2 ); } else { Console.WriteLine(item / 2 ); } } } Overly Complex Method This method seems to handle multiple conditions; could we split this into smaller, more focused methods? C#

public int Divide ( int x, int y) { return x / y; } Lack of Error Handling What happens if 'y' is zero? Should we add error handling for that case? C#

public void LogData ( string message) { if (message.Contains( "error" )) { Console.WriteLine( "ERROR: " + message); } else { Console.WriteLine( "Info: " + message); } } Inconsistent Logging Levels Should we standardize how we log messages to ensure they align with logging levels? C#

/// <summary> /// Calculates the area of a circle given the radius. /// </summary> /// <param name="radius"> The radius of the circle. </param> /// <returns> The area of the circle. </returns> public double CalculateArea ( double radius) { return Math.PI * radius * radius; } Effective Use of Documentation and Clean Code <Seems legit, all neat and clean? C#

Thank you