DEV Community

Cover image for Common Mistakes in JavaScript Naming Conventions and How to Avoid Them
Rajat Patel for HyScaler

Posted on

Common Mistakes in JavaScript Naming Conventions and How to Avoid Them

When it comes to writing clean and maintainable JavaScript code, proper naming conventions play a crucial role. However, it's common for developers to make mistakes in this area, leading to confusion and potential bugs down the line.

In this article, we will explore some of the most common mistakes in JavaScript naming conventions and provide tips on how to avoid them.

Using Unclear Variable Names

One of the most frequent mistakes developers make is using vague or ambiguous names for their variables.

For instance, using single-letter variable names like "x" or "y" may seem convenient at first, but it can make the code difficult to understand for others (or even for the developers themselves after some time).

Instead, opt for descriptive names that accurately convey the purpose of the variable, such as "userAge" or "isUserLoggedIn".

Overusing Abbreviations

While abbreviations can save typing time, they often sacrifice clarity. Overusing abbreviations in variable names can make the codebase harder to comprehend.

For example, using "usrCnt" instead of "userCount" may seem efficient, but it could lead to confusion for anyone else reading the code.

It's essential to strike a balance between brevity and clarity in naming variables.

Ignoring CamelCase or snake_case Conventions

JavaScript developers are familiar with the camelCase convention for naming variables, functions, and objects.

While it's a widely accepted practice, some developers overlook this convention, leading to inconsistencies in the codebase.

Similarly, disregarding snake_case for naming constants can also create confusion. Adhering to these conventions ensures uniformity and makes the code more readable for everyone involved.

Misusing Capitalization

Another common mistake is misusing capitalization in variable names. For instance, using "userName" in one instance and "username" in another within the same codebase can lead to inconsistencies.

Consistent capitalization helps maintain code coherence and aids in comprehension.

Failing to Use Meaningful Function Names

Functions are the building blocks of JavaScript applications, and giving them clear and explicit names is crucial.

Naming functions based on their actions or purpose makes the code self-explanatory.

Avoid generic names like "doSomething" and instead opt for descriptive names that convey the function's specific behavior or role.

Neglecting Proper Naming for Classes and Constructors

In JavaScript, classes and constructors play a significant role in object-oriented programming.

Failing to use proper naming for classes and constructors can lead to confusion and hinder the readability of the code.

Make sure to use descriptive and meaningful names for classes and constructors to enhance code clarity.

Conclusion

Clear and consistent naming conventions not only benefit the developers but also streamline collaboration and understanding among team members.

Keep these tips in mind to write cleaner and more understandable JavaScript code.

Remember, the devil is in the details, and paying attention to naming conventions can make a world of difference in the quality of your JavaScript code.

Top comments (6)

Collapse
 
seandinan profile image
Sean Dinan • Edited

Fully agree! I think the only exceptions I normally make for abbreviations is e for event, str for string, and val for value since they're common enough to not cause confusion. And req/res in server-side stuff. Also for some inline functions I'll use the first letter of a variable:

const authors = books.map(b => b.author);

// Slightly more concise than:
const authors = books.map(({ author }) => author);
Enter fullscreen mode Exit fullscreen mode

Sometimes 3rd party APIs will return results in snake_case and I'll always pass them through a utility function to convert them to camelCase for the sake of consistency.

Collapse
 
corners2wall profile image
Corners 2 Wall

For boolean please use next prefix is or has
Example:
isVisible, hasBorder and so on

your function or method must be start with verb
Example:
checkPermission(), getName() and so on

Don't duplicate type into variable:
Mistake:
const numbersArray: number[] = [1,2,3]
Not bad:
const numbers: number[] = [1,2,3];

Read and write more code for practice.

Collapse
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop

It's so hard to find a good name 😅. There is this famous quote: "There are only two hard things in Computer Science: cache invalidation and naming things."

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

You forgot 'off-by-one errors'

Collapse
 
_ndeyefatoudiop profile image
Ndeye Fatou Diop

Ah ah definitely the worse especially when it comes to coding interviews

Collapse
 
a_a_hajji profile image
Ahmed abdirahman • Edited

print("hello world!");