Coding Standards in C++

Coding standards are a set of guidelines and rules that help you write clean, readable, consistent, and maintainable code. Following good standards makes your code easier to understand for yourself (after months), for teachers, teammates, and future employers.

This topic covers two major parts of coding standards:

  1. Naming Conventions
  2. Comments

These are essential for all programmers — from Class 5–12 students starting their first programs to BS-level students working on large projects.

Naming Conventions

Naming conventions are rules for choosing meaningful and consistent names for variables, functions, classes, constants, etc.

Why Naming Conventions Matter

  • Good names make code self-documenting (you understand what it does without extra comments).
  • Poor names lead to confusion and bugs.
  • In team projects (school or job), everyone must follow the same style.

General Rules (For All Levels)

  1. Use meaningful names – Avoid single letters (except loop counters like i, j).
    • Bad: x, temp, data
    • Good: studentAge, totalMarks, fileName
  2. Be consistent – Choose one style and stick to it throughout the project.
  3. Avoid abbreviations unless very common (e.g., num for number, avg for average).
  4. Use nouns for variables, verbs for functions.
    • Variable: studentName, marksArray
    • Function: calculateAverage(), displayRecord()

Popular Naming Styles in C++

 
 
StyleExampleWhen to UseRecommended For
snake_casestudent_age, total_marksMost common in C++ school textbooks (India)Class 5–12 (Highly Recommended)
camelCasestudentAge, totalMarksVery popular in modern C++Class 11–12 & BS
PascalCaseStudentAge, TotalMarksUsed for class names, typesAll levels (for classes)
UPPER_SNAKE_CASEMAX_STUDENTS, PIConstants, macrosAll levels
 

Detailed Guidelines by Type

Variables

  • Use snake_case (school level) or camelCase (advanced).
C++
 
// Good (school level)
int student_roll_number;
float average_marks;
string student_name;

// Good (BS level - camelCase)
int studentRollNumber;
float averageMarks;
string studentName;
 
 

Constants

  • Always UPPER_SNAKE_CASE
C++
 
const int MAX_STUDENTS = 100;
const double PI = 3.14159;
 
 

Functions

  • Use verbs + camelCase or snake_case
C++
 
// Good
void calculate_average();
void display_student_record();
int get_total_marks();

// Bad
void calc();        // Not clear
void show();       // Too vague
 
 

Classes and Structs

  • Use PascalCase
C++
 
class StudentRecord {
    // ...
};

struct EmployeeDetails {
    // ...
};
 
 

Private Member Variables (BS Level)

  • Common styles:
    • Prefix with m_ (member)
    • Suffix with _
C++
 
class Student {
private:
    string m_name;     // or name_
    int m_age;         // or age_
};
 
 

Example: Applying Naming in Student Record System

C++
 
// Good naming (snake_case - suitable for Class 5-12)
struct Student {
    int roll_number;
    char student_name[50];
    int age;
    float total_marks;
};

void add_student();
void display_all_students();
void search_student_by_roll(int search_roll);
 
 
C++
 
// Modern style (camelCase + PascalCase - BS level)
class Student {
public:
    int rollNumber;
    string studentName;
    int age;
    float totalMarks;
    
    void displayDetails();
};

void addStudent();
void displayAllStudents();
 
 

Comments

Comments explain why and how the code works. They do NOT repeat what the code obviously does.

Types of Comments in C++

  1. Single-line comment: // comment
  2. Multi-line comment: /* comment */
  3. Documentation comment (advanced): /** … */ (used by tools like Doxygen)

Good vs Bad Comments

 
 
Bad Comment (Useless)Good Comment (Helpful)
i++; // increment i// Skip header row in CSV file
int marks = 95; // marks// Cap marks at 100 to prevent invalid input
return 0; // end program// Return -1 if student not found
 

When to Write Comments

  1. At the top of file – File purpose, author, date.
  2. Before functions – What it does, parameters, return value.
  3. Complex logic – Explain tricky algorithms.
  4. TODO or FIX – Mark things to improve later.

Best Practices for Comments

1. File Header Comment (Recommended for all projects)

C++
 
/*
 * File: student_record_system.cpp
 * Author: Your Name
 * Class: 10th / BS CS
 * Date: 24 December 2025
 * Description: A menu-driven program to manage student records
 *              using binary file handling.
 */
 
 

2. Function Comment (Very Important)

C++
 
/**
 * Adds a new student record to the binary file
 * @param none
 * @return void
 * Prompts user for details and appends to "students.dat"
 */
void addStudent() {
    // ...
}

/* Alternative simple style (for school) */
 // Function to display all students from file
void displayAllStudents() {
    // ...
}
 
 

3. Inline Comments – Use Sparingly

Only for complex parts:

C++
 
total = (midterm + finalExam * 2) / 3;  
// Final exam has double weight as per school policy
 
 

4. TODO Comments (Useful in projects)

C++
 
// TODO: Add validation for duplicate roll numbers
// TODO: Implement sorting by marks
 
 

What NOT to Do

  • Over-comment obvious code.
  • Write outdated comments (comment says one thing, code does another → worse than no comment!).
  • Use comments to disable code permanently → delete it or use version control.

Full Example: Clean Code with Standards

C++
 
/*
 * File: clean_student_system.cpp
 * Description: Simple student management with good naming and comments
 */

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int MAX_NAME_LENGTH = 50;

// Structure to hold one student record
struct Student {
    int roll_number;
    char student_name[MAX_NAME_LENGTH];
    float marks;
};

// Adds a new student to file
void add_student() {
    ofstream out_file("students.dat", ios::binary | ios::app);
    
    Student new_student;
    cout << "Enter roll number: ";
    cin >> new_student.roll_number;
    
    cout << "Enter name: ";
    cin.ignore();
    cin.getline(new_student.student_name, MAX_NAME_LENGTH);
    
    cout << "Enter marks: ";
    cin >> new_student.marks;
    
    // Validate marks range
    if (new_student.marks < 0 || new_student.marks > 100) {
        cout << "Invalid marks! Setting to 0." << endl;
        new_student.marks = 0;
    }
    
    out_file.write((char*)&new_student, sizeof(new_student));
    out_file.close();
    
    cout << "Student added successfully!\n";
}

int main() {
    // TODO: Add menu system later
    add_student();
    return 0;
}
 
 

Summary Table (For Your Website)

 
 
ElementRecommended Style (School)Recommended Style (BS/Professional)Example
Variablessnake_casecamelCasetotal_marks / totalMarks
ConstantsUPPER_SNAKE_CASEUPPER_SNAKE_CASEMAX_STUDENTS
Functionssnake_casecamelCasedisplay_record()
Classes/StructsPascalCasePascalCaseStudentRecord
Comments// and /* *///, /* /, /* */ (Doxygen)Clear, meaningful