Naming variables, functions and classes is one of the most important ways to allow readers of your code to understand it without having them go through it in detail. Following are a few rules to keep in mind when naming things in your code.

  1. Meaningful Names: Names should convey the purpose and content of the element without the need to dig deeper into the code. This helps readers understand your code at a glance.

    Example:

     // Meaningful variable names
     int userData;
     bool isValid;
    
     // Meaningful function names
     void sendData();
     bool inputIsValid();
    
     // Meaningful class names
     class User { /* ... */ }
     class RequestBody { /* ... */ }
    
  2. Variables & Constants:

    • Use nouns or short phrases with adjectives for variables and constants.

    • Choose names that reflect the data contained.

Example:

    // Variables and Constants
    string userName;
    int numberOfProducts;
    const double pi = 3.141592;
  1. Functions/Methods:

    • Use verbs or short phrases with adjectives for functions and methods.

    • Choose names that describe the operation or calculation performed.

    • Describe the operation or question answered by the function.

    • Provide additional details without introducing redundancy.

Example:

    // Function and Method Names
    void sendDataToServer();
    bool isInputValid();
    int calculateTotalPrice();
    User getUserById(int userId);
    bool isValidEmailAddress(string email);
  1. Classes:

    • Use nouns or short phrases with nouns for class names.

    • Describe the object or entity represented by the class.

    • Avoid redundant suffixes and unnecessary information.

Example:

    // Class Names
    class User { /* ... */ }
    class Product { /* ... */ }
    class Customer { /* ... */ }
    class RequestBody { /* ... */ }
  1. Name Casing:

    • snake_case, camelCase, PascalCase, and kebab-case are some of the options that you can use in your code. Be aware of any language specific conventions when using casing. E.g. snake_case is commonly used in python, camelCase is used in Java and C++ and PascalCase is used primarily for class names.
  2. Naming Variable, Constant, and Properties:

    • Choose names that reflect the nature of the value being stored.

    • Use names that describe the value's type.

Example:

    // Variable and Property Names
    User user;
    string name;
    bool isActive;
  1. Other Naming Rules:

    • Avoid redundancy, slang, unclear abbreviations, and disinformation in names.

    • Choose distinctive names that differentiate elements.

    • Be consistent in your naming conventions throughout your codebase.

  2. Avoid Bad Naming Practices:

    • variables with single letters - they don't tell you anything about the variable

    • never abbreviate

      • abbreviations rely on context that you may or may not have.
    • don't put types in your code. e.g. for Hungarian notation like uint3_t iSpeed, bool is valid

      • Everything used to be a standard type (int) in c. But now languages are statically typed
    • put units in your variable names. E.g. delaySeconds instead of delay.

      • Even better is to use the type that removes the ambiguity. E.g. chrono::duration Delay
    • Don't put types to types. E.g. IFile for interface files

      • we do not need to know if File class is an interface
    • Do not put the words "Base" or "Abstract" in the name of a class

      • hint: Rename child class if you cannot come up with a good name for a parent class.
    • Don't name code "Utils" or "Module"

      • sometimes these functions can be moved to the respective type classes or separate classes.