Difference between revisions of "Coding Style"

From ReactOS Wiki
Jump to: navigation, search
(Clean this up a bit until (if) the new Coding Style gets published.)
Line 1: Line 1:
 
The ReactOS Project currently enforces very few rules for coding style.
 
The ReactOS Project currently enforces very few rules for coding style.
  
== Generic ideas whether and how to apply formatting ==
+
== 3rd party code ==
 +
ReactOS uses code from other projects (e.g., Wine). In these instances the following guidelines do not apply and the existing style should be used. This allows easier syncing of future changes.
  
* When hacking on existing code follow the existing style rather than reformat
+
== Formatting ==
* If reformatting to the generic coding style:
+
Formatting should be done separately from code changes. Patches or commits that only change formatting should indicate so (e.g., "Formatting, no code changes", [FORMATTING] label).
** never mix code changes and formatting
 
** say clear in commit message that you change formatting only (like [FORMATTING])
 
* Don't reformat shared code (with Wine or other projects), it's hard to merge changes into reformatted code
 
  
 +
=== Indentation ===
 +
One level of indentation is 4 columns. Spaces must be used.
  
 +
=== Braces ===
 +
Braces must be placed on their own lines. They must be at the same indentation level as the previous statement. Braces are optional if only one statement is enclosed in it, excepting when a comment is also present.
  
== Source code file structure ==
+
Incorrect:
 +
<syntaxhighlight lang="c">
 +
if (foo()) {
 +
    bar();
 +
}
 +
 
 +
if (foo())
 +
    /* Comment */
 +
    bar();
 +
</syntaxhighlight>
 +
Correct:
 +
<syntaxhighlight lang="c">
 +
if (foo())
 +
{
 +
    bar();
 +
}
 +
 
 +
 
 +
if (foo())
 +
    bar();
 +
 
 +
 
 +
if (foo())
 +
{
 +
    /* Comment */
 +
    bar();
 +
}
 +
</syntaxhighlight>
 +
 
 +
=== Long lines ===
 +
80 columns should be used as a guide for line length.
 +
 
 +
* When a line with function call is less than 80 chars long, params should be written on the same line
 +
* When a line with function call is more than 80 chars long (because calling func has many params), it maybe wise to write params as column, writing comments for each param (when it is needed)
 +
 
 +
== File structure ==
 
The usual source code file consists of the following parts (some sections can be omitted if they are empty):
 
The usual source code file consists of the following parts (some sections can be omitted if they are empty):
 
* The header (an example)
 
* The header (an example)
Line 51: Line 88:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
== The Rules ==  
+
== The Rules ==
 
+
Example of standard API header, please don't make up your own until really necessary:
* When a line with function call is less than 80 chars long, params should be written on the same line
 
* When a line with function call is more than 80 chars long (because calling func has many params), it maybe wise to write params as column, writing comments for each param (when it is needed)
 
* {} must be on its own line, they must be aligned to the beginning of previous statement, statements inside {} are indented
 
* [NOT STRICT] Don't use {} if only one statement is enclosed in it (except when comments like /* TODO */ or /* FIXME */ or DPRINTs are included also)
 
* TABs vs. Spaces:
 
** Kernel is strictly Spaces-only
 
** When editing other modules, either obey the style, or do a conversion to one style (which results in fewer changes of tabs->spaces or vice versa).
 
* Generic note about TABs usage: Don't use TABs for formatting; use TABs for indenting only and use only spaces for formatting
 
Example:
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
NTSTATUS
+
/*++
SomeApi(IN Type Param1,
+
* @name SomeAPI
[spaces]IN Type Param2)
+
* @implemented NT4
{
+
*
[TAB]ULONG MyVar;
+
* Do nothing for 500ms.
[TAB]MyVar = 0;
+
*
[TAB]if ((MyVar == 3) &&
+
* @param SomeParameter
[TAB][sp](Param1 == TRUE))
+
*        Description of the parameter. Wrapped to more lines on ~70th
[TAB]{
+
*        column.
[TAB][TAB]CallSomeFunc();
+
*
...
+
* @param YetAnotherParameter
</syntaxhighlight>
+
*        Bleh, bleh :)
* Unfortunately if the code is shared with WINE, it isn't possible to use // comments, /*...*/ must always be used
+
*
* Example of standard API header, please don't make up your own until really necessary:
+
* @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
  /*++
+
*        othwerwise.
  * @name SomeAPI
+
*
  * @implemented NT4
+
* @remarks Must be called at IRQL == DISPATCH_LEVEL
  *
+
*
  * Do nothing for 500ms.
+
*--*/
  *
