Skip to content

CodingStyle

joshdahmer edited this page Jan 17, 2018 · 2 revisions

Coding Style Guide

The DropBot project contains various software components written in either C++ (e.g., Arduino firmware and host proxies for the ControlBoard and other hardware modules) and Python (e.g., the Microdrop UI and wrappers for hardware proxies). Both the Python and Arduino ecosystems have their own style guidelines (e.g., Python's PEP81 and Arduino's style guide2 and API style guide3).

One option would be to use the Arduino style for all C++ code and PEP8 for all Python code.

Pros:

  • People familiar with programing Arduino and/or Python don't have to learn a new style
  • C++/Python libraries used outside of the !DropBot project would adhere to those communities' conventions (e.g., the !RemoteObject class)

Cons:

  • Lack of consistency within the !DropBot project. The same variable would have a `mixedCase` name in the C++ code (e.g., numberOfChannels), but a `lower_case_with_underscores` name in the corresponding Python wrapper (e.g., number_of_channels).
  • People have to learn and remember two separate styles

We chose to prioritize consistency across our code base and adopted a compromise between PEP8 and the Arduino style guides for both C++ and Python code. Where these styles conflict (e.g., naming conventions), we generally defer to PEP8 since the majority of the Microdrop/DropBot code is written in Python. This has the benefit that it reduces naming collisions on the Arduino, which has a cluttered global namespace.

For Python code

  • Follow PEP8 conventions

For C++/Arduino code

  • Use `CamelCase` for C++ file names, consistent with Arduino conventions (e.g., `DMFControlBoard.cpp`)
  • Use a trailing underscore for member variables (e.g., `DMFControlBoard::number_of_channels_`)
  • In contrast to the Arduino guidelines, prefer explicit type names (e.g., `uint16_t` vs `int`, `uint32_t` vs `long`). Since we are writing C++ code for both the Arduino and PC and the size of these data types may change between processors/compilers, it is better to be explicit.
  • Use 2 spaces for indentation (no tabs!). This is the default for the Arduino IDE. It differs from PEP8, but most editors will allow you to set the indentation level based on the file extension so you can easily use 4 spaces for Python code and 2 spaces for C++ code.

For Both Python and C++/Arduino code

  • Follow PEP8 naming conventions (variable and function names use `lower_case_with_underscores`, class/struct names use `CamelCase`, constants use `CAPITALS_WITH_UNDERSCORES`)

References

1.PEP8 2.Arduino Style Guide 3.Arduino API Style Guide 4.Google C++ Style Guide

Clone this wiki locally