Clean Code #1 Choosing Names

Rahaf
3 min readMar 30, 2021

if you spend extra time to make sure the written code is clean.You saved a lot.

1.Follow the language naming conventions

in each programming language community there are certain rules that preferred to follow .The consequences of following these rules will lead to increasing both software readability and maintainability .example of this rules is using underscore in python 🐍 and camelCase in java.These are some related links and the same concept applied to the rest languages just GOOGLE IT.

Python

Java

2.Use Intention-Revealing Names

your variable,function ,class,etc name should answer the following three questions :

  • why it exists?
  • what it does?
  • how it use?

Avoid writing comments to explain the name by choosing self explanatory names

int y ; // years in service

int yearsInService;

3.Avoid Misleading Names

Wrong information may be passed on by some mistakes in naming some examples are:

choosing name users or userGroup is better than usersList if the data structure for the variable is not a list .In addition to choosing O upper case letter lower case l because it could be read as 0,1 numbers. Lastly try to make the difference between the names of the variables clear (more than three letters, for example) in order to avoid that the other developer chooses the name of the wrong variable, especially with the development of the intelligence of the IDE’s and automatic completion.

4.Make Meaningful Distinctions

Never misspelled a name nor add number series or noisy words just to satisfy the compiler (sometimes it happen if the two things are in the same scope)

source , destination is better naming arguments for the copy list function than a1,a2 .Do not confuse the other developer about the differences between the names eg getAccountsInfo() and getAccountsData() and getAccounts().

Noisy words example customerObject there is no need for object word (noisy word )nor table word when naming tables.

5.Use Pronounceable Names

compare creationTimestamp with creymdms (cre short for creation,y short for year, m short for month, d short for day ,m for minute and s for second ) we always prefer what is easier to pronounce .

6.Be Professional Not Smart nor Funny Programmer

Clarity is the most important factor when writing the code from a professional programmer perspective .A smart programmer can notice that r variable is lower case version of url (without host and scheme)but that does n’t mean r is a good variable name .Try to avoid slangs and jokes in your variables names.

6.Apply These Rules

Class name ->noun(eg Customer)

Method name->verb (eg deletePage)

Overloaded constructor ->use static factory method with names describing the arguments and try to enforce that by making the corresponding construction private.

Example from clean code book

Complex flurcumPoint = Complex.FromRealNumber(2.0); is better than

Complex flurcumPoint = new Complex(2.0);

change names if you find better ones

7. Be standardise

Pick one phrase by function and make sure to standardise it in the whole code

eg choose one of these (get…,retrieve….,fetch…..)or (…..Controller,….Manager,…..Driver).

but if there is some difference by the function or concept you can change the phrase to be clearer eg all the adds methods in the classes do the following (concatenating tow values then add new value from the result of the concatenation)and you would like to append the passed value to the collection it is preferable to call the method append than add.

8. Solution Domain Names VS Problem Domain Names

It is better to start thinking of a name the comply to the Solution Domain so other programmers will be familiar with it by default(that has pattern names, algorithms terms or any CS terms) in this way you don’t need to ask the customer or the domain expert to explain if you don’t find the appropriate ones you can use Problem Domain Names.

Finally

if you are excited to learn more check Clean Code by Robert C.Martin

--

--