Qt Coding Style: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
[[Category:Developing_Qt]]
[[Category:Developing_Qt]]


[toc align_right="yes" depth="2"]
[toc align_right="yes" depth="2"]


= Qt Coding Style =
= Qt Coding Style =


This is an overview of the low-level coding conventions we use when writing Qt code.<br />See [[Coding-Conventions]] for the higher-level conventions.
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.
The data has been gathered by mining the Qt sources, discussion forums, email threads and through collaboration of the developers.
Line 23: Line 24:
* Wait when declaring a variable until it is needed
* Wait when declaring a variable until it is needed


<code><br /> // Wrong<br /> int a, b;<br /> char *c, *d;
<code>
// Wrong
int a, b;
char *c, *d;


// Correct<br /> int height;<br /> int width;<br /> char *nameOfThis;<br /> char '''nameOfThat;<br /></code>
// Correct
<br />''' Variables and functions start with a lower-case letter. Each consecutive word in a variable’s name starts with an upper-case letter<br />* Avoid abbreviations
int height;
int width;
char *nameOfThis;
char '''nameOfThat;
</code>


<code><br /> // Wrong<br /> short Cntr;<br /> char ITEM_DELIM = '';
''' 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


// Correct<br /> short counter;<br /> char itemDelimiter = '';<br /></code>
<code>
// Wrong
short Cntr;
char ITEM_DELIM = '';
 
// Correct
short counter;
char itemDelimiter = '';
</code>


* 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).
* 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).
Line 41: Line 58:
* 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><br /> // Wrong<br /> if(foo){<br /> }
<code>
// Wrong
if(foo){
}


// Correct<br /> if (foo) {<br /> }<br /></code>
// Correct
if (foo) {
}
</code>


* For pointers or references, always use a single space between the type and ‘'''’ or ‘&amp;’, but no space between the ‘'''’ or ‘&amp;’ and the variable name:
* For pointers or references, always use a single space between the type and ‘'''’ or ‘&amp;’, but no space between the ‘'''’ or ‘&amp;’ and the variable name:


<code><br /> char '''x;<br /> const QString &amp;myString;<br /> const char''' const y = &quot;hello&amp;quot;;<br /></code>
<code>
char '''x;
const QString &amp;myString;
const char''' const y = "hello";
</code>


* Surround binary operators with spaces
* Surround binary operators with spaces
Line 53: Line 80:
* Avoid C-style casts when possible
* Avoid C-style casts when possible


<code><br /> // Wrong<br /> char* blockOfMemory = (char* ) malloc(data.size());
<code>
// Wrong
char* blockOfMemory = (char* ) malloc(data.size());


// Correct<br /> char '''blockOfMemory = reinterpret_cast&amp;lt;char'''&gt;(malloc(data.size()));<br /></code>
// Correct
char '''blockOfMemory = reinterpret_cast<char'''>(malloc(data.size()));
</code>


* Do not put multiple statements on one line
* Do not put multiple statements on one line
* By extension, use a new line for the body of a control flow statement:
* By extension, use a new line for the body of a control flow statement:


<code><br /> // Wrong<br /> if (foo) bar();
<code>
// Wrong
if (foo) bar();


// Correct<br /> if (foo)<br /> bar();<br /></code>
// Correct
if (foo)
bar();
</code>


== Braces ==
== Braces ==
Line 68: Line 104:
* 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:
* 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:


<code><br /> // Wrong<br /> if (codec)<br /> {<br /> }<br /> else<br /> {<br /> }
<code>
// Wrong
if (codec)
{
}
else
{
}


// Correct<br /> if (codec) {<br /> } else {<br /> }<br /></code>
// Correct
if (codec) {
} else {
}
</code>


* 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><br /> static void foo(int g)<br /> {<br /> qDebug(&quot;foo: %i&amp;quot;, g);<br /> }
<code>
static void foo(int g)
{
qDebug("foo: %i", g);
}


class Moo<br /> {<br /> };<br /></code>
class Moo
{
};
</code>


* Use curly braces only when the body of a conditional statement contains more than one line:
* Use curly braces only when the body of a conditional statement contains more than one line:


<code><br /> // Wrong<br /> if (address.isEmpty()) {<br /> return false;<br /> }
<code>
// 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);
</code>
 
* Exception 1: Use braces also if the parent statement covers several lines / wraps:
 
<code>
// Correct
if (address.isEmpty() || !isValid()
|| !codec) {
return false;
}
</code>
 
* Exception 2: Brace symmetry: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines:
 
<code>
// 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
}
</code>
 
* Use curly braces when the body of a conditional statement is empty
 
<code>
// Wrong
while (a);
 
// Correct
while (a) {}
</code>
 
h2. Parentheses
 
* Use parentheses to group expressions:
 
<code>
// Wrong
if (a &amp;&amp; b || c)
 
// Correct
if ((a &amp;&amp; b) || c)


for (int i = 0; i &lt; 10; +''i) {<br /> qDebug(&quot;%i&amp;quot;, i);<br /> }
// Wrong
<br /> // Correct<br /> if (address.isEmpty())<br /> return false;
a'' b &amp; c
<br /> for (int i = 0; i &lt; 10;i)<br /> qDebug(&quot;%i&amp;quot;, i);<br /></code>
<br />* Exception 1: Use braces also if the parent statement covers several lines / wraps:
<br /><code><br /> // Correct<br /> if (address.isEmpty() || !isValid()<br /> || !codec) {<br /> return false;<br /> }<br /></code>
<br />* Exception 2: Brace symmetry: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines:
<br /><code><br /> // Wrong<br /> if (address.isEmpty())<br /> return false;<br /> else {<br /> qDebug(&quot;%s&amp;quot;, qPrintable(address));<br />it;<br /> }
<br /> // Correct<br /> if (address.isEmpty()) {<br /> return false;<br /> } else {<br /> qDebug(&quot;%s&amp;quot;, qPrintable(address));<br />it;<br /> }
<br /> // Wrong<br /> if (a)<br /> if (b)<br /> …<br /> else<br /> …
<br /> // Correct<br /> if (a) {<br /> if (b)<br /> …<br /> else<br /> …<br /> }<br /></code>
<br />* Use curly braces when the body of a conditional statement is empty
<br /><code><br /> // Wrong<br /> while (a);
<br /> // Correct<br /> while (a) {}<br /></code>
<br />h2. Parentheses
<br />* Use parentheses to group expressions:
<br /><code><br /> // Wrong<br /> if (a &amp;&amp; b || c)
<br /> // Correct<br /> if ((a &amp;&amp; b) || c)
<br /> // Wrong<br /> a'' b &amp; c


// Correct<br /> (a + b) &amp; c<br /></code>
// Correct
(a + b) &amp; c
</code>


== Switch statements ==
== Switch statements ==
Line 108: Line 230:
* 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><br /> switch (myEnum) {<br /> case Value1:<br /> doSomething();<br /> break;<br /> case Value2:<br /> case Value3:<br /> doSomethingElse();<br /> // fall through<br /> default:<br /> defaultHandling();<br /> break;<br /> }<br /></code>
<code>
switch (myEnum) {
case Value1:
doSomething();
break;
case Value2:
case Value3:
doSomethingElse();
// fall through
default:
defaultHandling();
break;
}
</code>


== Jump statements (break, continue, return, and goto) ==
== Jump statements (break, continue, return, and goto) ==
Line 114: Line 249:
* Do not put 'else' after jump statements:
* Do not put 'else' after jump statements:


<code><br /> // Wrong<br /> if (thisOrThat)<br /> return;<br /> else<br /> somethingElse();
<code>
// Wrong
if (thisOrThat)
return;
else
somethingElse();


// Correct<br /> if (thisOrThat)<br /> return;<br /> somethingElse();<br /></code>
// Correct
if (thisOrThat)
return;
somethingElse();
</code>


* Exception: If the code is inherently symmetrical, use of 'else' is allowed to visualize that symmetry
* Exception: If the code is inherently symmetrical, use of 'else' is allowed to visualize that symmetry
Line 125: Line 269:
* 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.
* 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.


<code><br /> // Wrong<br /> if (longExpression +<br /> otherLongExpression +<br /> otherOtherLongExpression) {<br /> }
<code>
// Wrong
if (longExpression +
otherLongExpression +
otherOtherLongExpression) {
}


// Correct<br /> if (longExpression<br /> + otherLongExpression<br /> + otherOtherLongExpression) {<br /> }<br /></code>
// Correct
if (longExpression
+ otherLongExpression
+ otherOtherLongExpression) {
}
</code>


== General exception ==
== General exception ==

Revision as of 08:38, 25 February 2015

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

[toc align_right="yes" depth="2"]

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
 // 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 = '';

// Correct
 short counter;
 char itemDelimiter = '';
  • 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:
 // 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 &amp;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()));
  • Do not put multiple statements on one line
  • By extension, use a new line for the body of a control flow statement:
 // Wrong
 if (foo) bar();

// Correct
 if (foo)
 bar();

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:
 // Wrong
 if (codec)
 {
 }
 else
 {
 }

// Correct
 if (codec) {
 } else {
 }
  • 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 only when the body of a conditional statement contains more than one line:
 // 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: Brace symmetry: 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) {}

h2. Parentheses

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

 // Correct
 if ((a &amp;&amp; b) || c)

 // Wrong
 a'' b &amp; c

// Correct
 (a + b) &amp; 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;
 }

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

  • Do not put 'else' after jump statements:
 // Wrong
 if (thisOrThat)
 return;
 else
 somethingElse();

// Correct
 if (thisOrThat)
 return;
 somethingElse();
  • 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.
 // Wrong
 if (longExpression +
 otherLongExpression +
 otherOtherLongExpression) {
 }

// Correct
 if (longExpression
 + otherLongExpression
 + otherOtherLongExpression) {
 }

General exception