This document outlines the coding style guidelines for meniOS, inspired by the Linux Kernel Coding Style but with specific modifications to suit the meniOS project. Following these guidelines ensures consistency and readability across the codebase.
-
Rule: Use two spaces for indentation. Do not use tabs.
-
Good Example:
if(condition) { do_something(); }
-
Bad Example:
if(condition) { do_something(); // Four spaces used for indentation } if(condition) { do_something(); // No indentation used } if(condition) { do_something(); // No indentation used } if(condition) { do_something(); // Tab used for indentation }
-
Rule: The maximum line length is 120 characters.
-
Good Example:
int calculate_result(int a, int b) { return a + b * 2; // Line length is well under 120 characters }
-
Bad Example:
int calculate_result(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) { return a + b + c + d + e + f + g + h + i + j; // Line length exceeds 120 characters }
-
Rule: The opening brace should be on the same line as the declaration. This applies to all control statements (if, else, for, while, etc.) and function definitions.
-
Good Example:
if(condition) { do_something(); } for (int i = 0; i < n; i++) { process(i); } void function() { // function body }
-
Bad Example:
if(condition) { do_something(); // Opening brace is on a new line } for (int i = 0; i < n; i++) { process(i); // Opening brace is on a new line } void function() { // function body; // Opening brace is on a new line }
-
Rule: Always use braces for all control statements, even if the body contains only one line.
-
Good Example:
if(condition) { do_something(); } while (condition) { continue_processing(); }
-
Bad Example:
if(condition) do_something(); // Braces are missing while (condition) continue_processing(); // Braces are missing
-
Rule: Header guards should be formatted based on the file location:
MENIOS_INCLUDE_KERNEL_
prefix for files insideinclude/kernel
.MENIOS_INCLUDE_
prefix for files insideinclude/
.MENIOS_
prefix for files located anywhere else.- Header guards should end with
_H
.
-
Good Examples:
// File: include/kernel/somefile.h #ifndef MENIOS_INCLUDE_KERNEL_SOMEFILE_H #define MENIOS_INCLUDE_KERNEL_SOMEFILE_H // header file content #endif // MENIOS_INCLUDE_KERNEL_SOMEFILE_H
// File: include/util.h #ifndef MENIOS_INCLUDE_UTIL_H #define MENIOS_INCLUDE_UTIL_H // header file content #endif // MENIOS_INCLUDE_UTIL_H
// File: src/driver.h #ifndef MENIOS_DRIVER_H #define MENIOS_DRIVER_H // header file content #endif // MENIOS_DRIVER_H
-
Bad Examples:
// File: include/kernel/somefile.h #ifndef KERNEL_SOMEFILE_H #define KERNEL_SOMEFILE_H // Incorrect prefix and missing MENIOS_INCLUDE_ // header file content #endif
// File: include/util.h #ifndef UTIL_H #define UTIL_H // Incorrect prefix and missing MENIOS_INCLUDE_ // header file content #endif
// File: src/driver.h #ifndef DRIVER_H #define DRIVER_H // Incorrect prefix and missing MENIOS_ // header file content #endif
For any scenarios or coding practices not covered by this document, please refer to the Linux Coding Style guidelines. Updates to this document will be communicated as necessary.
By following these guidelines, the meniOS codebase will maintain a consistent and readable format, improving collaboration and code maintenance.