ASIF_-_ASHIK's blog

By ASIF_-_ASHIK, history, 11 months ago, In English

Introduction

The compiler doesn't require naming conventions. However, naming conventions are required to increase code readability. When I was a beginner, I often used meaningless variable names, which made it difficult for me to understand my code when revisiting it after a few months. The lack of readability in my code was a significant issue. Following a naming convention rule enhances code readability and helps in debugging. When sharing code with teammates or mentors, following a naming convention allows them to easily understand your code.

Some Common Naming Convention Rules

here I am adding some common naming convention rules:

Camel Case: This convention is used for variable names. It starts with a lowercase letter and each subsequent word in the name begins with a capital letter without any spaces.
Example: myVariableName, firstName, totalAmount.

Pascal Case: This convention, also known as UpperCamelCase, is similar to Camel Case but starts with an uppercase letter. It is typically used for naming classes and sometimes methods.
Example: MyClassName, CustomerDetails, CalculateTotal.

Snake Case: This convention involves connecting words with underscores and using all lowercase letters. It's commonly used for naming variables, constants, and sometimes file names.
Example: user_name, total_amount, max_iterations.

Kebab Case: In the Kebab Case, words are separated by dashes ("-"). It's often used in contexts like CSS classes, URLs, and command-line arguments.
Example: page-header, text-align, background-color.

Hungarian Notation: Hungarian Notation prefixes identifiers with a group of characters indicating the type of the variable, followed by a descriptive name.
For example, "strFirstName" might indicate that the variable is a string holding the first name.

Uppercase Convention: In programming, the convention of using uppercase letters for naming constants is known as "Uppercase." In this convention, constant names consist of words that are separated by underscores, creating clear and distinct identifiers. The letters are in uppercase letters.
example: PI_VALUE

Each of these conventions aims to improve code readability, maintainability, and understandability by providing consistent and descriptive names for variables, classes, methods, and other identifiers. Choosing the appropriate naming convention depends on the programming language, the project's coding standards, and the context in which the code will be used. Now I am adding some naming convention examples for variable names, method names and class names, and constants.

Common Variable Name Rules

Variable names are identifiers used to represent data storage locations in a program. A variable name should be descriptive, concise, and meaningful, reflecting the purpose or content of the data it holds.

Camel Case: In many programming languages, including C++, Java, and Python, variable names typically follow the Camel Case convention. In Camel Case, variable names start with a lowercase letter, and subsequent words are capitalized.
Example:

int myVariable = 30; //C++

UPD: Python does NOT typically use camel case for variable names. Instead, snake case is advised, Reference:PEP8 — Style Guide for Python Code. . Thank you gardengnome for this update.

Snake Case: In languages like Python, variables can also use Snake Case, where words are separated by underscores.
Example:

my_variable = 30  #python

Snake Case is popular in C++ as well. Google C++ Style Guide does not use camelCase at all (ordinary variables are named with snake_case), and this policy is propagated to their open-source libraries (like Abseil, Protobuf, GoogleTest etc.). Thank you Papercut for the information about Snake Case.

Common Function/Method Name Rules

A function name is an identifier that represents a block of code designed to perform a specific task or operation within a program. Function names should be descriptive and convey the purpose of the function.

Camel Case: In Camel Case, function names start with a lowercase letter, and subsequent words are capitalized. This convention makes function names easier to read and understand. Example:

int myCalculator(int a, int b) 
{
  //code here 
}

Here, "myCalculator" follows Camel Case where "my" is lowercase, and "Calculator" begins with an uppercase letter.

Snake Case: Although not as common for function names, Snake Case can be used in languages like Python.It is popular in C++ as well. In Snake Case, words are separated by underscores, and all letters are lowercase. Example:

def my_calculator(a, b):
    # code here

Class Name Rules

In programming, when naming classes, it's common to follow Pascal Case. This means that each word in the class name starts with a capital letter, and there are no underscores between words. This helps make class names clear and easy to distinguish from variables and functions in the code.

Pascal Case: Pascal Case is a naming convention used in various programming languages, including C++, Java, and C#. It dictates that identifiers, such as class names, start with an uppercase letter, and subsequent words within the identifier are also capitalized without any underscores or spaces between them. Example:

 class MyData {
    string name;
    int id;
};

Constant Name Rule

Constant names in programming are identifiers used to represent fixed values that do not change during the execution of a program. One common convention for naming constants is to use uppercase letters and separate words with underscores.

Uppercase: When using uppercase letters for constant names, developers ensure that constants stand out within the code and are easily recognizable as unchanging values. Constant names in uppercase with underscores provide a clear and consistent way to represent fixed values in programming. Example:

const double PI_VALUE = 3.1416;

Conclusion

While specific naming conventions may vary depending on the programming language and project requirements, the key principles of clarity, consistency, and meaningfulness remain constant. Therefore, understanding and applying appropriate naming conventions is an essential skill for every programmer striving to write clean, maintainable, and professional code.

  • Vote: I like it
  • +5
  • Vote: I do not like it

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Helpful blog vai

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Well, camelCase is not a common choice for naming ordinary variables in C++...

  • »
    »
    11 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thank you for your opinion. It would help me to improve this blog. i have searched and read a lots of articles online. in case of c++ variable name, method/function name, camelCase is used most.

    I am a human being, still i might do mistakes. if i am wrong you can correct me with proper references, i will correct this blog. Thank you .

    • »
      »
      »
      11 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      For example, Google C++ Style Guide does not use camelCase at all (ordinary variables are named with snake_case), and this policy is propagated to their open-source libraries (like Abseil, Protobuf, GoogleTest etc.).
      One of the most popular C++ libraries — Boost, avoids camelCase as well (I don't know whether their internal formatting rules are published or not, though, I speak just from my experience of working with this library).
      Basically, the one and only library with camelCase naming, which is known to me, is POCO.
      It is quite interesting to look at the opposite examples, though

      • »
        »
        »
        »
        11 months ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Thank you. I have updated this post and mentioned you as well.

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by ASIF_-_ASHIK (previous revision, new revision, compare).

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Yeah,I rarely end up understanding the authors implementation od a problem so this would be very useful.

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Python does NOT typically use camel case for variable names. Instead, snake case is advised, see PEP8 — Style Guide for Python Code.

  • »
    »
    11 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I used python as Snake Case example as snake case is used in python most. Thank you for your information, I have updated this post and mentioned you as well.