DEV Community

Cover image for Clean Code Part 1: Meaningful Names
Tanisha Sabherwal
Tanisha Sabherwal

Posted on • Edited on

Clean Code Part 1: Meaningful Names

Naming variables, functions, classes, arguments, and packages is an art not everyone understands. Those who understand, live a technical debt-free life. But few simple pointers can help you reach this goal. While most of these will sound obvious but are not completely implemented by a lot of developers. Read ahead to crack the code for writing meaningful names.

Reveal the Intent

Choosing good names takes time but saves more than it takes.

The names of variables, functions, classes should answer three broad questions:

  1. Why it exists
  2. What it does
  3. How it is used
var d;  //does not reveal anything

var daysSinceCreation; // clearly reveals what it stores
var isModalOpen; // depicts the variable is a boolean
const getUserID; // denotes a function or task
Enter fullscreen mode Exit fullscreen mode

Hence, focus on the simplicity of the code rather than simplicity. If a variable requires a comment, it does not reveals its intent well.

Avoid Disinformation

Avoid names which leave false clues which might obscure the meaning of code. Treat it as an honourable human, it should say what it means and mean what it says.

  • Don't name a group of accounts as AccountList unless it is a list.
  • Avoid two names with subtle differences like isAccountInformationOfUserRequired and isAccountInformationOfAdminRequired .

Make Meaningful Distinctions

Number-series naming like a1, a2, ...., an makes the code non-informative and does not reveals anything about the intention.

Another avoidable typing of naming is using noise words. Words like variable, NameString, Customer, does not reveal anything and so should be avoided.

Use Pronounceable and Searchable Names

Use words that are pronounceable so that others can use it for communication. This will enable better collaboration because "Programming is a Social Activity".

Imagine someone calling you to fix the bug and mentions using the function getmddh which gets month, date, day and hour for the post. Saying "get-M-D-D-H" is both frustrating and confusing, so rather just call it getPostTimeStamp.

Similarly names should be searchable. Single or double letter names are hard to search in a codebase. In this case Ctrl+Shift+F will no longer be at rescue and is such a bade case for giving meaningful names.

Defining Roles

Names should be such that it reveals the role of the variable, this specially is very important in languages like JavaScript everything is either a var, const or let.

  1. Class and Object names should be a noun or noun phrase like AddressParser, WikiPage, and CustomerDetails.
  2. Method names should be a verb or verb phase like deleteUserEntry, getUserDetails and savePostData.
  3. Booleans should have prefix like "is" to communicate binary or 0-1 behaviour.

Add Meaningful Context

Sometimes variable names require additional data to give context to generic variable names like firstName, LastName, city, zipcode etc. In such a case adding a prefix like addrfirstName and addrLastName will denote it being a part of a larger Address class.

Scope Length Rule

  1. Variables with shorter scope should have shorter name and vice-versa because ones with longer scope at used at multiple locations and should be descriptive.
  2. For classes/functions, the opposite is true because the longer the scope, easier should be the access for class/functions names. It should be short and crisp however for shorter scopes, longer and descriptive names can be helpful to reveal the intent.

Ending Notes

Quoting from the book,

Any fool can write code a computer can understand, but it takes a good programmer to write code that humans can understand.

Head over to Part 2 of the series.

Top comments (6)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Single letter variable names are very useful as loop counters, or in small utility functions where the functionality is obvious. They have their place and should not be discounted

Collapse
 
tanisha03 profile image
Tanisha Sabherwal

Yes, using single letter variables in loops is justifiable because its standardised but single or double letter names using in functions or variables which are spread across files should be avoided because of it cannot be searched easily. Even for utility functions, names should be clear enough to understand what the function does.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Sorry, but I disagree. This function gains nothing by 'meaningfully' naming the parameter:

const isItemRed = i => i.colour == 'red'
Enter fullscreen mode Exit fullscreen mode

I've been writing code for about 38 years - 27 of those professionally. Experience has taught me that the best code is succinct and clear. It really isn't necessary to give everything a meaningful name - especially when the context makes it perfectly obvious what things are, and what they do.

Overly verbose code is exhausting to read, and totally unnecessary

Thread Thread
 
tanisha03 profile image
Tanisha Sabherwal

Sorry, if I came across as unclear or rather misunderstood your point. But I totally agree to what you just mentioned. What I was referring to was the name of the function and not the parameters, in this case being isItemRed.

Collapse
 
codeitout_ profile image
Codeitout

Perfect tanisha :D

Collapse
 
tanisha03 profile image
Tanisha Sabherwal

I am glad you liked it :)