Login procedures have been improved. Check this page for details.

Coding Style

From QtProject
(Difference between revisions)
Jump to: navigation, search
 
Line 48: Line 48:
* Always use a single space after a keyword and before a curly brace.
* Always use a single space after a keyword and before a curly brace.
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         if(foo){
         if(foo){
Line 56: Line 56:
         if (foo) {
         if (foo) {
         }
         }
-
</code>
+
</syntaxhighlight>
* For pointers or references, always use a single space between the type and ‘*’ or ‘&’, but no space between the ‘*’ or ‘&’ and the variable name.
* For pointers or references, always use a single space between the type and ‘*’ or ‘&’, but no space between the ‘*’ or ‘&’ and the variable name.
-
<code>
+
<syntaxhighlight>
         char *x;
         char *x;
         const QString &myString;
         const QString &myString;
         const char * const y = "hello";
         const char * const y = "hello";
-
</code>
+
</syntaxhighlight>
* Surround binary operators with spaces.
* Surround binary operators with spaces.
Line 70: Line 70:
* Avoid C-style casts when possible.
* Avoid C-style casts when possible.
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         char* blockOfMemory = (char* ) malloc(data.size());
         char* blockOfMemory = (char* ) malloc(data.size());
Line 76: Line 76:
         // Correct
         // Correct
         char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));
         char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));
-
</code>
+
</syntaxhighlight>
== Braces ==
== Braces ==
Line 82: Line 82:
* As a base rule, the left curly brace goes on the same line as the start of the statement:
* As a base rule, the left curly brace goes on the same line as the start of the statement:
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         if (codec)
         if (codec)
Line 91: Line 91:
         if (codec) {
         if (codec) {
         }
         }
-
</code>
+
</syntaxhighlight>
* Exception: Function implementations and class declarations always have the left brace on the start of a line:
* Exception: Function implementations and class declarations always have the left brace on the start of a line:
-
<code>
+
<syntaxhighlight>
         static void foo(int g)
         static void foo(int g)
         {
         {
Line 104: Line 104:
         {
         {
         };
         };
-
</code>
+
</syntaxhighlight>
* Use curly braces when the body of a conditional statement contains more than one line, and also if a single line statement is somewhat complex.
* Use curly braces when the body of a conditional statement contains more than one line, and also if a single line statement is somewhat complex.
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         if (address.isEmpty()) {
         if (address.isEmpty()) {
Line 124: Line 124:
         for (int i = 0; i < 10; ++i)
         for (int i = 0; i < 10; ++i)
             qDebug("%i", i);
             qDebug("%i", i);
-
</code>
+
</syntaxhighlight>
* Exception 1: Use braces also if the parent statement covers several lines / wraps
* Exception 1: Use braces also if the parent statement covers several lines / wraps
-
<code>
+
<syntaxhighlight>
         // Correct
         // Correct
         if (address.isEmpty() || !isValid()
         if (address.isEmpty() || !isValid()
Line 134: Line 134:
             return false;
             return false;
         }
         }
-
</code>
+
</syntaxhighlight>
* Exception 2: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines
* Exception 2: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         if (address.isEmpty())
         if (address.isEmpty())
Line 169: Line 169:
                 ...
                 ...
         }
         }
-
</code>
+
</syntaxhighlight>
* Use curly braces when the body of a conditional statement is empty
* Use curly braces when the body of a conditional statement is empty
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         while (a);
         while (a);
Line 179: Line 179:
         // Correct
         // Correct
         while (a) {}
         while (a) {}
-
</code>
+
</syntaxhighlight>
== Parentheses ==
== Parentheses ==
Line 185: Line 185:
* Use parentheses to group expressions:
* Use parentheses to group expressions:
-
<code>
+
<syntaxhighlight>
         // Wrong
         // Wrong
         if (a && b || c)
         if (a && b || c)
Line 197: Line 197:
         // Correct
         // Correct
         (a + b) & c
         (a + b) & c
-
</code>
+
</syntaxhighlight>
== Switch statements ==
== Switch statements ==
Line 204: Line 204:
* 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.
* 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.
-
<code>
+
<syntaxhighlight>
         switch (myEnum) {
         switch (myEnum) {
         case Value1:
         case Value1:
Line 217: Line 217:
             break;
             break;
         }
         }
-
</code>
+
</syntaxhighlight>
== Line breaks ==
== Line breaks ==
Line 224: Line 224:
* Commas go at the end of a broken line; operators start at the beginning of the new line. An operator at the end of the line is easy to not see if your editor is too narrow.
* Commas go at the end of a broken line; operators start at the beginning of the new line. An operator at the end of the line is easy to not see if your editor is too narrow.
-
<code>
+
<syntaxhighlight>
         // Correct
         // Correct
         if (longExpression
         if (longExpression
Line 236: Line 236:
             otherOtherLongExpression) {
             otherOtherLongExpression) {
         }
         }
-
</code>
+
</syntaxhighlight>
== Inheritance and the `virtual` keyword ==
== Inheritance and the `virtual` keyword ==

Latest revision as of 13:23, 4 November 2011

This is an overview of the coding conventions we use when writing Qt code.

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

Contents

Indentation

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

Declaring variables

  • Declare each variable on a separate line
  • Avoid short (e.g. “a”, “rbarr”, “nughdeget”) names whenever possible
  • 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
        // Wrong
        int a, b;
        char *c, *d;
 
        // Correct
        int height;
        int width;
        char *nameOfThis;
        char *nameOfThat;
  • 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
        // Wrong
        short Cntr;
        char ITEM_DELIM = '\t';
 
        // Correct
        short counter;
        char itemDelimiter = '\t';
  • Classes always start with an upper-case letter. Public classes start with a ‘Q’ (QRgb). Public functions most often start with a ‘q’ (qRgb).

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.
        // Wrong
        if(foo){
        }
 
        // Correct
        if (foo) {
        }
  • For pointers or references, always use a single space between the type and ‘*’ or ‘&’, but no space between the ‘*’ or ‘&’ and the variable name.
        char *x;
        const QString &myString;
        const char * const y = "hello";
  • Surround binary operators with spaces.
  • No space after a cast.
  • Avoid C-style casts when possible.
        // Wrong
        char* blockOfMemory = (char* ) malloc(data.size());
 
        // Correct
        char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));

