<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://olddev.minetest.org/index.php?action=history&amp;feed=atom&amp;title=Code_style_guidelines</id>
	<title>Code style guidelines - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://olddev.minetest.org/index.php?action=history&amp;feed=atom&amp;title=Code_style_guidelines"/>
	<link rel="alternate" type="text/html" href="https://olddev.minetest.org/index.php?title=Code_style_guidelines&amp;action=history"/>
	<updated>2026-04-15T19:09:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.38.7</generator>
	<entry>
		<id>https://olddev.minetest.org/index.php?title=Code_style_guidelines&amp;diff=15&amp;oldid=prev</id>
		<title>&gt;Sfan5 at 13:43, 26 February 2022</title>
		<link rel="alternate" type="text/html" href="https://olddev.minetest.org/index.php?title=Code_style_guidelines&amp;diff=15&amp;oldid=prev"/>
		<updated>2022-02-26T13:43:08Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This is the coding style used for C/C++ code. Also see the [[Lua code style guidelines]].&lt;br /&gt;
&lt;br /&gt;
The coding style is based on the [https://www.kernel.org/doc/html/latest/process/coding-style.html Linux kernel code style]. Much of the existing code doesn't follow the current code style guidelines, do not try to replicate that. Use your best judgment for C++-specific syntax.&lt;br /&gt;
&lt;br /&gt;
Currently, the code uses C++14. Do not use features that depend on more recent versions.&lt;br /&gt;
&lt;br /&gt;
=== Spelling ===&lt;br /&gt;
&lt;br /&gt;
Use American English, but avoid idioms that may be difficult to understand by non-native speakers.&lt;br /&gt;
&lt;br /&gt;
=== Function declarations ===&lt;br /&gt;
&lt;br /&gt;
In case your function parameters don't fit within the defined line length, use the following style.&lt;br /&gt;
Indention for continuation lines is '''exactly''' two tabs.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void some_function_name(type1 param1, type2 param2, type3 param3,&lt;br /&gt;
		type4 param4, type5 param5, type6 param6, type7 param7)&lt;br /&gt;
{&lt;br /&gt;
	...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes with complex function declarations, it might be messy to define as many parameters as possible on the same line.&lt;br /&gt;
This is acceptable too (and currently used in some places):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
void some_function_name(&lt;br /&gt;
		const ReallyBigLongTypeName &amp;amp;param1,&lt;br /&gt;
		ReallyBigLongTypeName *param2,&lt;br /&gt;
		void *param3,&lt;br /&gt;
		size_t param4,&lt;br /&gt;
		const void *param5,&lt;br /&gt;
		size_t param6)&lt;br /&gt;
{&lt;br /&gt;
	...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
No more than 7 parameters allowed (except for constructors).&lt;br /&gt;
&lt;br /&gt;
=== Spaces ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;Do '''not''' use spaces to indent.&amp;lt;/span&amp;gt;&lt;br /&gt;
* Try to stay under 6 levels of indentation.&lt;br /&gt;
* Add spaces between operators so they line up when appropriate (don't go overboard). For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
np_terrain_base   = settings-&amp;gt;getNoiseParams(&amp;quot;mgv6_np_terrain_base&amp;quot;);&lt;br /&gt;
np_terrain_higher = settings-&amp;gt;getNoiseParams(&amp;quot;mgv6_np_terrain_higher&amp;quot;);&lt;br /&gt;
np_steepness      = settings-&amp;gt;getNoiseParams(&amp;quot;mgv6_np_steepness&amp;quot;);&lt;br /&gt;
np_height_select  = settings-&amp;gt;getNoiseParams(&amp;quot;mgv6_np_height_select&amp;quot;);&lt;br /&gt;
...&lt;br /&gt;
bool success =&lt;br /&gt;
		np_terrain_base  &amp;amp;&amp;amp; np_terrain_higher &amp;amp;&amp;amp; np_steepness &amp;amp;&amp;amp;&lt;br /&gt;
		np_height_select &amp;amp;&amp;amp; np_trees          &amp;amp;&amp;amp; np_mud       &amp;amp;&amp;amp;&lt;br /&gt;
		np_beach         &amp;amp;&amp;amp; np_biome          &amp;amp;&amp;amp; np_cave;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above code looks really nice.&lt;br /&gt;
* Separate different parts of functions with newlines for readability.&lt;br /&gt;
* Separate functions by two newlines (not necessary, but encouraged).&lt;br /&gt;
* Use a space after &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;for&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;do&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;while&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;case&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;try&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;catch&amp;lt;/code&amp;gt;, etc.&lt;br /&gt;
* When breaking conditionals, indent following lines of the conditional with two tabs and the statement body with one tab. For example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (std::vector&amp;lt;std::string&amp;gt;::iterator it = strings.begin();&lt;br /&gt;
		it != strings.end();&lt;br /&gt;
		++it) {&lt;br /&gt;
	*it = it-&amp;gt;substr(1, 1);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Align backslashes for multi-line macros with spaces:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#define FOOBAR(x) do {    \&lt;br /&gt;
	int __temp = (x); \&lt;br /&gt;
	foo(__temp);      \&lt;br /&gt;
} while (0)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Bracing and indentation ===&lt;br /&gt;
==== &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statements ====&lt;br /&gt;
This rule has already been explicitly stated in the [https://www.kernel.org/doc/html/latest/process/coding-style.html Linux kernel code style] from which this code style inherits, but it will be repeated here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;'''Putting the body of an &amp;lt;code&amp;gt;if&amp;lt;/code&amp;gt; statement on the same line as the condition is strictly prohibited.'''&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (foobar &amp;lt; 3) foobar = 45;      // Bad &lt;br /&gt;
(foobar &amp;lt; 3 &amp;amp;&amp;amp; (foobar = 45));    // Bad&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Violating this rule will result in '''instant rejection'''.&lt;br /&gt;
&lt;br /&gt;
Examples of good if statement wordings:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
if (foobar &amp;lt; 3)&lt;br /&gt;
	foobar = 45;&lt;br /&gt;
&lt;br /&gt;
if (foobar &amp;lt; 6) {&lt;br /&gt;
	foobar = 62;&lt;br /&gt;
	return;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Nested for loop exception ====&lt;br /&gt;
Special exception to the standard bracing/indent rules for nested loops:&lt;br /&gt;
If a nested loop iterates over a set of coordinates, it is permitted to omit the braces for all but the innermost loop and keep the outer loops at the same indentation level, like so:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
for (s16 z = pmin.Z; z &amp;lt;= pmax.Z, z++)&lt;br /&gt;
for (s16 y = pmin.Y; y &amp;lt;= pmax.Y; y++)&lt;br /&gt;
for (s16 x = pmin.X; x &amp;lt;= pmax.X; x++) {&lt;br /&gt;
	// ... do stuff here ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Do not be too C++y ===&lt;br /&gt;
&lt;br /&gt;
* Avoid passing non-&amp;lt;code&amp;gt;const&amp;lt;/code&amp;gt; references to functions.&lt;br /&gt;
* Don't use initializer lists unless absolutely necessary (initializing an object inside a class, or initializing a reference).&lt;br /&gt;
* Try to minimize the use of exceptions.&lt;br /&gt;
* Avoid operator overloading like the plague.&lt;br /&gt;
* Avoid templates unless they are very convenient.&lt;br /&gt;
* Usage of macros is not discouraged, just don't overdo it [http://cgit.freedesktop.org/xorg/xserver/tree/randr/rrscreen.c?id=01e18af17f8dc91451fbd0902049045afd1cea7e#n325 like X.org]. It's better to use inline functions or lambdas instead.&lt;br /&gt;
&lt;br /&gt;
=== Classes ===&lt;br /&gt;
&lt;br /&gt;
* '''Class names are ''PascalCase'', method names are ''camelCase''.'''&lt;br /&gt;
* Don't put actual code in header files, unless it's a 4-liner, an inline function, or part of a template.&lt;br /&gt;
* Class definitions should go in header files.&lt;br /&gt;
* Substantial methods (over 4 lines) should be defined outside of the class definition.&lt;br /&gt;
* Functions not part of any class should use &amp;lt;code&amp;gt;lowercase_underscore_style()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Comments ===&lt;br /&gt;
&lt;br /&gt;
* Doxygen comments are acceptable, but '''please''' put them in the header file.&lt;br /&gt;
* Don't make uninformative comments like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Draw &amp;quot;Loading&amp;quot; screen&lt;br /&gt;
draw_load_screen(L&amp;quot;Loading...&amp;quot;, driver, font);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Add comments to explain a non-trivial but important detail about the code, or explain behavior that is not obvious.&lt;br /&gt;
* For comments with text, be sure to add a space between the text and the comment tokens:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
DoThingHere();  // This does thing    &amp;lt;--- yes!&lt;br /&gt;
DoThingHere();  /* This does thing */ &amp;lt;--- yes!&lt;br /&gt;
&lt;br /&gt;
DoThingHere();  //This does thing      &amp;lt;--- no!&lt;br /&gt;
DoThingHere();  /*This does thing*/    &amp;lt;--- no!&lt;br /&gt;
DoThingHere();//This does thing        &amp;lt;--- no!&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Use STL, avoid Irrlicht containers, and no, Boost will not even be considered, so forget it ===&lt;br /&gt;
&lt;br /&gt;
* In general, adding new dependencies is considered serious business.&lt;br /&gt;
* We are using C++14; Boost will never be an option.&lt;br /&gt;
&lt;br /&gt;
=== Don't let things get too large ===&lt;br /&gt;
&lt;br /&gt;
* '''Try to keep lines under 95 characters.''' It's okay if it goes over by a few, but do not exaggerate. (Note that this column count assumes 4-space indents.)&lt;br /&gt;
* Functions should not have over 200 lines of code – if you are concerned with having to pass too many parameters to child functions, make whatever it is into a class.&lt;br /&gt;
* Don't let files get too large (over 1500 lines of code). Currently, existing huge files (&amp;lt;code&amp;gt;game.cpp&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;server.cpp&amp;lt;/code&amp;gt;, …) are in the slow process of being cleaned up.&lt;br /&gt;
&lt;br /&gt;
=== Files ===&lt;br /&gt;
&lt;br /&gt;
* Files should be named using ''snake_case'' style.&lt;br /&gt;
* Files should have includes for everything that they depend on. Don't depend on, eg, &amp;lt;code&amp;gt;&amp;quot;util/numeric.h&amp;quot;&amp;lt;/code&amp;gt; including &amp;lt;code&amp;gt;&amp;lt;string&amp;gt;&amp;lt;/code&amp;gt;!&lt;br /&gt;
* Uniqueness when compiling headers is ensured by using &amp;lt;code&amp;gt;#pragma once&amp;lt;/code&amp;gt;. ([https://github.com/minetest/minetest/issues/6259 Accepted by all coredevs])&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#pragma once&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class Foo {&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* All files should include the appropriate license header.&lt;br /&gt;
&lt;br /&gt;
=== Miscellaneous ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;Do '''not''' use &amp;lt;code&amp;gt;or&amp;lt;/code&amp;gt;, use &amp;lt;code&amp;gt;||&amp;lt;/code&amp;gt;.&amp;lt;/span&amp;gt;&lt;br /&gt;
* Set pointer values to &amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt; (C++11), not 0.&lt;br /&gt;
* When using floats, add the &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; suffix, e.g. &amp;lt;code&amp;gt;float k = 0.0f;&amp;lt;/code&amp;gt; and not &amp;lt;code&amp;gt;float k = 0.0;&amp;lt;/code&amp;gt;.&lt;br /&gt;
* Avoid non-ASCII characters in source files. Other UTF-8 characters may (only) be used in string literals and comments where ASCII would worsen readability.&lt;br /&gt;
* Use of Hungarian notation is very limited. Scope specifiers such as &amp;lt;code&amp;gt;g_&amp;lt;/code&amp;gt; for globals, &amp;lt;code&amp;gt;s_&amp;lt;/code&amp;gt; for statics, or &amp;lt;code&amp;gt;m_&amp;lt;/code&amp;gt; for members are allowed. The prefix &amp;lt;code&amp;gt;m_&amp;lt;/code&amp;gt; is discouraged for public members in newer code as it is a part of the class' interface, but sometimes needed for consistency when adding a member to older code.&lt;br /&gt;
* Use ''snake_case'' for local variables, not ''camelCase''.&lt;br /&gt;
* Don't use distracting and unnecessary amounts of object-oriented abstraction. See [https://github.com/MovingBlocks/Terasology Terasology] as an example of what not to do.&lt;br /&gt;
** Don't add unnecessary design patterns to your code, such as factories/providers/sources.&lt;br /&gt;
* In &amp;lt;code&amp;gt;switch-case&amp;lt;/code&amp;gt; statements, add &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; to the last case and to the &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; case.&lt;br /&gt;
* In &amp;lt;code&amp;gt;if-else&amp;lt;/code&amp;gt; statements, put the code which is more likely to be executed first.&lt;br /&gt;
* For consistency, use American English where spellings differ (e.g. use &amp;quot;color&amp;quot;, not &amp;quot;colour&amp;quot;).&lt;br /&gt;
&lt;br /&gt;
[[Category:Core Engine]]&lt;br /&gt;
[[Category:Rules and Guidelines]]&lt;/div&gt;</summary>
		<author><name>&gt;Sfan5</name></author>
	</entry>
</feed>