-
Notifications
You must be signed in to change notification settings - Fork 9
Coding Style
Local/Public variables will be Camel Case. Ex: myVar
Private variables will be Camel Case with an "m_" in front. Ex: m_myVar
Functions (all of them) are also Camel Case. Ex: myFunc()
Class names will be Pascal Case. Ex: MyClass
Constants are all caps, separated by underscores. Ex: MY_CONSTANT
Enum names are Pascal case, and values are written like constants. We use enum classes instead of just enums.
//For small enums
enum class MyEnum { ENUM_1, ENUM_2 };
//For large enums
enum MyEnum {
ENUM_1,
ENUM_2,
...
};
//Then to call an enum, you use
MyEnum::ENUM_1;
Switch blocks should be organized with the following indentation. They should also attempt to use default whenever possible.
switch (value) {
case 0:
case 1:
// Code
break;
case 2:
// Code
break;
default:
// Code
break;
}
C++ pointers and references should have their reference symbol next to the type rather than to the name.
float* savePercentages;
// NOT: float *savePercentages; or float * savePercentages;
int& numCups;
// NOT: int &numCups; or int & numCups;
The name of the object is implicit, and should be avoided in a method name.
puck.getDensity(); // NOT: puck.getPuckDensity();
The terms get/set may be used where an attribute is accessed directly.
player.getNumber();
player.setNumber(number);
stick.getFlex();
stick.setFlex(flex);
The term initialize should be used where an object or a concept is established.
rink.initializePaintedLines();
video.initializeOnScreenScore();
Variables representing GUI components should be suffixed by the component type name.
Ex: scoreboardText
, mainWindow
, fileMenu
Iterator variables should be called i
, j
, k ect. or
y,
z,
x`, depending on how they are used
//iterating through an array
for (int i = 0; i < numGoals); i++) {
goals[i].playVideo();
}
//Iterating through a chunk
for (int y = 0; y < CHUNK_WIDTH; y++) {
for (int z = 0; z< CHUNK_WIDTH; z++) {
for (int x = 0; x < CHUNK_WIDTH; x++) {
…
The prefix is should be used for boolean variables and methods.
isGoodGoal
, isRetired
, isWinningTeam
.
Occasionally the has
, can
, should
, in
, and want
prefixes will be better choices.
Complement names must be used for complement operations
get/set
, add/remove
, create/destroy
, start/stop
.
computeGoalsAgainstAverage(); // NOT: compGlsAgstAvg();
Puck* puck; // NOT: Puck* puckPtr;
bool isRetired; // NOT: isNotRetired or isNotPlaying
In header files, use minimal includes and forward declaration if possible. In source files, includes should be laid out in the following block order, and A-Z within blocks (except for the first two includes):
#include "stdafx.h"
#include "HeaderForMyFileIfItExists.h"
#include <systemfiles>
#include <standardfiles.h>
#include <ThirdParty/dependencies.h>
#include <ExternalProjectReferences.h>
#include "MyProjectFiles.h"
enum Jersey {
JERSEY_HOME,
JERSEY_AWAY,
JERSEY_ALTERNATE
};