The only explicitly allowed exception to the 50-72 rules is that if the first line can be MDEV-###### title', even if the title would make the line longer than 50 characters.
The commit messages are typically rendered in [Markdown format](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax), so markdown formatting is permitted for the message body.
Everyone has a preferred coding style, there is no real correct style for all projects around the world.
What is important is that we stick to one common style throughout this code base.
### Indentation
We should use a variant of the [Allman indentation style](https://en.wikipedia.org/wiki/Indentation_style#Allman_style).
The variation is to use two spaces instead of tabs and has a couple of minor rule changes as below.
Allman style specifies that braces associated with a statement should be on the following line with the same indentation and the statements inside the braces are next level indented.
The closing braces are also on a new line at the same indentation as the original statement.
For example:
```cpp
while (x == y)
{
something();
somethingelse();
}
finalthing();
```
#### Switch / Case statements
For switch / case statements the `case` needs to be inline with the `switch`.
Compiler preprocessor directives should have no indentation to them, even if in the middle of indented code.
For example:
```c
case SSL_TYPE_NONE: // SSL is not required
if (opt_require_secure_transport)
{
enum enum_vio_type type= vio_type(vio);
#ifdef HAVE_OPENSSL
return type != VIO_TYPE_SSL &&
#ifndef _WIN32
type != VIO_TYPE_SOCKET;
#else
type != VIO_TYPE_NAMEDPIPE;
#endif
#else
#ifndef _WIN32
return type != VIO_TYPE_SOCKET;
#else
return type != VIO_TYPE_NAMEDPIPE;
#endif
#endif
}
```
Comments reflecting the original `#if` condition can be appended to `#else` / `#endif` to provide additional clarity. This can be useful for large code blocks between the start and end preprocessor directives or nested preprocessor directives.
Single line / inline code comments can use the double slash (`//`) style of coding, whereas multi-line code comments should use `/*` as a start and `*/` at the end, with the text indented by 2 spaces, for example:
```cpp
/*
This is a multi-line code comment.
It has an indentation of two spaces.
*/
```
### Variables classes, and functions
Variables and functions should be descriptive and in "snake case", for example:
```cpp
void my_function(uint16 variable_name)
{
```
Class names should also be "snake case" but should start with an upper-case character.
Such as this:
```cpp
class Buffered_logs
{
```
Assignments should not have a space on the left side of the equals, and one space on the right hand side. For example:
```cpp
a= 1; // Correct
a = 1; // Incorrect for the server code,
// ok for Storage Engines if they use it (aka Connect)
Alternatively the integer can be defined using an `enum` or `#define`.
### Spacing
#### Whitespace
* Lines should not have any trailing whitespace.
* There should not be any trailing blank lines at the end of a file.
* Line endings are POSIX style (`\n`).
* Two spaces for each indentation level, not tabs.
#### Pointers
The `*` of a pointer should be on the side of the variable name such as:
```cpp
void my_function(THD *thd)
{
```
As yet there is no standard as to whether the `*` in a casting should have a space or not.
Both of these are valid:
```cpp
name= (const char*)db_name;
name= (const char *) db_name;
```
#### Function variables
There should be a space after each comma in a definition and usage of a function.
For example:
```cpp
my_function(thd, db_name);
```
### Types
In general the usage of types such as `char`, `int` and `long` should be discouraged but there are shortened versions of the unsigned variants available for these in `my_global.h`.
They can be different sizes across platforms and `char` can be either unsigned or signed depending on platform, and therefore are not portable.
Instead these should be used as appropriate:
* 8-bit signed / unsigned int -> `int8` / `uint8`
* 16-bit signed / unsigned int -> `int16` / `uint16`
* 32-bit signed / unsigned int -> `int32` / `uint32`
* 64-bit signed / unsigned int -> `int64` / `uint64`
* Integer file descriptor -> `File`
* Integer socket descriptor -> `my_socket`
`size_t` and `ptrdiff_t` are used in the source where appropriate, buffer sizes for example.
It should be noted that these are implementation dependent but are useful when used in the correct context.
Further types can be found in the `include/` directory files.
There are also general utility functions in `mysys`.