Naming Conventions: From Confusion to Clarity

Variables play a key role in logic, enabling software to perform tasks, manipulate data, and adapt to diverse scenarios. Yet, amidst the complexity of code, there exists a seemingly simple but profoundly impactful aspect often overlooked: naming conventions. This common practice encourages all developers to adhere to standard naming conventions, which play a crucial role in maintaining the readability, maintainability, and scalability of our codebases.

Before we dive into the universe of naming conventions, a joke! 🤡

Why did the programmer name his variable “notDefinedYet”?
He was stuck in an infinite loop of indecision.

Description and Meaning

Providing a variable with an appropriate name to describe its usage greatly aids in understanding its purpose. I bet you won’t be able to guess what the following code is calculating. 😉

let x = 10;
let y = 12;
let z = x * y;

Here is the version with more descriptive naming:

let subscriptionCost = 10;
let subscriptionDurationInMonths = 12;
let totalSubcriptionCost = subscriptionCost * subscriptionDurationInMonths;

This is just a simple example, but it can have a profound impact on larger codebases.

camelCase, snake_case, and More

wHy Is CaSiNg So ImPoRtAnT? It helps with readability and maintainability to choose a casing convention and adhere to it consistently. This ensures that you, as well as others, won’t have to spend time deciphering and interpreting it. Casing also helps to distinguish between different types, such as constants, variables, functions, and classes. While these conventions aren’t set in stone, they should serve as a guide to maintain consistency throughout your codebase.

  • For classes and functions, PascalCase is commonly used: UserController, GetUserNameById, etc.
  • For variables, camelCase is preferred: userName, listOfUsers, etc.
  • Constants are typically represented in UPPER_CASE: HOURS_IN_DAY, DATABASE_NAME, etc.
  • snake_case can sometimes be found in different languages’ naming conventions like Python and Ruby, or in configuration files, or filenames in some cases.

Naming your booleans

A practice I’ve always adhered to when naming booleans is to keep them positively phrased and in their simplest form. Let me explain: booleans are either true or false, so it’s easier to comprehend whether the boolean description is true rather than false.

if (!isNotReady) {
    console.log("is ready");
}

This code snippet essentially reads as “is not not ready”, employing a double negative. However, it’s much clearer when expressed as:

if (isReady) {
    console.log("is ready");
}

Please try to adhere to prefixes like is and has.

Avoid Reserved Words

Avoid using language keywords or reserved words when naming your variables, as this can lead to confusion and errors. class is a reserved word in Python and many other languages, and using it will result in invalid syntax. There are many other reserved words, so it’s best to be familiar with some of the most common ones when starting to learn a new language.

# Define a function that takes a class name as input and prints a message
def print_class_name(class):
    print("The class name is:", class)

# Attempt to use the function with a variable named "class"
class = "User"
print_class_name(class)

Avoid Abbreviations

Using variable naming abbreviations is generally considered a bad practice due to their negative impact on readability, maintainability, clarity, consistency, and scalability. Descriptive variable names are preferred. This might not be as obvious to some, but in strongly typed languages like C#, the variables already have assigned data types, making it redundant to specify an abbreviated type like strFirstName or numAge.

Use nouns for Variables

Naming variables after nouns makes code easier to understand and work with. It’s like giving each variable a name that tells you what it’s about, just like how we name things in everyday life. This helps everyone on the team follow along and work together more smoothly. Plus, it keeps everything neat and tidy, making the codebase feel more organised and friendly.

Avoid Magic Numbers

Instead of hardcoding numeric values directly into your code, assign them to variables with descriptive names. This enhances readability and makes maintenance easier. Imagine having to find and replace a specific number or value across your app; you might miss a few places where it is implemented. Depending on the size of your app, it would be better to defer variables like that to a more centralised place.

In coding, naming conventions are crucial. Clear and descriptive names make code easier to understand, maintain, and scale. By picking meaningful names, steering clear of shortcuts like magic numbers and abbreviations, and promoting a team culture around naming conventions, developers ensure smoother workflows and more user-friendly software.

Latest