Qt Coding Style: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[Qt Coding Style Spanish|Spanish]] [[Qt Coding Style SimplifiedChinese|简体中文]] [[Qt Coding Style Russian|Русский]] [[Qt Coding Style Japanese|日本語]]
'''English''' [[Qt_Coding_Style_Spanish|Spanish]] [[Qt_Coding_Style_SimplifiedChinese|简体中文]] [[Qt_Coding_Style_Russian|Русский]] [[Qt_Coding_Style_Japanese|日本語]]


=Qt Coding Style=
[[Category:Developing_Qt]]


This is an overview of the low-level coding conventions we use when writing Qt code.<br /> See [[Coding-Conventions|Coding Conventions]] for the higher-level conventions.
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
 
= 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.


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.


==Indentation==
== Indentation ==


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


==Declaring variables==
== Declaring variables ==


* Declare each variable on a separate line
* Declare each variable on a separate line
Line 19: Line 23:
* Wait when declaring a variable until it is needed
* 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
<code><br /> // Wrong<br /> int a, b;<br /> char *c, *d;
* Avoid abbreviations
 
// Correct<br /> int height;<br /> int width;<br /> char *nameOfThis;<br /> char '''nameOfThat;<br /></code>
<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
 
<code><br /> // Wrong<br /> short Cntr;<br /> char ITEM_DELIM = '';
 
// Correct<br /> short counter;<br /> char itemDelimiter = '';<br /></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).
* Acronyms are camel-cased (e.g. QXmlStreamReader, not QXMLStreamReader).
* Acronyms are camel-cased (e.g. QXmlStreamReader, not QXMLStreamReader).


==Whitespace==
== Whitespace ==


* Use blank lines to group statements together where suited
* Use blank lines to group statements together where suited
Line 31: Line 41:
* 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:


* 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 /> // Wrong<br /> if(foo){<br /> }
 
// Correct<br /> if (foo) {<br /> }<br /></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:
 
<code><br /> char '''x;<br /> const QString &amp;myString;<br /> const char''' const y = &quot;hello&amp;quot;;<br /></code>


* Surround binary operators with spaces
* Surround binary operators with spaces
* No space after a cast
* No space after a cast
* Avoid C-style casts when possible
* Avoid C-style casts when possible
<code><br /> // Wrong<br /> char* blockOfMemory = (char* ) malloc(data.size());
// Correct<br /> char '''blockOfMemory = reinterpret_cast&amp;lt;char'''&gt;(malloc(data.size()));<br /></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:


==Braces==
<code><br /> // Wrong<br /> if (foo) bar();
 
// Correct<br /> if (foo)<br /> bar();<br /></code>
 
== 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:
* 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 /> }
// Correct<br /> if (codec) {<br /> } else {<br /> }<br /></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:


* Use curly braces only when the body of a conditional statement contains more than one line:
<code><br /> static void foo(int g)<br /> {<br /> qDebug(&quot;foo: %i&amp;quot;, g);<br /> }


* Exception 1: Use braces also if the parent statement covers several lines / wraps:
class Moo<br /> {<br /> };<br /></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:
* Use curly braces only when the body of a conditional statement contains more than one line:


* Use curly braces when the body of a conditional statement is empty
<code><br /> // Wrong<br /> if (address.isEmpty()) {<br /> return false;<br /> }


==Parentheses==
for (int i = 0; i &lt; 10; +''i) {<br /> qDebug(&quot;%i&amp;quot;, i);<br /> }
<br /> // Correct<br /> if (address.isEmpty())<br /> return false;
<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


* Use parentheses to group expressions:
// Correct<br /> (a + b) &amp; c<br /></code>


==Switch statements==
== Switch statements ==


* The case labels are in the same column as the switch
* 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.
* 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)==
<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>


* Do not put ‘else’ after jump statements:
== Jump statements (break, continue, return, and goto) ==


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


==Line breaks==
<code><br /> // Wrong<br /> if (thisOrThat)<br /> return;<br /> else<br /> somethingElse();
 
// Correct<br /> if (thisOrThat)<br /> return;<br /> somethingElse();<br /></code>
 
* 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
* 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.
* 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==
<code><br /> // Wrong<br /> if (longExpression +<br /> otherLongExpression +<br /> otherOtherLongExpression) {<br /> }
 
* When strictly following a rule makes your code look bad, feel free to break it


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


* [[:Category:Developing Qt|Developing_Qt]]
== General exception ==

Revision as of 14:14, 23 February 2015

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

[toc align_right="yes&quot; depth="2&quot;]

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
<br /> // Wrong<br /> int a, b;<br /> char *c, *d;

// Correct<br /> int height;<br /> int width;<br /> char *nameOfThis;<br /> char '''nameOfThat;<br />


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

<br /> // Wrong<br /> short Cntr;<br /> char ITEM_DELIM = '';

// Correct<br /> short counter;<br /> char itemDelimiter = '';<br />
  • 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:
<br /> // Wrong<br /> if(foo){<br /> }

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

// Correct<br /> char '''blockOfMemory = reinterpret_cast&amp;lt;char'''&gt;(malloc(data.size()));<br />
  • Do not put multiple statements on one line
  • By extension, use a new line for the body of a control flow statement:
<br /> // Wrong<br /> if (foo) bar();

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

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

// Correct<br /> if (codec) {<br /> } else {<br /> }<br />
  • Exception: Function implementations and class declarations always have the left brace on the start of a line:
<br /> static void foo(int g)<br /> {<br /> qDebug(&quot;foo: %i&amp;quot;, g);<br /> }

class Moo<br /> {<br /> };<br />
  • Use curly braces only when the body of a conditional statement contains more than one line:
<br /> // Wrong<br /> if (address.isEmpty()) {<br /> return false;<br /> }

for (int i = 0; i &lt; 10; +''i) {<br /> qDebug(&quot;%i&amp;quot;, i);<br /> }
<br /> // Correct<br /> if (address.isEmpty())<br /> return false;
<br /> for (int i = 0; i &lt; 10;i)<br /> qDebug(&quot;%i&amp;quot;, i);<br />


* Exception 1: Use braces also if the parent statement covers several lines / wraps:


<br /> // Correct<br /> if (address.isEmpty() || !isValid()<br /> || !codec) {<br /> return false;<br /> }<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 /> // 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 />


* Use curly braces when the body of a conditional statement is empty


<br /> // Wrong<br /> while (a);
<br /> // Correct<br /> while (a) {}<br />


h2. Parentheses
* Use parentheses to group expressions:


<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 />

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.
<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 />

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

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

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

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

General exception