Skip to content

Code_organization

Proper project structure improves maintainability.

MyProject/
├── include/
│ └── myproject/
│ ├── utils.h
│ ├── config.h
│ └── types.h
├── src/
│ ├── utils.cpp
│ ├── main.cpp
│ └── CMakeLists.txt
├── tests/
│ ├── test_utils.cpp
│ └── CMakeLists.txt
├── CMakeLists.txt
└── README.md
utils.h
#ifndef MYPROJECT_UTILS_H
#define MYPROJECT_UTILS_H
// 1. Includes
#include <string>
#include <vector>
// 2. Forward declarations
class Config;
// 3. Constants
constexpr int MAX_SIZE = 100;
// 4. Function declarations
int calculate(int x);
// 5. Classes
class Utils {
public:
static void process();
};
#endif
utils.cpp
// 1. Module headers
#include "utils.h"
// 2. System headers
#include <iostream>
// 3. Implementation
int calculate(int x) {
return x * 2;
}
  1. One class per header file
  2. Use include guards
  3. Group related functionality
  4. Keep headers self-contained
  5. Use forward declarations when possible