Repository navigation
6. Code Style
TheSlowGrowth edited this page Mar 22, 2021
·
1 revision
- Use clang-format. The clang-format spec is enforced in pull requests.
- C++ files end on
.cpp - C files end on
.c - Header files for both languages end on
.h - C++ code is preferred over C code
- Each C++ header file has
#pragma once - Each C header has an include guard:
#ifndef __DAC_H__
#define __DAC_H__
#endif // #ifndef __DAC_H__- Use
""for including local files, use<>for system headers - Never use
using namespacein a header file - Where possible, unit tests are provided
- Avoid preprocessor macros where possible, use
static constexprvariables instead - Prefer
using TypeA = typeBovertypedef TypeB TypeA; - Be generous with comments, use doxygen comments where it makes sense.
- No dynamic memory allocation. Allocate everything statically.
- Use descriptive names
int numBytesReceived; // good
int nRx; // bad- Use camelCase
int numBytesReceived; // good
int num_bytes_received; // bad- Types start with an upper case letter
class WavFileReader;- Variables and function arguments start with an lower case letter
int numFilesWritten;- Functions start with an lower case letter
void setupAudioOutput();- Member variables end on an underscore to destinguish them from function arguments:
class Counter
{
private:
int value_;
};- Preprocessor macros are all upper case with snake_case_formatting:
#define PLEASE_DONT_USE_MACROS_WHEN_POSSIBLE- Prefer strong types
- Use
enum classinstead ofenum - Don't use raw pointers unless that's strictly required
- Prefer descriptive
enum classarguments instead ofboolarguments:
setLeds(true, false, false); // bad
setLeds(Red::on, Blue::off, Green::off); // good- Use
constwhere possible:
void doSomething(const int input); // good, communicates intention: This won't be changed during the function
void doSomething(int input); // bad, even though technically identical.- Do not pass and return simple types by const ref
int doSomething(); // good, copy the simple type
int& doSomething(); // bad, pass simple type by ref with no performance benefit, but security risks.- Use
StaticVector<>instead of C-style arrays - Properly utilize
overrideandfinal