.clang-format in the project root.
This document describes the coding style used for C++ code in the Serenity Operating System project. All new code should conform to this style.
Names
A combination of CamelCase, snake_case, and SCREAMING_CASE:- Use CamelCase (Capitalize the first letter, including all letters in an acronym) in a class, struct, or namespace name
- Use snake_case (all lowercase, with underscores separating words) for variable and function names
- Use SCREAMING_CASE for constants (both global and static member variables)
Right:
Wrong:
Data Members
Data members in C++ classes should be private. Static data members should be prefixed bys_. Other data members should be prefixed by m_. Global variables should be prefixed by g_.
Getters and Setters
Precede setters with the word “set”. Use bare words for getters. Setter and getter names should match the names of the variables being set/gotten.Function Names
Use descriptive verbs in function names.When there are two getters for a variable, and one of them automatically makes sure the requested object is instantiated, prefix that getter function with
ensure_. As it ensures that an object is created, it should consequently also return a reference, not a pointer.Function Parameters
Leave meaningless variable names out of function declarations. A good rule of thumb is if the parameter type name contains the parameter name (without trailing numbers or pluralization), then the parameter name isn’t needed.Enums and Constants
Enum members should use InterCaps with an initial capital letter. Preferconst to #define. Prefer inline functions to macros.
#defined constants should use all uppercase names with words separated by underscores.
Header Guards
Use#pragma once instead of #define and #ifdef for header guards.
Other Punctuation
Constructors
Constructors for C++ classes should initialize their members using C++ initializer syntax. Each member (and superclass) should be indented on a separate line, with the colon or comma preceding the member on that line. Prefer initialization at member definition whenever possible.Iterators
Prefer index or range-for over iterators in Vector iterations for terse, easier-to-read code.Pointers and References
Both pointer types and reference types should be written with no space between the type name and the* or &.
An out argument of a function should be passed by reference except rare cases where it is optional in which case it should be passed by pointer.
”using” Statements
In header files in the AK sub-library, it is acceptable to use “using” declarations at the end of the file to import one or more names in the AK namespace into the global scope.Types
Omit “int” when using “unsigned” modifier. Do not use “signed” modifier. Use “int” by itself instead.Classes
For types with methods, preferclass over struct.
- For classes, make public getters and setters, keep members private with
m_prefix. - For structs, let everything be public and skip the
m_prefix.
Constructors and Type Conversion
Use a constructor to do an implicit conversion when the argument is reasonably thought of as a type conversion and the type conversion is fast. Otherwise, use the explicit keyword or a function returning the type.Singleton Pattern
Use a static member function named “the()” to access the instance of the singleton.Comments
Make comments look like sentences by starting with a capital letter and ending with a period (punctuation). One exception may be end of line comments like thisif (x == y) // false for NaN.
Use FIXME: (without attribution) to denote items that need to be addressed in the future.
Overriding Virtual Methods
The declaration of a virtual method inside a class must be declared with thevirtual keyword. All subclasses of that class must also specify either the override keyword when overriding the virtual method, or the final keyword when overriding the virtual method and requiring that no further subclasses can override it.
Const Placement
Use “east const” style whereconst is written on the right side of the type being qualified.
Casts
Before you consider a cast, please see if your problem can be solved another way that avoids the visual clutter.Omission of Curly Braces
Curly braces may only be omitted fromif/else/for/while/etc. statement blocks if the body is a single line.
Additionally, if any body of a connected if/else statement requires curly braces according to this rule, all of them do.
