Menu
Coddy logo textTech

Naming Conventions

Part of the Fundamentals section of Coddy's C++ journey — lesson 12 of 74.

In programming, it's important to follow naming conventions to keep your code readable and maintainable. However, there are some rules and conventions you should follow when naming things in C++:

  • Names can contain letters, digits, and underscores.
  • Names must begin with a letter or an underscore.
  • Names are case-sensitive (myVariable and myvariable are different).
  • Names cannot be C++ keywords (like int, float, if, etc.).

In addition to these rules, there are some common naming conventions that developers use to make their code more consistent and readable:

  • camelCase — words are joined together, with each word after the first starting with a capital letter (e.g., totalAmount, numberOfStudents). Commonly used for variable and function names in C++.
  • PascalCase — similar to camelCase, but the first word also starts with a capital letter (e.g., TotalAmount, MyClass). Commonly used for class names in C++.
  • snake_case — words are all lowercase and separated by underscores (e.g., total_amount, number_of_students). Often used in C++ for file names or in other languages like Python.
  • SCREAMING_SNAKE_CASE — like snake_case but all uppercase (e.g., MAX_SIZE, TOTAL_AMOUNT). Typically used for constants and macros in C++.
  • Be descriptive and avoid overly short names (e.g., numberOfStudents is better than n).
  • Avoid using single-letter variable names, except for simple counters (e.g., i, j, k).

Here are some examples of good and bad variable names:

// Good
int age = 30;
double totalPrice = 150.99;      // camelCase
int numberOfStudents = 25;       // camelCase
const int MAX_STUDENTS = 100;    // SCREAMING_SNAKE_CASE for constants

// Bad
int a = 30;  // Too short and unclear
double TOTAL_PRICE = 150.99;  // Uppercase is not for regular variables
int n = 25;  // Too short and unclear

Cheat sheet

C++ naming rules:

  • Names can contain letters, digits, and underscores
  • Names must begin with a letter or underscore
  • Names are case-sensitive (myVarmyvar)
  • Names cannot be C++ keywords (e.g., int, float)

Common naming conventions:

  • camelCase — variables and functions: totalAmount
  • PascalCase — class names: MyClass
  • snake_case — file names / other languages: total_amount
  • SCREAMING_SNAKE_CASE — constants and macros: MAX_SIZE
  • Be descriptive; avoid short or unclear names (prefer numberOfStudents over n)
  • Single-letter names only acceptable for simple counters (i, j, k)
int age = 30;                    // good
double totalPrice = 150.99;      // camelCase
const int MAX_STUDENTS = 100;    // SCREAMING_SNAKE_CASE

int a = 30;                      // bad: unclear
double TOTAL_PRICE = 150.99;     // bad: uppercase for non-constant

Try it yourself

This lesson doesn't include a code challenge.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals