Code Smells: Naming

Code Smells: Naming

Today's article aims to discuss Naming in relation to good coding practices.

ยท

6 min read

This is part of a code smell series we will be looking at. Just so you know, I am no Elon Musk or Robert Martin; I am just sharing what I know ๐Ÿ˜Š

Code Smells

smell.jpg

The term "code smells" was invented by Kent Beck, an American Software Engineer and the creator of the famous Extreme Programming. A code smell is basically an indication that software code is of low quality and possesses problems. Codes devoid of smells are more readable, understandable, and easier to maintain.

Naming

A notable example of code smells is bad naming, hence using proper naming conventions while coding is essential. It primarily makes the written code appear meaningful to the author of the code and other readers.

In the early days of programming, languages such as BASIC and Fortran allowed variables to have a maximum of two and six characters respectively. Can you imagine? How can you make sense of variables that are named along the lines of a2 and k5? Nowadays, programming languages are more accommodating and this has made it easier for developers to write purposeful code.

Characteristics of Good Names

Good code names speak for themselves and are typically:

- Purposeful and meaningful:
Good code names reveal the programmer's intent and make sense to anyone who reads it. For example: String firstName โœ… String n โŒ

- Brief: Even though programming languages allow identifiers to have long names, it is advisable to keep names short and straight to the point. Brief code names are less tiring to read. Take a look at these 2 functions:

String getNameAndVersionDetailsOfDatabase(){
return "dummy data";
}      

String getDatabaseInfo(){
return "dummy data";
}

getNameAndVersionDetailsOfDatabase() looks a little verbose, don't you think so? Personally, I advise not having more than four words in a name if possible.

- Consistent:
Be wary of using naming conventions inconsistently. It usually results in confusion and makes code non-uniform. For instance, once you use uppercase names for constants, you should continue using this pattern throughout the code.

Naming Categories

- Snake case: This naming convention entails the use of underscores in names. They are sometimes used in naming variables but are usually appropriate for constant variables e.g. TOKEN_EXPIRY.

- Camel case: This involves starting an identifier's name with a lower case letter and making subsequent words begin with uppercase. e.g. userDetailsService. Camel cases are generally used when naming variables and functions.

- Pascal case: Pascal case is quite identical to camel case but only differs by the first letter being in uppercase. e.g. CarModel or UserController. Pascal cases are usually applied in naming classes.

Helpful Tips

  1. Class names should be nouns e.g.UserDetail or Shape
  2. Function names should be verbs because they perform actions e.g. println or getAge
  3. Booleans should have question-like names e.g. isEnabled or hasRegistered
  4. Avoid one-letter variables e.g. int a. Only use one-letter variables in loops e.g.
    for (int i = 0; i < 5; i++) {
    System.out.println(i);
    }
    

Below are the standard naming conventions for identifiers

IdentifierSuitable Case To UseExample
Packageslowercasecontroller
Classespascal caseUserProfile
Functionscamel casegetUsername
Variablescamel case or lowercaseuserEmail
Constantssnake caseAPP_NAME

In summary, good code names should ultimately be meaningful, brief, and consistent. Adhering to these conventions will in the long run make it easier to read and understand code. All programmers (both beginners and advanced) should endeavour to write clean code as it saves a lot of time and effort. Don't be a "pain in the b*" on your team.

I will catch you later with another post but for now, remember

"Any fool can write code that a computer can understand but Good programmers write code that humans can understand" - Martin Fowler

Drop a comment if I left out other naming conventions. Cheers!