- Headers
- Macros
- Types
- Global Variables
- Functions
- main
- DO use 4 spaces as tab.
{
on own line.}
also on own line except ondo while
statements.
Example:
if (cond)
{
// ...
}
else
{
// ...
}
while (cond)
{
// ...
}
do
{
// ...
} while (cond);
struct FooBar
{
FooBar();
};
namespace bazz
{
} // namespace bazz
extern "C"
{
// ...
}
void do_foo()
{
// ...
}
- Avoid comment that paraphrases the code. Comments should answer the why and the code the how.
- Return type and modifiers on the same line as the function name.
- The arguments is adjacent to the function name.
- Use
snake_case
for their name.
Example:
static void foo()
{
bar;
}
foo();
- Also use
snake_case
for their name. - In declaration of pointer the * is adjacent to their name, not the type.
- Use a space after
if
,for
,while
,switch
. - Do not use a space after the opening ( and before the closing ).
- Preferably use () with
sizeof
. - Use
nullptr
when necessary otherwise useNULL
. - Always use curly braces after
if
,for
,while
,switch
statements.
DON'T
if (foo)
bar;
DO
if (foo)
{
bar;
}
- Don't indent cases another level.
- Switch case should NOT falltrough.
- But if you have no other choise (very unlikely) add a
/* FALLTROUGH */
comment.
Example:
switch(foo)
{
case bar:
printf("foo");
/* FALLTROUGH */
case mitzvah:
printf("barmitzvah\n");
break;
}
- Include libraries first, then include local headers (separate them with a new line).
- Use
.h
as extension. - Most of the headers of skift include the file
libutils/Prelude.h
wich include most of the standard libraries (stdio
,stdlib
,stddef
,stdint
and some Macros). - If you want to declare a new header use
#pragma once
instead of using cross-defines.
Example:
#include <libio/Streams.h>
#include "foo.h"
- Don't use
type_t
naming. (If you see one in the code base (on non-posix standard libraries), please report it 😅) - Don't typedef builtin types.
- Use
PascalCase
for typedef'd types.
- Keep lines between 60 and 80 characters long.
- Always put
argc
andargv
as arguments (NEVER put nothing or void as arguments). - If you don't need
argc
and/orargv
use the macroUNUSED
. - Use double pointer for
argv
.
Example:
int main(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
}
return -1
when there is an errorreturn 0
when there is not- When using
HjResult
for error handling, use the functionresult_to_string
for printing errors out
DON'T
if (result == ERR_NOT_READABLE)
{
printf("rmdir: failed to remove '%s': Not readable", path);
}
else if (result == ERR_DIRECTORY_NOT_EMPTY)
{
printf("rmdir: failed to remove '%s': Directory not empty", path);
}
DO
printf("rmdir: failed to remove '%s': %s", path, result_to_string(result));
- You can use the command
clang-format -r
for formatting your code automaticly