![]() |
BSB Developer Docs
|
This is a stub page about the code style.
After making any changes, the code must be formatted by running ./fbt format from the project root directory. In case it is preferable to use the built-in formatting feature in the IDE/editor, it must use clang-format with the configuration file provided in the aforementioned directory.
Always try to make the code as clear and straightforward as possible. Avoid "clever code", obscure language constructions or other kinds of obfuscation.
Only make the changes in existing code that are necessary to implement the feature or to fix a bug. Do NOT make changes in existing code that are related to style or personal preference, as this drastically increases noise during review.
Style-related corrections for existing code should be carried out via separate pull requests where possible.
Avoid comments unless it will greatly improve clarity. Most of the time a comment can be avoided by extracting a function or by a more descriptive variable name.
If using an LLM, care must be taken to rephrase or remove the generated comments, as they tend to be overly verbose.
Both C++ // This is a comment and ANSI C style /* This is a comment */ comments can be used. The latter should be preferred for multi-line comments.
Functions must be relatively short and do roughly one thing. They must have descriptive names that make it clear what the function is doing.
my_awesome_header.h, ingenious_source.cmy_app_settings.c instead of settings.cFunction and variable names must be in snake case:
Custom types must be in camel case:
Always put curly braces around conditional blocks, even when the branch body is a single line:
If a variable does not have to change, mark it as const. This applies both to global and local variables.
If a function parameter is not an input parameter, mark it as const, unless it is of a primitive type.
Where possible, replace a switch with array indexing:
If the above is not practical, e.g. my_index values are not contigious, it is preferable to use if...else over switch:
It is preferable to always use one return statement per function. Among other benefits, it reduces the chance of forgetting to deallocate heap memory due to an early return.
Note the do ... while(false) pattern, it is widely used throughout the BUSY Bar firmware.
Always define custom types explicitly using typedef. Avoid using anonymous inline types:
If a file my_module.h describes a class-like object, all of the method functions should have a my_module_ prefix.
For object-oriented code, a this function parameter should be called instance, unless this function operates on a sub-object or a helper type (see above).
Always try hiding as much implementation detail as possible by using opaque pointers: