MySQL Internals Manual  /  ...  /  Indentation and Spacing

2.1.3 Indentation and Spacing

  • For indentation use space; do not use the tab (\t) character. See the editor configuration tips at the end of this section for instructions on configuring a vim or emacs editor to use spaces instead of tabs.

  • Avoid trailing whitespace, in code and comments.


if (a) 


if (a)<SP><SP><TAB><SP> 

Remove trailing spaces if you are already changing a line, otherwise leave existing code intact.

  • Use line feed (\n) for line breaks. Do not use carriage return + line feed (\r\n); that can cause problems for other users and for builds. This rule is particularly important if you use a Windows editor.

  • To begin indenting, add two spaces. To end indenting, subtract two spaces. For example:

  code, code, code
    code, code, code
  • An exception to the rule above: namespaces (named or unnamed) do not introduce a new level of indentation. Example:

namespace foo
class Bar
}  // namespace foo
  • The maximum line width is 80 characters. If you are writing a longer line, try to break it at a logical point and continue on the next line with the same indenting. Use of backslash is okay; however, multi-line literals might cause less confusion if they are defined before the function start.

  • You may use empty lines (two line breaks in a row) wherever it seems helpful for readability. But never use two or more empty lines in a row. The only exception is after a function definition (see below).

  • To separate two functions, use three line breaks (two empty lines). To separate a list of variable declarations from executable statements, use two line breaks (one empty line). For example:

int function_1()
  int i;
  int j;


int function2()
  • Matching '{}' (left and right braces) should be in the same column, that is, the closing '}' should be directly below the opening '{'. Do not put any non-space characters on the same line as a brace, not even a comment. Indent within braces. Exception: if there is nothing between two braces, that is, '{}', they should appear together. For example:

if (code, code, code)
  code, code, code;
for (code, code, code)
  • Indent switch like this:

switch (condition)
case XXX:
case YYY:
  • You may align variable declarations like this:

Type      value;
int       var2;
ulonglong var3;
  • When assigning to a variable, put zero spaces after the target variable name, then the assignment operator ('=''+=' etc.), then space(s). For single assignments, there should be only one space after the equal sign. For multiple assignments, add additional spaces so that the source values line up. For example:

a/= b;
return_value= my_function(arg1);
int x=          27;
int new_var=    18;

Align assignments from one structure to another, like this:

foo->member=      bar->member;
foo->name=        bar->name;
foo->name_length= bar->name_length;
  • Put separate statements on separate lines. This applies for both variable declarations and executable statements. For example, this is wrong:

int x= 11; int y= 12;

z= x; y+= x;

This is right:

int x= 11;
int y= 12;

z= x;
y+= x;
  • Put spaces both before and after binary comparison operators ('>', '==', '>=', etc.), binary arithmetic operators ('+' etc.), and binary Boolean operators ('||' etc.). Do not put spaces around unary operators like '!' or '++'. Do not put spaces around [de-]referencing operators like '->' or '[]'. Do not put space after '*' when '*' introduces a pointer. Do not put spaces after '('. Put one space after ')' if it ends a condition, but not if it ends a list of function arguments. For example:

int *var;

if ((x == y + 2) && !param->is_signed)
  • When a function has multiple arguments separated by commas (','), put one space after each comma. For example:

ln= mysql_bin_log.generate_name(opt_bin_logname, "-bin", 1, buf); 
  • Put one space after a keyword which introduces a condition, such as if or for or while.

  • After if or else or while, when there is only one instruction after the condition, braces are not necessary and the instruction goes on the next line, indented.

if (sig != MYSQL_KILL_SIGNAL && sig != 0)
while (*val && my_isspace(mysqld_charset, *val))
  • In function declarations and invocations: there is no space between function name and '('; there is no space or line break between '(' and the first argument; if the arguments do not fit on one line then align them. Examples:

Return_value_type *Class_name::method_name(const char *arg1,
                                           size_t arg2, Type *arg3)
return_value= function_name(argument1, argument2, long_argument3,
  long_long_function_name(long_long_argument1, long_long_argument2,
Long_long_return_value_type *
long_long_method_name(const char *long_long_arg1, size_t long_long_arg2,
                      Long_long_type *arg3)

(You may but don't have to split Class_name::method_name into two lines.) When arguments do not fit on one line, consider renaming them.

  • Format constructors in the following way:

Item::Item(int a_arg, int b_arg, int c_arg)
  :a(a_arg), b(b_arg), c(c_arg)

But keep lines short to make them more readable:

Item::Item(int longer_arg, int more_longer_arg)

If a constructor can fit into one line:

Item::Item(int a_arg) :a(a_arg) {} 

User Comments
Sign Up Login You must be logged in to post a comment.