Editing Miniature/Policies

Warning: You are not logged in. Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 11: Line 11:
Note: the current code violates this style. We will fix it.
Note: the current code violates this style. We will fix it.
-
We mostly <s>try to follow the offical Qt Coding Guidelines, but with some exceptions.</s> follow mikhas' personal coding style for Qt:
+
We mostly try to follow the offical Qt Coding Guidelines, but with some exceptions.
* We use whitespaces over tabs.
* We use whitespaces over tabs.
* Max. length of a source code line is 120 characters.
* Max. length of a source code line is 120 characters.
Line 25: Line 25:
* Use Qt containers over std containers.
* Use Qt containers over std containers.
-
== Naming conventions ==
+
== Advanced Coding Style ==
-
 
+
-
* Methods *do* something, that's why their names should always contain a verb (slots and signals count as methods, too).
+
-
 
+
-
* typedef'd types *might* hint at their composed types (to emphasize their intended use), but avoid directly including the type information in the name:
+
-
 
+
-
  typedef QSharedPointer<SomeClass> SomeSharedClass; // Good! Sharing a resource is a concept.
+
-
  typedef QSharedPointer<SomeClass> SomeClassPtr; // Bad! What is a "SomeClassPtr *some_variable" now?
+
-
 
+
-
  typedef QList<SomeClass> SomeClassList; // still OK, since "List" is a concept, too.
+
-
 
+
-
* Avoid hungarian variable names.
+
-
 
+
-
== Advanced coding style ==
+
* Signal and slot connections should be easy to read, as they are a constant source of bugs:
* Signal and slot connections should be easy to read, as they are a constant source of bugs:
Line 65: Line 52:
* Use const refs for arguments when possible, even if the type is COW (copy-on-write, for example QString).
* Use const refs for arguments when possible, even if the type is COW (copy-on-write, for example QString).
-
 
-
* Conditionals
 
-
** One line before and after conditional blocks, to visually separate it from the surrounding code.
 
-
** It is OK for single-line conditional blocks to omit the braces (because of the extra newlines).
 
-
** && and || belong on the next line, if the condition needs to be wrapped (indented by 4 spaces). Put braces around complex conditions, to logically group them together (and to prevent operator precendence bugs, too):
 
-
 
-
  if ((something && somethingElse)
 
-
      || orPerhapsThis)
 
-
      ...
 
-
 
-
** Details:
 
-
*** Conditionals often contain bugs. Especially when the surrounding code gets refactored, those bugs simply slip in.
 
-
*** If a conditional needs to be wrapped, it starts to look ugly. Good! It might lead you to refactor the code, moving the condition itself into a predicate function (which has the nice effect to serve as a comment, if it has a good name).
 
-
*** Nested conditionals quickly start to look ugly with all those line separators, but you probably guessed it already: It's just another sign that this code might need some cleanup! Nested conditionals should be used rarely or hidden within dedicated methods.
 
-
 
-
* One space after keywords that could be mistaken as functions (if, for, ...).
 
* If you store a pointer to an object over which you do not intend to take ownership, then wrap this pointer in a QPointer:
* If you store a pointer to an object over which you do not intend to take ownership, then wrap this pointer in a QPointer:
Line 90: Line 61:
** Details:
** Details:
-
*** QPointers keep track of object deletion and set all referring QPointers to zero, preventing dangling pointers. Especially when using signals & slots, this is an often (and sometimes hard to spot!) source of bugs. With QPointers, you can wrap your code with conditional guards like so:
+
*** QPointers keep track of object deletion and set all referring QPointers to zero, preventing dangling pointers. Especially when using signals & slots, this is an often (and sometimes hard to spot!) source of bugs. With QPointers, you can wrap your code with guards like so:
   if (m_member)
   if (m_member)
   {
   {
       // Do sth with m_member object.
       // Do sth with m_member object.
-
       // This block *never* gets executed even if m_member was deleted elsewhere.
+
       // This block *never* gets executed if m_meber was deleted elsewhere.
   }
   }
* Assign 0 to pointers after deletion (or use QPointers).
* Assign 0 to pointers after deletion (or use QPointers).
* Always initialize type primitives (e.g., pointers) in the constructor's initializer list.
* Always initialize type primitives (e.g., pointers) in the constructor's initializer list.
-
* Don't use forward declaration unless necessary. It is not your task to optimize the compiler. Also, Qt Creator gets confused by them.
 
== How to keep code testable (and hopefully, mostly bugfree) ==
== How to keep code testable (and hopefully, mostly bugfree) ==

Learn more about Contributing to the wiki.


Please note that all contributions to maemo.org wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see maemo.org wiki:Copyrights for details). Do not submit copyrighted work without permission!


Cancel | Editing help (opens in new window)