Arduino Introduction (notes)
Štěpán Stenchlák
Code decomposition
We decompose a code because:
- We may use it somewhere else
- We can test individual parts separately
- It is easier to spot bugs
- It is easier to develop larger applications this way
Therefore individual functions
- Should have a single functionality
- Should be as independent as possible
// Universal functions
int findArrayMin(int[] values, int size);
void repeatCharacter(char character, size_t count);
// Task specific functions
void printCelmometherLine(int value, int leftPadding);
void printCelmomether(int[] values, int size);Descriptive naming
You do not usually need comments if you correctly name variables, functions, and arguments.
Function name and its arguments are the only things that the programmer sees (besides the comments).
void textHelper(char c, size_t i);
void repeatCharacter(char character, size_t count);
void printLine(...);
void printCelmometherLine(...);Consistent style & consistency & style
if (i > 0) {
doSomething(i, true);
} else {
doSomethingElse(-i, false);
}Use constants
Arduino Introduction
By Štěpán Stenchlák
Arduino Introduction
- 361