+
NTSTATUS
  * @param SomeParameter
+
NTAPI
  *        Description of the parameter. Wrapped to more lines on ~70th
+
SomeAPI(...)
  *        column.
 
  *
 
  * @param YetAnotherParameter
 
  *        Bleh, bleh :)
 
  *
 
  * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
 
  *        othwerwise.
 
  *
 
  * @remarks Must be called at IRQL == DISPATCH_LEVEL
 
  *
 
  *--*/
 
  NTSTATUS
 
  NTAPI
 
  SomeAPI( ...
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== Astyle for kernel code ==
 
== Astyle for kernel code ==
 
 
  astyle -b -c -y --style=ansi --mode=c $FILE
 
  astyle -b -c -y --style=ansi --mode=c $FILE
  
 
== Text Editors ==
 
== Text Editors ==
 
 
* Notepad (GvG), problem is tab always == 8 spaces
 
* Notepad (GvG), problem is tab always == 8 spaces
 
* Syn (w3seek), problem is tabs again
 
* Syn (w3seek), problem is tabs again

Revision as of 03:59, 2 July 2013

The ReactOS Project currently enforces very few rules for coding style.

3rd party code

ReactOS uses code from other projects (e.g., Wine). In these instances the following guidelines do not apply and the existing style should be used. This allows easier syncing of future changes.

Formatting

Formatting should be done separately from code changes. Patches or commits that only change formatting should indicate so (e.g., "Formatting, no code changes", [FORMATTING] label).

Indentation

One level of indentation is 4 columns. Spaces must be used.

Braces

Braces must be placed on their own lines. They must be at the same indentation level as the previous statement. Braces are optional if only one statement is enclosed in it, excepting when a comment is also present.

Incorrect:

if (foo()) {
    bar();
}

if (foo())
    /* Comment */
    bar();

Correct:

if (foo())
{
    bar();
}


if (foo())
    bar();


if (foo())
{
    /* Comment */
    bar();
}

Long lines

80 columns should be used as a guide for line length.

  • When a line with function call is less than 80 chars long, params should be written on the same line
  • When a line with function call is more than 80 chars long (because calling func has many params), it maybe wise to write params as column, writing comments for each param (when it is needed)

File structure

The usual source code file consists of the following parts (some sections can be omitted if they are empty):

  • The header (an example)
 /*
  * PROJECT:         ReactOS Kernel
  * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            ntoskrnl/ob/namespce.c
  * PURPOSE:         Manages all functions related to the Object Manager name-
  *                  space, such as finding objects or querying their names.
  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
  *                  Eric Kohl
  *                  Thomas Weidenmueller (w3seek@reactos.org)
  */
  • Includes section, which has all #include statements, and vital defines (like #define NDEBUG)
 /* INCLUDES ******************************************************************/
  • Defines section, for per-file defines
 /* DEFINES *******************************************************************/
  • Private (not exported, or used only within the scope of this file) functions
 /* PRIVATE FUNCTIONS ******************************************************/
  • Public functions (if it's a DLL, or a driver)
 /* PUBLIC FUNCTIONS **********************************************************/
  • Or just this way, if it's a standalone app
 /* FUNCTIONS *****************************************************************/
  • [OPTIONAL?]File ends with
 /* EOF */<NewLineCharacterOnYourPlatform>

The Rules

Example of standard API header, please don't make up your own until really necessary:

/*++
 * @name SomeAPI
 * @implemented NT4
 *
 * Do nothing for 500ms.
 *
 * @param SomeParameter
 *        Description of the parameter. Wrapped to more lines on ~70th
 *        column.
 *
 * @param YetAnotherParameter
 *        Bleh, bleh :)
 *
 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL
 *         othwerwise.
 *
 * @remarks Must be called at IRQL == DISPATCH_LEVEL
 *
 *--*/
NTSTATUS
NTAPI
SomeAPI(...)

Astyle for kernel code

astyle -b -c -y --style=ansi --mode=c $FILE

Text Editors

  • Notepad (GvG), problem is tab always == 8 spaces
  • Syn (w3seek), problem is tabs again
  • Eclipse (w3seek), like the same
  • Ultraedit on windows (hpoussin), indenting blank lines
  • vi on unix (tinus)
  • Crimson Editor (Ged) no known problems
  • VS 2k3 (Brandon), Don't know any problems. I like using tabs. Willing to change if need be.

See Also