Braces

  • As a base rule, the left curly brace goes on the same line as the start of the statement:
        // Wrong
        if (codec)
        {
        }
 
        // Correct
        if (codec) {
        }
  • Exception: Function implementations and class declarations always have the left brace on the start of a line:
        static void foo(int g)
        {
            qDebug("foo: %i", g);
        }
 
        class Moo
        {
        };
  • Use curly braces when the body of a conditional statement contains more than one line, and also if a single line statement is somewhat complex.
        // Wrong
        if (address.isEmpty()) {
            return false;
        }
 
        for (int i = 0; i < 10; ++i) {
            qDebug("%i", i);
        }
 
        // Correct
        if (address.isEmpty())
            return false;
 
        for (int i = 0; i < 10; ++i)
            qDebug("%i", i);
  • Exception 1: Use braces also if the parent statement covers several lines / wraps
        // Correct
        if (address.isEmpty() || !isValid()
            || !codec) {
            return false;
        }
  • Exception 2: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines
        // Wrong
        if (address.isEmpty())
            return false;
        else {
            qDebug("%s", qPrintable(address));
            ++it;
        }
 
        // Correct
        if (address.isEmpty()) {
            return false;
        } else {
            qDebug("%s", qPrintable(address));
            ++it;
        }
 
        // Wrong
        if (a)
            if (b)
                ...
            else
                ...
 
        // Correct
        if (a) {
            if (b)
                ...
            else
                ...
        }
  • Use curly braces when the body of a conditional statement is empty
        // Wrong
        while (a);
 
        // Correct
        while (a) {}

Parentheses

  • Use parentheses to group expressions:
        // Wrong
        if (a && b || c)
 
        // Correct
        if ((a && b) || c)
 
        // Wrong
        a + b & c
 
        // Correct
        (a + b) & c

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.
        switch (myEnum) {
        case Value1:
            doSomething();
            break;
        case Value2:
        case Value3:
            doSomethingElse();
            // fall through
        default:
            defaultHandling();
            break;
        }

Line breaks

  • Keep lines shorter than 100 characters; insert breaks if necessary.
  • Commas go at the end of a broken line; operators start at the beginning of the new line. An operator at the end of the line is easy to not see if your editor is too narrow.
        // Correct
        if (longExpression
            + otherLongExpression
            + otherOtherLongExpression) {
        }
 
        // Wrong
        if (longExpression +
            otherLongExpression +
            otherOtherLongExpression) {
        }

Inheritance and the `virtual` keyword

  • When reimplementing a virtual method, do not put the `virtual` keyword in the header file.

General exception

  • Feel free to break a rule if it makes your code look bad.
Personal tools
license
Attribution-ShareAlike 2.5 Unported