70% of a Developer's Time is Spent Reading Code. See How to Reduce it with Clean Code

Introduction

Hello, devs! Did you know that about 70% of our time is spent reading and understanding code, while only 30% is actually coding?

According to a study by Bin Lin and Gregorio Robles (https://cs.paperswithcode.com/paper/investigating-the-impact-of-vocabulary), developers spend most of their time comprehending source code during software development.

The study highlights that easy-to-understand code offers several benefits, such as facilitating code reviews and speeding up the onboarding process for new developers. Additionally, renowned Clean Code author Uncle Bob states that in legacy systems, this ratio can be even higher, reaching 10:1. Clean Code techniques are essential for improving our productivity and efficiency.

Let’s explore how these can make a difference.

Technique 1: Meaningful Names

Meaningful names are fundamental for code clarity.

Using clear and descriptive names for variables, functions, and classes helps quickly understand the purpose and functionality of each element.

Avoid abbreviations and prefer terms that clearly express the code’s intent.

// Bad example
public void Calc(int d)
{
    var r = d * 1.19;
    Console.WriteLine(r);
}

// Good example
public void CalculateTotalPrice(int basePrice)
{
    const decimal taxRate = 0.19m;
    decimal totalPrice = basePrice * (1 + taxRate);
    Console.WriteLine(totalPrice);
}

Technique 2: Small Methods

Keeping methods short and specific is an essential Clean Code practice. This approach is also fundamental in Clean Architecture to ensure that each component has a single responsibility.

Small methods are easier to read, understand, and test. They should do only one thing and do it well.

If a method is doing more than one task, consider splitting it into smaller methods.

This approach not only improves readability but also makes code maintenance and reuse easier.

Technique 3: Consistent Formatting

Consistent code formatting helps maintain organization and readability.

Follow a defined formatting standard and use linting tools to ensure compliance. Indentation, spacing, and line breaks are important aspects to consider.

Additionally, techniques such as fail fast and avoiding nested if-else statements help make the code clearer and more understandable. See below the difference in readability between two methods by simply repositioning the return statement and avoiding nested ifs.

// Bad example
public void ProcessUser(User user)
{
    if (user is not null)
    {
        if (user.HasSubscription)
        {
            if (user.Age >= 18)
            {
                ShowFullVersion();
            }
            else
            {
                ShowChildVersion();
            }
        }
        else
        {
            throw new Exception("User needs a subscription");
        }
    }
    else
    {
        throw new Exception("No user found");
    }
}

// Good example
public void ProcessUser(User user)
{
    if (user is null)
        throw new Exception("No user found");

    if (!user.HasSubscription)
        throw new Exception("User needs a subscription");

    if (user.Age >= 18)
    {
        ShowFullVersion();
        return;
    }

    ShowChildVersion();
}

Technique 4: DRY Principle (Don't Repeat Yourself)

The DRY principle suggests that you should not duplicate code unnecessarily. Reuse functions and methods whenever possible.

This not only reduces the amount of code but also makes maintenance and updates easier.

// Bad example
public decimal CalculateCustomerDiscount(decimal amount, decimal discount)
{
    return amount - (amount * (discount / 100));
}

public decimal CalculateProductDiscount(decimal amount, decimal discount)
{
    return amount - (amount * (discount / 100));
}

// Good example
public decimal CalculateDiscount(decimal amount, decimal discount)
{
    return amount - (amount * (discount / 100));
}

public decimal CalculateCustomerDiscount(decimal amount, decimal discount)
{
    return CalculateDiscount(amount, discount);
}

public decimal CalculateProductDiscount(decimal amount, decimal discount)
{
    return CalculateDiscount(amount, discount);
}

Technique 5: Continuous Refactoring

Regularly refactoring code is an important practice to keep it clean and efficient.

Continuously review and improve the code, fixing problems and simplifying the structure as necessary.

Refactoring helps prevent code degradation over time, ensuring it remains understandable and easy to modify. Incorporating refactoring as a continuous activity in your workflow can prevent the accumulation of “technical debt”.

Conclusion

Applying these Clean Code techniques can significantly reduce the time spent reading and understanding code.

Adopting these practices in your daily routine will make your code more readable, easier to maintain, and improve your productivity.

Invest time in writing clean code and reap the long-term benefits.

Which Clean Code techniques do you already use? Share your tips and experiences in the comments below!

See ya!