Qt Coding Style

From Qt Wiki
Revision as of 16:43, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

English Spanish 简体中文 Русский 日本語

Qt Coding Style

This is an overview of the low-level coding conventions we use when writing Qt code.
See Coding Conventions for the higher-level conventions.

The data has been gathered by mining the Qt sources, discussion forums, email threads and through collaboration of the developers.

Indentation

  • 4 spaces are used for indentation
  • Spaces, not tabs!

Declaring variables

  • Declare each variable on a separate line
  • Avoid short or meaningless names (e.g. “a”, “rbarr”, “nughdeget”)
  • Single character variable names are only okay for counters and temporaries, where the purpose of the variable is obvious
  • Wait when declaring a variable until it is needed
  • Variables and functions start with a lower-case letter. Each consecutive word in a variable’s name starts with an upper-case letter
  • Avoid abbreviations
  • Classes always start with an upper-case letter. Public classes start with a ‘Q’ (QRgb) followed by an upper case letter. Public functions most often start with a ‘q’ (qRgb).
  • Acronyms are camel-cased (e.g. QXmlStreamReader, not QXMLStreamReader).

Whitespace

  • Use blank lines to group statements together where suited
  • Always use only one blank line
  • Always use a single space after a keyword and before a curly brace:
  • For pointers or references, always use a single space between the type and ‘*’ or ‘&’, but no space between the ‘*’ or ‘&’ and the variable name:
  • Surround binary operators with spaces
  • No space after a cast
  • Avoid C-style casts when possible
  • Do not put multiple statements on one line
  • By extension, use a new line for the body of a control flow statement:

Braces

  • Use attached braces: The opening brace goes on the same line as the start of the statement. If the closing brace is followed by another keyword, it goes into the same line as well:
  • Exception: Function implementations and class declarations always have the left brace on the start of a line:
  • Use curly braces only when the body of a conditional statement contains more than one line:
  • Exception 1: Use braces also if the parent statement covers several lines / wraps:
  • Exception 2: Brace symmetry: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines:
  • Use curly braces when the body of a conditional statement is empty

Parentheses

  • Use parentheses to group expressions:

Switch statements

  • The case labels are in the same column as the switch
  • Every case must have a break (or return) statement at the end or a comment to indicate that there’s intentionally no break, unless another case follows immediately.

Jump statements (break, continue, return, and goto)

  • Do not put ‘else’ after jump statements:
  • Exception: If the code is inherently symmetrical, use of ‘else’ is allowed to visualize that symmetry

Line breaks

  • Keep lines shorter than 100 characters; wrap if necessary
  • Commas go at the end of wrapped lines; operators start at the beginning of the new lines. An operator at the end of the line is easy to miss if the editor is too narrow.

General exception

  • When strictly following a rule makes your code look bad, feel free to break it

Categories: