Engineering

Code reviews – Part 1

Reading Time: 3 mins

As I read an engaging thread on Twitter about the utility of mandatory code reviews, I realized we have benefited a good deal from code reviews.
Our recommended practice is to have at least two levels of code reviews: one at merge request level and another at pre-deployment level.

Part 1 of the code reviews post will focus on certain key checks that are mostly non-functional.

Passwords in codebase

Connection strings are routinely defined in YAML for database connections with passwords hard-coded.

While this would work locally for one developer it wouldn’t for other developers if the passwords configured are different. Not everyone follows the same conventions for defining passwords.

A good practice is to define usernames, passwords and other connection details as environment variables. Assist those who aren’t familiar with defining them.

Relative URLs

Absolute vs relative URL naming is a common pitfall for developers. While it helps the cause of popular claim (it works on my machine!) it might not necessarily work on testing or production environment.

While such practice is documented one can forget during project execution and fallback to their usual practice (ex: js/helper.js vs .js/helper.js). A developer experienced with production environments can guide the inexperienced members in reviews.

Spaghetti code

In jQuery,  $.get() is a simple to use method to fetch server data. Sequential dependent requests from server quickly spiral the code in to spaghetti mode

$.get('endpoint1', function(response1) {
  // do something with response1
  $.get('endpoint2', function(response2) {
    // do something with response2
    $.get('endpoint3', function(response3) {
      // process
    })
  })
})

There are other approaches that can address this situation well.

Tip – Adopting a coding style guide (ex: Airbnb, Google) for your team will ensure uniform coding standards in the codebase.

Commented unused code

Often we find unused code (or dead code) that’s commented in the hope that we will return to it at some point and re-use it.

This creates a ripple effect where the code permeates through different commits across weeks when someone realizes it’s not being used at all and deletes that block.

A better practice is move the commented code block to a snippet (if using GitLab) or a similar commonplace and refer a link in the codebase. The objective is to keep the codebase light enough.

Comment what’s necessary

We write code not only for ourselves, machines but also for our current and future collaborators. Documenting enormously benefits the team in determining the utility of code blocks. JSDoc is a way technique to document code (within JavaScript files) with support for parameters and return values in functions, creating tutorials and exporting the documentation which can be hosted elsewhere.

// an example of documenting a function - https://jsdoc.app/about-getting-started.html
/**
 * Represents a book.
 * @param {string} title - The title of the book.
 * @param {string} author - The author of the book.
 */function book(title, author) {
}

Output of the exported documentation looks as below:

Documentation generated by JSDoc

Storytime.dev is an interesting way to document code but in a story format (play, pause events).

Formatting

Dominantly, we work with Python, JavaScript, HTML, CSS, YAML and follow the default standards (4-space indentation for Python files and 2-space indentation for the rest).

Hide what’s not necessary

Intermediate state values do not reveal much about an application. Defining a variable with lots of default values can be moved to configuration (ex: YAML).

The utility of code reviews will depend on the skill level of the developers an organization employs, focus and bandwidth on training to improve the skills.

In the next part, we will discuss about key functional aspects of code reviews.

Bhanu Kamapantula

Leave a Comment
Share
Published by
Bhanu Kamapantula

Recent Posts

Top Generative AI Use Cases in Healthcare

The emergence of Generative AI (GenAI) is reshaping healthcare use cases and facilitating the rapid… Read More

2 days ago

Generative AI in Pharma Regulation: Insights from FDA, EMA, and Health Canada

The U.S. Food and Drug Administration's (FDA) stance on GenAI is clear: it's a groundbreaking… Read More

1 week ago

AInonymize – AI for Secure Health Data and Innovation

Executive Summary In healthcare, protecting patient information is not just a legal requirement; it's a… Read More

2 weeks ago

How Demand Forecasting Turns Supply Chains into Mind Readers?

Demand forecasting in the supply chain is crucial for optimizing inventory levels and ensuring efficient… Read More

3 weeks ago

LLM Numerology: We Experimented with 3 LLMs to Find Out Their Favorite Numbers

Hi, I am ChatGPT 3.5 Turbo. Do you know what my favorite number is? Do… Read More

4 weeks ago

Data-Driven Sustainability: Achieve Business Value from ESG Data

After a successful webinar on digital transformation and sustainability, we organized a sequel titled “Data-Driven… Read More

1 month ago

This website uses cookies.