Code Reviews

Njunu-sk
2 min readAug 16, 2023

--

Code reviews are an essential part of the software development process, aimed at identifying issues, improving code quality, and ensuring that the code aligns with the established coding standards and best practices.

When conducting a code review, here are some principles you should consider:

Principles of Code Review:

  • Readability: Code should be easy to read and understand. If you struggle to understand what a piece of code does, it’s a sign of poor readability.
  • Maintainability: The code should be easy to maintain and modify in the future. Complex and convoluted code can lead to difficulties in making changes.
  • Modularity: Code should be modular, with each module or function having a single responsibility.

Identifying Code Smells:

Code smells are patterns or characteristics in the code that indicate potential issues.

Let’s start with the most common smell, long methods.

Long Methods:

Long methods are a code smell that can be addressed using the “Extract Method” refactoring technique.

This involves breaking down a long method into smaller, more manageable pieces. Here’s how to do it:

Symptoms of Long Methods:

  • You can’t easily understand what a method does just by looking at it.
  • The method has multiple levels of nesting or abstraction.

Example:

after_create_commit do
broadcast_replace_to cart,
target: "...",
partial: "...",
locals: { count: }
end

Steps to Extract Method:

  1. Pick a Name: Choose a descriptive name for the new method that explains what the extracted code does.
  2. Move Code: Move the relevant code from the original method to the new method.
  3. Call the New Method: Replace the extracted code in the original method with a call to the new method.
after_create_commit do
update_item_count
end

private

def update_item_count
broadcast_replace_to cart,
target: "...",
partial: "...",
locals: { count: }
end

In this example, the update_item_count method was extracted from the original code for better readability and maintainability.

In the next section we will discuss the “Move Method” refactoring technique. So, stay tuned this technique builds upon the principles of the “Extract Method” pattern and takes your code improvement journey to the next level.

--

--