Clean Code Use Good Names To Tell Beautiful Story

AnyOneCanCode
12 min readAug 21, 2020

--

This blog is beneficial only for those that aspire to be a good programmer and ready to do hard work as it is not an easy way. Many just write code and makes it work. is the working code is the end goal?

SpongeBob Know the Value of Clean Code

Good programmer considers their work as an art and recognizes themselves as an artist. They think a lot while writing. They write programs, not for self but others their code convey the beautiful story(solution). Other programmers love to work with a good programmer as it is easy to collaborate. Their work makes reviewer happy. They inspire people around them by the way of their art.
Bad programmer is just happy by quickly writing a bunch of lines code & making it work. They are always in a hurry to finish there work and want to complete it by anyway. it doesn’t matter what or how they have written as long as code is working. Whoever works on their code will get pain in the head to understand things. They take their work as a mere job and feel a burden to think about clean code.

Just a photo of someone that reviewed bad programmer code(Mr. Bean is Legend)

As many factors matter for writing clean code and it required hard work, experience to get the mindset.
I am writing about the most important factor Naming that anyone can implement from newbie to professional. it is easy to use but it significantly makes codes clean.

ready to clean

First Let’s Understand Why Naming is Important -

Don’t write for self but for others, you are an author -

Let’s try to understand this statement by example.

ex. I am writing this blog so I become an author. so it is a responsibility of mine to try making blog as much expressive as it can be. I can express it by words, pictures, weird examples, etc But the end goal is to whoever read it just get the information very easily.

ex. I remember a fight in my college class where one student is arguing with the teacher that he writes the same thing as other student but didn’t get an equal mark.

Teacher & Student Arguing

So the teacher give him an answer sheet and told him to compare. By the first glance look of the sheet, he realizes that the other student writing is so good that by seeing it give a warm heal to eyes.
so even the answer is the same that doesn’t mean he is able to convey the solution efficiently.
In this story, I have nothing to argue because I wrote no answer to that question :) so better to watch!.

So try to relate the examples with the programming world it is no different. when you are writing a program you become an author. so it is a responsibility to make it understandable to others .when someone will go through code they should say wow so easy to understand. neat clean code that makes the eye feel good.
Let’s take another example.

for(int i=0;i<10;i++) {
boolean bool = true;
for(int j=0;j<9;j++) {
if(grid[i][j] != grid[i][j+1]) {
bool = false;
break;
}
}
if(bool) {
return grid[i][i];
}
}

can you tell what this piece of code is doing at first glance? what kind of problem it is trying to solve? what is the intention behind writing the code? where it can be used in the program?
it is just a bad programmer art that completed his work quickly, makes the code work & ready to go home happy.

Let’s see good programmer work.

for(int ver=0;ver<VERTICAL_SIZE;ver++) {
boolean isRowEqual = true;
for(int hori=0;hori<HORIZONTAL_SIZE — 1;hori++) {
if(BoardCell[ver][hori] != BoardCell[ver][hori+1]) {
isRowEqual = false;
break;
}
}
if(isRowEqual) {
return grid[ver][ver];
}
}

Try to compare both codes. using simple variable names it becomes more informative. first, we have a BoardCell so the author talking about 2d Board, For loop is iterating over vertical size than horizontal size, isRowEqual flag told us we are looking for an equalRowHorizontally.

int days;
int month;
int year;

compare to

int daysSinceUpdated;
int monthsSinceUpdated;
int yearsSinceUpdated;

it signifies variables have the information about updating time.

void copyValue(char[] c1 , char[] c2) {
for(int i=0;i<c1.length;i++) {
c2[i] = c1[i];
}
}

compare to

void copyString(char[] src , char[] dest) {
for(int i=0;i<src.length;i++) {
dest[i] = src[i];
}
}

it becomes more informative.it shows which parameter is source & target.so another person using your code will not get confused.

class Student {
int value1;
int value2;
String value3;
String value4;
}

compare to

class Student {
int id;
int enrollmentId;
String name;
String fatherName;
}

Don’t use variable like s1,s2,value1,value2………….
it is dis-informative.

getAccountInfo()
getAccountData()

see these two method declaration isn’t it convey the same meaning. how the other person reading program can know what is different in these methods.

see these short trick variable example

Date creymdhms;
Date modymdhms;

