§2024-12-02
In C++, naming conventions are crucial for maintaining readable, maintainable, and consistent code. While different organizations or projects may have specific rules, the following guide provides a general approach to naming conventions for variables, functions, classes, and more in C++.
-
General Principles
Consistency: Follow a consistent naming style throughout the codebase. Descriptiveness: Choose names that clearly describe the purpose of the variable, function, or class. Clarity: Avoid unnecessary abbreviations; use names that are easy to understand. Avoid Confusion: Do not name variables similarly to standard library functions, classes, or common data types (e.g., int, float, vector).
-
Variables
- Local Variables:
- starting with a lowerCamelCase for local variables (starting with a lowercase letter).
int userAge;
double totalAmount;
``
- Member Variables( a variable that is associated with a specific object):\
Prefix member variables with an underscore (_) or m_ to differentiate them from local variables.
class Person { private: int _age; // or int m_age; public: void setAge(int age) { _age = age; // or m_age } };
- Global Variables:
Use all lowercase letters with underscores between words (snake_case). In larger projects, it’s also common to prefix global variables with the project or module name (e.g., g_).
int g_windowWidth; int total_count;
3. Functions
- Function Names:
- Use lowerCamelCase for function names (starting with a lowercase letter).
void calculateTotal(); int getAge();
- Const Functions:
- If the function does not modify any member variables, consider appending const at the end of the function name.
int getAge() const;
4. Classes and Structs
- Class Names:
- Use UpperCamelCase (Pascal case), where each word starts with a capital letter.
class Person {}; class StudentRecord {};
- Struct Names:
- Similar to classes, use UpperCamelCase, but in some conventions, structs may be prefixed with S_ to differentiate them from classe.
struct Book {};
struct S_Employee {};
5. Constants
Constant Variables:
Use UPPER_SNAKE_CASE for constants and macros. If using constexpr or const, follow the same naming style.
const int MAX_BUFFER_SIZE = 1024;
constexpr double PI = 3.14159;
6. Enumerations
Enum Names:
Use UpperCamelCase for enum names. Enum values can be in UPPER_SNAKE_CASE.
enum class Color {
RED,
GREEN,
BLUE
};
7. Typedefs / Aliases
Type Aliases:
Use UpperCamelCase for type aliases.
using IntVector = std::vector<int>;
using StringList = std::list<std::string>;
8. Templates
Template Parameters:
Template parameters should be descriptive, typically using UpperCamelCase.
template <typename T>
class Box {
private:
T value;
public:
void setValue(T v) { value = v; }
};
9. Macros
Macros:
Use UPPER_SNAKE_CASE for macro names, and avoid long, complex names.
#define MAX_BUFFER_SIZE 1024
10. Namespaces
Namespace Names:
Use lowerCamelCase or UpperCamelCase, but typically avoid using underscores.
namespace utilities {
int calculate();
}
namespace Graphics {
void draw();
}
11. File Names
Source Files:
Use lowercase with underscores separating words (snake_case), or UpperCamelCase depending on your project.
main.cpp
person_details.cpp
Header Files:
Typically, UpperCamelCase with .h or .hpp extension for headers.
Person.h
StudentRecord.hpp
12. Avoiding Reserved Names
Keywords:
Do not use C++ keywords like int, float, class, namespace, etc., as variable or function names.
Standard Library Names:
Avoid using names that are too similar to standard library classes, functions, or types. For example, don’t name a variable vector or map to avoid conflicts with the std::vector or std::map class.
Example Summary:
// File: person.h
class Person {
private:
int _age; // Member variable
std::string name; // Member variable
public:
// Constructor
Person(int age, std::string name) : _age(age), name(name) {}
// Getter function
int getAge() const {
return _age;
}
// Setter function
void setAge(int age) {
_age = age;
}
// Print function
void printInfo() const {
std::cout << "Name: " << name << ", Age: " << _age << std::endl;
}
};
// File: main.cpp
int main() {
Person john(30, "John Doe");
john.printInfo();
return 0;
}
By adhering to these conventions, your C++ code will be more organized, maintainable, and easier for others (or even yourself) to understand in the future.