Clean Code 101: Naming Conventions

Clean Code is a controversial book. When something reaches the popularity levels that book has reached, it goes from book to cult. BUT naming is one of the most overlooked, important topics of software development I think. Everyone should take so time to sit down and learn good naming conventions.

Variables, Objects, and Classes

Variables, classes, and objects describe things so their names should be a noun.

In %90 of cases the value will be an object, number, or string in which you should simply describe the value.

var user = "user";

When the value is a boolean, answer a true/false question (“isActive”).

var isActive = true;

Another easy tip, is to provide more details about the variable without introducing redundancy.

guestUser
userRepository

Other ways to describe this process is principle of least suprise. Basically, aim to write code that other people expect and try not to suprise. Also, try to use intention revealing names that describe what the code does in a non-redundant way.

Functions

Methods and functions perform actions in code so their names should be a verb phrase.

If function returns a boolean, pose function as a question.

function isValid() {
    //Code here
}

isValid()

“Meaningful Context” Explained

In Clean Code, there is a concept called meaningful context. Imagine you had words like “TSLA”, “PLTR”, “moon”. If you saw the word “moon” without those words, the meaning would entirely change. You can add context with prefixes like “stockMoon”.

Shorter names are generally better than longer ones and add no more context then needed. This is especially true in classes as prefixes are frowned upon.

Conclusion

While tempting to skip, naming conventions can have great impact on the code base. Let’s face it: naming conventions is low IQ but the easy things can often times have the biggest impact.

Leave a Reply

Your email address will not be published. Required fields are marked *