asMSX coding style
April 15, 2019 ยท View on GitHub
If you want to contribute to asMSX, please consider following our shared guildelines for coding style
1. Set your editor to use tabs and show them as 4 spaces
In vim, add following lines to your .vimrc:
set tabstop=4
set shiftwidth=4
set noexpandtab
Visual Studio seems to come with those settings by default.
2. Highlight tabs, spaces, cr and lf explicitly if you can
In vim, add those lines to your .vimrc to highlight spaces and tabs:
syntax on
set syntax=whitespace
In Visual Studio, press Ctrl+R and Ctrl+W.
3. Use Allman brackets
function samplef(int a, int b, int c, int d)
{
if (a == 1)
{
a_function();
if (b == 50)
c = 100;
}
else if ((a == BBB) && (d != 0))
{
another_function();
c = 200;
}
else
{
fprintf(stderr, "Unexpected value found: %d\n", a);
exit(1);
}
}
- Curly brackets are on their own line and start at the begining of the block.
- Don't add comments after curly brackets, put them above or below please.
- If you want to use
if () {} else if () {} else {};, putelse ifandelseon a separate line, idented same as starterif. - If you have more then a couple of
else if, consider usingswitch()instead. - If block consist of single statement, don't use curly brackets, unless it is a very convoluted, deeply nested 'if/else if/else' code or you have some other reasons to beleive it will improve clarity.
- C99
<stdint.h>types are preferred, but plain C89 char/int/long with optional signed/unsigned qualifier are acceptable. - Only do typecasts when necessary.
- Try to address all the compiler warnings in your code: remove unused variables, avoid using uninitialized variables etc.
- Once the project starts using gtest/ctest, please add test units to cover new or changed code in your patch.
- Use static checking tools that are available: enable all the warnings in compiler, use a linter in Linux/BSD, use cppcheck and Visual Studio Code Analysis tool on Windows.
- If you can do it, please build your code on as many platforms as possible: Visual Studio 2017 on Windows 10, gcc on Linux, clang on BSD and Mac etc. Sometimes you'll find that header you think is everywhere, isn't everywhere.
- Consider reading John Carmack's article In-depth: Functional programming in C++. It works well for plain C programs too.
- Consider reading How to C in 2016. Good tips.
- List of verifications tools.