Repository navigation
Coding conventions
OlegHahm edited this page Mar 29, 2013
·
35 revisions
- Avoid dynamic memory allocation (malloc/free, new etc.)! It will break real-time guarantees, increase code complexity, and make it more likely to use more memory than what is available.
- Line length: aim for not more than 80 characters per line, the absolute maximum should be at 100 characters per line.
- Be careful with platform depending type sizes like
intorlong. Use data types that includes the bit length in the name likeuint16_twhen you need to make sure that a certain variable has exactly this length. - A type definition (using
typedef) always ends with a "_t". - If typedefs are used for structs, it has to be specified at the struct definition (i.e., not as a separate line). E.g. like this:
typedef struct {
uint8_t a;
uint8_t b;
} foobar_t;
- Do NOT use global variables unless there is no way to avoid it.
- If you declare a variable within a header file, you MUST use the keyword
extern.
- Every functions needs a prototype - additional to its definition. If a prototype is specified within a c-File it has to be declared BEFORE any function definition.
- If the scope of a function is limited to one file, it MUST be declared as static.
- Functions without parameters must be specified with
void, e.g.xxx(void). - Keep functions short! As a rule of thumb, the function's body should not exceed one screen.
- Do NOT use global macros defining more than one line of code. Use inline functions instead.
- Any function must return one of the following values:
- logical value (zero or not zero)
- an error code (given as a negative number or zero) or a positive status value
- the count of read or written bytes/values for I/O functions
- the position or address (for search functions)
- a pointer
-
NULLindicates an error case, too. - Do NOT return a struct or any bigger value! They would get copied to the stack, resulting in expensive operations. Moreover, some compilers have trouble with larger return values. Use pointer to structs instead, but pay attention to the lifetime of the struct.
- If possible, prefer int over uint, in order to add error codes later on.
RIOT - The friendly Operating System for the Internet of Things
Homepage | [GitHub] (https://github.com/RIOT-OS/) | Developers Mailing List | Users Mailing List | Twitter @RIOT_OS
- Family: ARM
- Board: Airfy Beacon
- Board: Arduino Due
- Board: IoT LAB_M3
- Board: mbed_lpc1768
- Board: MSB-IoT
- Board: MSBA2
- Board: OpenMote
- Board: PCA1000x (nRF51822 Development Kit)
- Board: UDOO
- Board: Samr21 xpro
- Board: Spark Core
- Board: STM32F0discovery
- Board: STM32F3discovery
- Board: STM32F4discovery
- Board: yunjia-nrf51822
- Family: ATmega
- Board: Arduino Mega2560
- Family: MSP430
- Board: MSB-430H
- Board: TelosB
- Board: WSN430
- Board: Zolertia Z1
- Board: eZ430-Chronos
- Family: native
- Board: native
- Family: x86
- Board: Intel Galileo