Skip to content

CPP Programming

mogsdad edited this page Feb 15, 2015 · 3 revisions

C++ conventions for objects, methods, and variables

For consistency with FRC library software, we utilize the same C++ conventions:

Type of name Naming Rule Examples
Class name Initial capital CamelCase1.
Acronyms all-caps.
Robot,
PWM
Method name Initial capital CamelCase. StartCompetition,
DoWork
Member variable m_ followed by member variable name,
initial lower CamelCase.
m_deleteSpeedControllers
Local variable Initial lower CamelCase targetAngle
Constant k followed by constant name,
initial capital CamelCase.
kMaxServoAngle
Macro All caps, spaced with _. DISALLOW_COPY_AND_ASSIGN

Code formatting

All code shall be written in Kernaghan & Ritchie style, utilizing the built-in default formatter in Eclipse.

/*
 * A sample source file for the code formatter preview
 */
#include <math.h>

class Point {
public:
    Point(double x, double y) :
            x(x), y(y) {
    }
    double distance(const Point& other) const;

    double x;
    double y;
};

double Point::distance(const Point& other) const {
    double dx = x - other.x;
    double dy = y - other.y;
    return sqrt(dx * dx + dy * dy);
}

Preference setting

Make sure that K&R formatting is selected under Window > Preferences.

Use formatter

To get the automatic formatter to clean up a section of code, just select the lines you want tidied, then go to Source > Format. Alternatively, you can use the keyboard shortcut CTRL-Shift-F.