The Importance of Code Reviews for Noobs

As developers, we all have our own unique coding styles and techniques. However, it's important to remember that we're not just writing code for ourselves - we're writing it for others to read and understand. That's where code reviews come in. In this post, we'll explore why code reviews are important, and how they can help you become a better developer.

Code Reviews: A Guide (of sorts)

Code reviews are a process of examining code written by other developers to ensure that it meets specific coding standards, is well-structured, and functions correctly. They provide an opportunity to learn from more experienced developers, while also improving the overall quality of the codebase.

How Code Reviews Help New Developers Learn and…Develop

For new developers, code reviews provide an opportunity to learn from more experienced developers. By reviewing the code of others, new developers can see how experienced developers approach problems, write code, and structure programs. They can learn new coding techniques, design patterns, and best practices. Code reviews also provide new developers with valuable feedback on their own code, helping them to identify areas for improvement and develop better coding habits.

The Importance of Reading Code, and How Code Reviews Can Help

Reading code can be a challenging task, especially when the codebase is large and complex. Code reviews provide an opportunity to see how code is structured and how different parts of the program interact with each other. Developers can learn how to navigate code more efficiently, how to identify bugs and errors, and how to understand the purpose of different functions and variables.

Here’s a small snippet of code we can use as an example…

function calculateTotalPrice(cartItems) {
  let totalPrice = 0;

  for (let i = 0; i < cartItems.length; i++) {
    const item = cartItems[i];

    if (item.quantity < 1) {
      continue;
    }

    const itemPrice = item.price * item.quantity;
    totalPrice += itemPrice;
  }

  if (totalPrice > 100) {
    totalPrice *= 0.9;
  }

  return totalPrice;
}

The function above calculates the total price of items in a shopping cart and applies a discount if the total price exceeds $100. There are a few areas that could benefit from a code review.

This function does not have any input validation. It will assume that the argument passed to the function is an array of cart items. If incorrect data type or an empty array is passed, the function will not work correctly. Input validation should be added to ensure that the function only accepts valid input.

The function also uses a “for” loop to iterate over the cart items. While this is a valid approach, it can be improved by using a forEach loop or a reduce function. This would make the code more concise and easier to read.

Finally, the function uses a magic number (100) to determine the discount threshold. Magic numbers (numbers in your code that appear arbitrary), should be avoided in code, as they can make the code difficult to maintain and modify in the future. A constant or calculation should be used instead to make the code more readable and easier to modify.

With just a few modifications, this function can be improved in terms of readability, maintainability, and reliability.

Improving Code Quality, One Review at a Time

Code reviews are critical for ensuring that the codebase is of high quality. Code reviews can identify potential bugs, security vulnerabilities, and performance issues. They can also ensure that the code adheres to specific coding standards and that it is easy to maintain and modify in the future. Code reviews help to prevent the introduction of new bugs and errors, and they can also identify areas where the code can be optimized for better performance.

Code reviews should be a regular part of the coding process, especially for beginners, such as myself. Not only is this a regular part of working on a team, but reading through your own code, as well as code from more experienced developers can improve your own coding skills and help you create better products overall.

Now, go open up your IDE and get to reading!