Skip to main content

Command Palette

Search for a command to run...

The Difference Between Good Code and Clean Code.

by: Farah Belhamiti.

Updated
2 min read

In programming, coders usually focus on writing code that doesn’t have errors and produces the correct output more than writing clean code, so what is the difference between writing code that works and writing clean code?

A good code is code that does its job correctly and gives the correct output; from a machine’s perspective, that’s enough; it doesn’t matter if the code is messy or hard to read, it only cares about the output.

However, clean code is also code that does its job correctly, but it’s well-organized and written in a way that if other people read that code, they can understand what it does and can modify it easily.

So, the key difference between good code and clean code is that good code focuses on machines, and clean code focuses on humans. For example, using short but confusing variable names vs clear names is a small detail but makes a huge difference in understanding the code, and another example is writing one big function vs small, structured functions; both of them will work, but one is easier for teamwork and debugging, while the other just does the job.

Another example:

Bad but good code:

int s(int a[], int n){

    int i, t=0;

    for(i=0;i<n;i++) t+=a[i];

    return t;

}

Clean version:

int calculateSum(int numbers[], int size){

    int sum = 0;

    for(int i = 0; i < size; i++){

        sum += numbers[i];

    }

    return sum;}

Both work, but one is much easier to understand.

In conclusion, writing code that works is important, but writing code that is clear and easy to understand is just as essential. Good code solves problems, while clean code makes those solutions accessible to others and your future self. A great programmer always aims to achieve both.

More from this blog

H

Hack United Blog

118 posts

Hack United is a non-profit organization founded by teenagers with a passion for programming and technology. On this blog, we spread recent tech news with a target audience of teenagers.