compare to

Date createdTimestamp;
Date modificationTimestamp;

Naming is very important and one of the basic steps to make code clean.
even if you are super smart and make a super optimal solution but if your code is not understandable to others, they faced difficulty to change even the value of the variable it is bad quality code.
Programming is not only about writing code and making it work. program is a self-explanatory solution.

It starts with Poor names in the project it becomes a big mess later -

Many project are hard to maintain due to poor names.
it started with poor names. suppose a new project is started all project members are happy by making code work and they pushed a lot of insignificant name in the project. later after a year team gets change. the new team is working on the project and when they see the project it is like this.

big mess

it will be very hard for them to figure out even a simple thing. the program is not self-explanatory and it will be very hard to maintain.
Many companies have faced these issues for years. many expensive projects get shut down because programmers haven’t used the basic concept of clean code.

Save time use good names -

it takes time to think about the name that is informative, explanatory, and describes the program. it takes to hit and trials also to write a good name but it saves a lot of time later.

let’s see this example where it is setting a color of head, para, tagline, background & footer, etc. the first problem with this program is that it is using hardcoded value to set the parameter e.g Color.blue. it is hard to maintain code. second, it creates code duplication as if you are using paragraphs in multiple places. For setting the simple color value of the paragraph it will be hard to locate the line in a program.

header.setColor(Color.BLUE);
paragraph.setColor(Color.BLACK);
tagline.setColor(Color.BLACK);
background.setColor(Color.WHITE);
footer.setColor(Color.red);

now if the program is written like this all the colors are defined globally with the name that is self-explanatory. it will be very easy to change color by just changing the variable name it can save a lot of time & thus easy to maintain.

COLOR headerColor = Color.BLUE;
COLOR paragraphColor = Color.BLACK;
COLOR taglineColor = Color.BLACK;
COLOR backgroundColor = Color.WHITE;
COLOR footerColor = Color.red;
header.setColor(headerColor);
paragraph.setColor(paragraphColor);
tagline.setColor(taglineColor);
background.setColor(backgroundColor);
footer.setColor(footerColor);

just by giving more effort while working on code later, it can become an easy maintain code that everyone likes & loves to work with.

Basic Technique for Better Code-

Use Intention Revealing Names -

Use names that reveal clear intention. Using meaningful simple names would make your code way better looking.

int d;

compare

int daysSinceUpdated;

you are naming either class, subclass, function, variable, enum anything always use names that clarify purpose just by looking.

Avoid using names that give incomplete information-

Name like arr, value, data, info, matrix, grid, etc. these are variables just coming randomly in mind and it gives incomplete information.

Data data;

it only talks about data not about what kind of data it is related to.

Data personData;

it gives information that data is related to a person.

similarly

int[] arr;

it only shows incomplete information that it is an int array.

int[] personId;

it is complete information that it is an array of personId;

Use Meaningful Distinct Names -

Use names that are distinct from one another. avoid using names that are similar and make confusion.

getPerson();
getPersons():
getPersonData();
getPersonInfo();

can you guess what is the distinction in the above methods?
what data & info have different?

Avoid Using inconsistency in naming-

always use names in a consistent way.
ex. suppose you are using the prefix ‘get’ in every method that returns something so don’t break that consistency with other names.
ex. you are using a capital letter for every global variable so don’t make any global thing in small letters.

URL getUrl();URL fetchUrl():

suppose one place you are using get & another fetch. but both are doing the same thing so it breaks consistency.
Making code consistent in names is good practice and it helps any new programmer to quickly understand the code, makes code easy to maintain.
Always make codebase that is using the name in a consistent manner. any newbie is working can know what is the name that needs to be used.

Always use pronounceable names -

you are going to look stupid if using a name that is not pronounceable imagine a situation where you are discussing code. my swwwp is not printing value or something is wrong in asffewa. my class xyxsdsds just don’t work.use the variable that is easy to do in conversation.hey man, the person class is not working. some issue is there with lastUpdated variable.
Don’t be zombie agghhhaggraaahaaaaaaaaaaaaaaaaaaaaaaaa!

i love zombie movies. nobody knows their language so nothing to explain.

Use Searchable Names -

