The Ultimate Guide to Clean Code

Rehan Pinjari
5 min readJul 20, 2024

--

The Ultimate Guide to Clean Code
Disclaimer: This post was originally published on Feb 1, 2023.
Disclaimer: This post was originally published on Feb 1, 2023.

Have you created any lines of control? Is the code clean? Can your code be understood by others?

Well, developing clean code does not simply mean producing code that works.

It is about how easy the code is to read and understand.

As developers, we spend a lot of time reading and understanding code.

To correct the error, add a feature, or deep dive into some code written by our colleagues, we need to understand the code.

It should be simple, since if it is hard, we will waste a lot of time and, thus, productivity.

Contact me visual
Have a BIG IDEA in mind? Let’s discuss what we can gain together.

Let’s talk → Gmail | LinkedIn

So, what exactly is clean code? Code that is:

  • Meaningful and readable
  • Simple to grasp and to the point
  • Avoid confusing names, complicated branching, and large code blocks.
  • conform to best practices and patterns in software development.
  • simple to keep up

Let’s look at some code examples below.

clean code example

Can you quickly understand what the code snippets are doing?

If it’s complicated, it’s not clean code.

Is it comparable with “Clean Structure”? It’s a different story.

In Clean Code, we focused on how to write code for a specific problem or file.

On Clean Architecture, we focused on where to write code across the whole project structure.

Selecting the most appropriate names for variables, functions, and classes.

Why is naming so important? Check out the code sample below.

Names for variables, functions, and classes for javascript

What specifically is the code doing? What exactly is “as”? What exactly is an “entity”? What exactly is “validate”?

Is it a string or a boolean? We need extra details to understand this bit; maybe a deeper look into the method and class to see how it works.

Now, look at the code sample below.

Example 02: names for variables, functions, and classes

That is the same code snippet, but it is much clearer.

We can see what the codes are doing without going into their functions and classes.

We can assume we are creating a user and saving it to a database.

And “isValidated” has a boolean value that checks if the user has already validated or not.

A meaningful name is important for improving code readability.

Variables, functions, and classes that are correctly named help readers understand the code without going too far into it.

What is the right method for naming “things”?

Clean code method for naming

Examples of Variable Naming

Examples of Function Naming

Examples of Class Naming

Avoid slang, confusing abbreviations, and misinformation

Slang may seem cool and exciting to some people or your team.

However, using such a language for naming objects will not help us achieve clean code.

When new project members try to deep dive, they may not understand what these approaches are gaining.

Don’t:

Do:

Confused abbreviations will also make our code difficult to understand and cause confusion.

Don’t:

Do:

Disinformation by name will confuse readers.

To confirm the information, we must check the usage of the “things” or do a deep dive into the ways.

Don’t:

Do:

Maintain Consistency

Methods can be named using verb phrases.

However, many verbs have similar meanings. When naming things, we must be consistent.

It will generate similar patterns and increase the readability of our code.

As an example:

The method name is suitable and describes the same thing.

It’s simple to understand, but you must maintain consistency throughout your project.

For example, if you want to use the term “get,” do the following:

Final Words

Variables, functions, and methods having clear labels help readers understand what the scripts do without exploring too far.

The use of good names will improve our code’s readability and make it easier to maintain, which is the goal of using Clean Code.

Don’t forget to clap and follow.

If you like this, please think about buying me a coffee! ☕️.

--

--

Rehan Pinjari
Rehan Pinjari

Written by Rehan Pinjari

Self-Taught Developer & UI/UX Designer

Responses (2)