Naming Variables, Functions, And Classes Can Benefit Clean Code
Good name usage will boost our code readability and make it easier to maintain.
What exactly is the Clean Code?
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.
So, what exactly is clean code? This is a code that means:
- 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.
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.
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.
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”?
Examples of Variable Naming
Examples of Function or Method 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.
Forgo:
Do:
Confused abbreviations will also make our code difficult to understand and cause confusion.
Forgo:
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.
Forgo:
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.