think words smartly use names that are searchable and can relate to the purpose. imagine there is thousands of line of code and someone wants to search for student record. if you have smartly use the keyword by just quickly searching student record.he can get all the methods, data related to that keyword. it will pretty easy for anyone to get what they want. if we have used good searchable names and can relate to the purpose.

Don’t give information about datatype in names-

try to don’t use datatype with the name.

String nameString;

name will always be of string type so why String.

Person personObject;

Object can be confused with the data type.
when you are defining variable it already gives the name of datatype.do not try to include data type information in a variable name.

Avoid Mental Mapping -

Avoid using variable i,j,k, etc for big scope. if the block is very small and scope is less than the single-letter variable is good. but using in bigger code will be hard to understand. anyone needs to mentally map these variables, that doesn’t make sense.
you can be super smart for your own code and good at mental mapping. don’t give that stress to others. develop code in which anyone doesn’t face a problem in reading, remembering variable.

Class is Noun & Method is Verb -

it is a very basic thing. never make a class with dogEating, dogBarking, or method with a name dog. use nouns for defining class and verb for method.

Don’t use humor in code -

Maybe you are dreaming to become a comedian and accidentally came to this field. Maybe you have good humor and wherever you go people love your humor but please don’t use humor in code. everyone around using code doesn’t have your level of humor or understanding.

Reviewer after getting humor in code

ex. don’t use getLost for delete, mySlaves for calling thread , justGetThePersonDataBuddy for getPersonData, giveMeThePower for fetchUrl , losingMyVerginity for freeResource etc.
Don’t use humorous words it is a serious work. if you do that then you are stupid. imagine a painter doing painting and writes a funny joke on it. it is just poor art.

Pick one word per concept and avoid using words that give multi-purposes -

Stick with one word per concept role. using get, retrieve & fetch for the same purpose is the poor way. imaging about the situation where all three are used and doing the same thing it is confusing. it is simple to use one word for one concept. get for getting data, retrieve to get something from other servers, or fetch for only for getting URL.
Don’t use words that give many purposes like insert maybe you are using in method name to add something and also on database script. so using the same word for many purposes is a bad way.

Use common names that are used by technical people -

Remember code is not made clean to read by people that don’t understand them.they can be your client or someone else.it always gets used by other programmers so using common technical names of pattern, algorithm, math equations name, etc is a good idea. think about the name like a queue, stack used in code. it is easy to understand.used common names that are used for years by technical people.

Use Problem & Solution Oriented Names -

Name that can relate to the problem and solution are good to use.
it can be simple like finding a student has taken subject dancing as an optional subject.

findStudentChooseDancing()

it clearly related to the problem and simply gives an explanation of what it is doing in the method.
Using a name that can relate to the problem that is tried to be solved or from the solution is easy to understand.

Use Meaningful Context -

Always use meaningful names and enclose them in meaningful classes, method, structure, etc. it gives meaning to the whole codebase and makes it clean code.

String streetAddress;
String blockAddress;
String cityAddress;

it signifies that street, block, city it belongs to the address but it is poor naming it can be enclosed in well name class.

class Address {
String street;
String block;
String city;
}

The class itself signifies it is an address.

Use a good name for the class, method, variable, etc, and together make them a meaningful code. the whole structure should be a well-defined name and everything telling their own story together making a beautiful story.

Final Words -

It looks simple to use the name but it requires practice to think descriptive names. Many newbie think it is not important but in the professional world, it matters a lot.
it is amazing how you can just make code clean by just using appropriate names.it is the most simple clean code factor that anyone can use.
Asked the professional people with experience what they get in the feedback in their first code review(PR) during the start of their career? most of them will say it is always the naming that they are using in a codebase.
Making code work is the least programmers can expect. You can be good at logic building and always make program work. but if that program doesn’t have a self-descriptive name it will be hard for anyone to recognize the work.
If you want to be a good programmer and liked by around programmer. always use names that make them happy.
Remember you are an author of code. so make a writing piece a story that tells how you reached to the beautiful solution.

Reference -

These all the above techniques are not mine they are used for decades by programmers and going to be used for decades. Read any clean book about clean there always will be these simple techniques that anyone can easily utilize.
I strongly recommend the book Clean Code By Robert C. Martin .

Clean Code

Thank you very much for your time,
Naman Sisodia

Are you on Social Media? Let’s connect you will find me on Instagram or Linkedin.

--

--

No responses yet