Compiler Warnings
- Your code compiles cleanly, without the compiler issuing any warnings (and this is not acheived by disabling the warnings we turn on for you in the provided Makefile).
Names
- Field (member variable) names are appropriately descriptive of what they stand for; names shouldn’t just reiterate the type. While concise names are best, long and understandable names are preferred over small and mysterious ones.
-
Variable, constant, and function names are in
lower_snake_case. -
“Small” structs and classes have
lower_snake_casenames, whereas “large” structs and classes haveUpper_snake_casenames. -
Preprocessor
#defines are inSCREAMING_SNAKE_CASE.
Variables
-
It’s better to define a variable with an initializer later
than to define it uninitialized earlier and assign it later.
Bad Better some_type* x; if (some_condition) { x = malloc(sizeof *x); if (x == NULL) exit(1); // use x in here }if (some_condition) { some_type* x = malloc(sizeof *x); if (x == NULL) exit(1); // use x in here }
Booleans
-
Don’t compare a Boolean to another Boolean, because it
already is a Boolean!
Bad Better if (is_good(doggo) == true) {if (is_good(doggo)) {if (is_good(doggo) == false) {if (!is_good(doggo)) { -
Don’t use an
ifjust to return a Boolean you already have.Bad Better if (a < b) { return true; } else { return false; }return a < b;if (is_good(doggo)) { return false; } else { return true; }return !is_good(doggo); -
When you need a Boolean, use
boolliteralstrueandfalse, notintliterals1and0. C automatically converts between the two types as needed, butboolmakes your code clearer because it gives the reader a more precise idea what it’s for. (To get access to typebooland its literals in C,#include <stdbool.h>.)
Statements
-
Use
elsealong withiffor a case whose condition is the opposite of theifcondition. In particular, don’t check a condition and then its opposite in adjacentifs.Bad Good if ( n % 2 == 0 ) { ⋮ // even case } if ( n % 2 != 0 ) { ⋮ // odd case }if ( n % 2 == 0 ) { ⋮ // even case } else { ⋮ // odd case } -
Always use braces with
if,while, andforstatements. Leaving them out can cause easy-to-make but difficult-to-find mistakes when modifying code later.Bad Good if (test_cond()) error_message();if (test_cond()) { error_message(); }while (result) result = check_state();while (result) { result = check_state(); }
Formatting
-
Lines are no longer than 80 characters.
You can use the grep(1) command to print out the lines in a file that exceed the limit. For example, to check every .c and .h file in the src directory, run
% grep -nE '.{81,}' src/*.{c,h} - Indentation is consistent and properly reflects code structure.
- Indentation is by four spaces; tabs are unacceptable because they do not display the same everywhere. (Note that this does not mean that you should avoid the Tab key—pressing Tab in Micro will correctly align the current line using spaces, not tabs.)
- Long blocks of code are separated into “paragraphs” using blank lines.
- Infix operators generally have space on both sides.
- Commas and semicolons have whitespace (a space or end-of-line) after but no space before.
Comments