<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="http://bugs.maemo.com/skins/common/feed.css?207"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://bugs.maemo.com/index.php?action=history&amp;feed=atom&amp;title=Miniature%2FDevelopment%2FCodingStyle</id>
		<title>Miniature/Development/CodingStyle - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://bugs.maemo.com/index.php?action=history&amp;feed=atom&amp;title=Miniature%2FDevelopment%2FCodingStyle"/>
		<link rel="alternate" type="text/html" href="http://bugs.maemo.com/index.php?title=Miniature/Development/CodingStyle&amp;action=history"/>
		<updated>2026-04-04T14:35:24Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.15.5-7</generator>

	<entry>
		<id>http://bugs.maemo.com/index.php?title=Miniature/Development/CodingStyle&amp;diff=45135&amp;oldid=prev</id>
		<title>mikhas:&amp;#32;/* General */</title>
		<link rel="alternate" type="text/html" href="http://bugs.maemo.com/index.php?title=Miniature/Development/CodingStyle&amp;diff=45135&amp;oldid=prev"/>
				<updated>2011-09-03T11:11:32Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;General&lt;/span&gt;&lt;/p&gt;

		&lt;table style=&quot;background-color: white; color:black;&quot;&gt;
		&lt;col class='diff-marker' /&gt;
		&lt;col class='diff-content' /&gt;
		&lt;col class='diff-marker' /&gt;
		&lt;col class='diff-content' /&gt;
		&lt;tr valign='top'&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;← Older revision&lt;/td&gt;
		&lt;td colspan='2' style=&quot;background-color: white; color:black;&quot;&gt;Revision as of 11:11, 3 September 2011&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 7:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Type names are in CamelCase&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Type names are in CamelCase&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Method names are in mixedCase (= lowerCamelCase).&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Method names are in mixedCase (= lowerCamelCase).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;-&lt;/td&gt;&lt;td style=&quot;background: #ffa; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Opening braces for code blocks &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;after &lt;/del&gt;newlines, *except* opening braces for classes and methods.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;background: #cfc; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Opening braces for code blocks &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;before &lt;/ins&gt;newlines, *except* opening braces for classes and methods.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Variables use under_scores and are lower_cases, to make them stand out from other symbols.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;* Variables use under_scores and are lower_cases, to make them stand out from other symbols.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;** Member variables are prefixed with &amp;quot;m_*&amp;quot; if there is no private data class (PIMPL). In the latter case, &amp;quot;d-&amp;gt;&amp;quot; already serves as a prefix. In the same sense, member variables in *Private classes have no prefix at all.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background: #eee; color:black; font-size: smaller;&quot;&gt;&lt;div&gt;** Member variables are prefixed with &amp;quot;m_*&amp;quot; if there is no private data class (PIMPL). In the latter case, &amp;quot;d-&amp;gt;&amp;quot; already serves as a prefix. In the same sense, member variables in *Private classes have no prefix at all.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;!-- diff generator: internal 2026-04-04 14:35:24 --&gt;
&lt;/table&gt;</summary>
		<author><name>mikhas</name></author>	</entry>

	<entry>
		<id>http://bugs.maemo.com/index.php?title=Miniature/Development/CodingStyle&amp;diff=45072&amp;oldid=prev</id>
		<title>mikhas:&amp;#32;New page: {{Miniature}} = General = We mostly &lt;s&gt;try to follow the offical Qt Coding Guidelines, but with some exceptions.&lt;/s&gt; follow mikhas' personal coding style for Qt: * We use whitespaces over ...</title>
		<link rel="alternate" type="text/html" href="http://bugs.maemo.com/index.php?title=Miniature/Development/CodingStyle&amp;diff=45072&amp;oldid=prev"/>
				<updated>2011-08-27T19:00:17Z</updated>
		
		<summary type="html">&lt;p&gt;New page: {{Miniature}} = General = We mostly &amp;lt;s&amp;gt;try to follow the offical Qt Coding Guidelines, but with some exceptions.&amp;lt;/s&amp;gt; follow mikhas&amp;#39; personal coding style for Qt: * We use whitespaces over ...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Miniature}}&lt;br /&gt;
= General =&lt;br /&gt;
We mostly &amp;lt;s&amp;gt;try to follow the offical Qt Coding Guidelines, but with some exceptions.&amp;lt;/s&amp;gt; follow mikhas' personal coding style for Qt:&lt;br /&gt;
* We use whitespaces over tabs.&lt;br /&gt;
* Max. length of a source code line is 120 characters.&lt;br /&gt;
* We use (nested) namespaces.&lt;br /&gt;
* Type names are in CamelCase&lt;br /&gt;
* Method names are in mixedCase (= lowerCamelCase).&lt;br /&gt;
* Opening braces for code blocks after newlines, *except* opening braces for classes and methods.&lt;br /&gt;
* Variables use under_scores and are lower_cases, to make them stand out from other symbols.&lt;br /&gt;
** Member variables are prefixed with &amp;quot;m_*&amp;quot; if there is no private data class (PIMPL). In the latter case, &amp;quot;d-&amp;gt;&amp;quot; already serves as a prefix. In the same sense, member variables in *Private classes have no prefix at all.&lt;br /&gt;
* Use initializer list and call construct each member explicitly.&lt;br /&gt;
* No boolean method parameters, unless it's a boolean property. Use enums instead.&lt;br /&gt;
* Use Q_SLOT, Q_SIGNAL as a method prefix.&lt;br /&gt;
* Impl files have a .cc extensions, header files use .h&lt;br /&gt;
* Use Qt containers over std containers.&lt;br /&gt;
* No explicit delete statements. Use QScopedPointer or C++'s inbuilt resource cleanup of member variables.&lt;br /&gt;
* Avoid QObject parent ownership - with C++ inbuilt mechanism, smart pointers and the concept of RAII, there is rarely need for it. It makes memory management more explicit and avoids another constant source of bugs.&lt;br /&gt;
** Try to use copyable value types instead of full-blown QObjects.&lt;br /&gt;
* Avoid singletons&lt;br /&gt;
** Singletons often become hidden dependencies of a class, making the class less reusable, as it increases coupling.&lt;br /&gt;
** Prefer dependency injection instead, this also increases testability.&lt;br /&gt;
* Public methods should be virtual.&lt;br /&gt;
** Makes classes easier to reuse, increases testability.&lt;br /&gt;
** Does not apply for structs.&lt;br /&gt;
&lt;br /&gt;
= Naming conventions =&lt;br /&gt;
&lt;br /&gt;
* Methods *do* something, that's why their names should always contain a verb (slots and signals count as methods, too).&lt;br /&gt;
&lt;br /&gt;
    connect(this,  SIGNAL(someSignalChanged()),&lt;br /&gt;
            other, SLOT(onSomeSignalChanged()));&lt;br /&gt;
&lt;br /&gt;
* typedef'd types *might* hint at their composed types (to emphasize their intended use), but avoid directly including the type information in the name:&lt;br /&gt;
&lt;br /&gt;
  typedef QSharedPointer&amp;lt;SomeClass&amp;gt; SomeSharedClass; // Good! Sharing a resource is a concept.&lt;br /&gt;
  typedef QSharedPointer&amp;lt;SomeClass&amp;gt; SomeClassPtr; // Bad! What is a &amp;quot;SomeClassPtr *some_variable&amp;quot; now?&lt;br /&gt;
&lt;br /&gt;
  typedef QList&amp;lt;SomeClass&amp;gt; SomeClassList; // still OK, since &amp;quot;List&amp;quot; is a concept, too.&lt;br /&gt;
&lt;br /&gt;
* Avoid hungarian variable names.&lt;br /&gt;
* If in doubt, name slot &amp;quot;onSomeSignalChanged&amp;quot;. This makes for nice connection code:&lt;br /&gt;
&lt;br /&gt;
  connect(some_signal_source, SIGNAL(theSignalWasEmitted()),&lt;br /&gt;
          some_receiver,      SLOT(onTheSignalWasEmitted()),&lt;br /&gt;
          Qt::UniqueConnection);&lt;br /&gt;
&lt;br /&gt;
** Use normalized signal/slot signatures (no const keyword, no &amp;amp;).&lt;br /&gt;
&lt;br /&gt;
= Advanced =&lt;br /&gt;
* Method parameters should be easy to read:&lt;br /&gt;
&lt;br /&gt;
  class SomeClass&lt;br /&gt;
  {&lt;br /&gt;
      void someMethod(SomeType arg1,&lt;br /&gt;
                      SomeOtherType arg2,&lt;br /&gt;
                      ...);&lt;br /&gt;
&lt;br /&gt;
** Details:&lt;br /&gt;
*** Move each argument to a new line. This serves two goals: header files will be easier to scan (for humans), and you'll spot those ugly methods that scream &amp;quot;refactor me!&amp;quot; faster because they have more than 3 arguments. Also, because it *is* ugly, you might just go and refactor it, which will help everyone.&lt;br /&gt;
&lt;br /&gt;
* Use const refs for arguments when possible, even if the type is COW (copy-on-write, for example QString).&lt;br /&gt;
&lt;br /&gt;
* Don't return const refs for value types. Leave the decision to the caller.&lt;br /&gt;
&lt;br /&gt;
* Conditionals&lt;br /&gt;
&lt;br /&gt;
  if ((something &amp;amp;&amp;amp; somethingElse)&lt;br /&gt;
       || orPerhapsThis) {&lt;br /&gt;
      ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
** Details:&lt;br /&gt;
*** Conditionals often contain bugs. Especially when the surrounding code gets refactored, those bugs simply slip in.&lt;br /&gt;
*** 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).&lt;br /&gt;
*** 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.&lt;br /&gt;
&lt;br /&gt;
* One space after keywords that could be mistaken as functions (if, for, ...).&lt;br /&gt;
&lt;br /&gt;
* If you store a pointer to an object over which you do not intend to take ownership, then wrap this pointer in a QWeakPointer:&lt;br /&gt;
&lt;br /&gt;
  class SomeOtherClass&lt;br /&gt;
  {&lt;br /&gt;
      QWeakPointer&amp;lt;SomeType&amp;gt; m_member;&lt;br /&gt;
  };&lt;br /&gt;
&lt;br /&gt;
** Details:&lt;br /&gt;
*** QWeakPointers keep track of QObject deletion and set all referring QWeakPointers to zero, preventing dangling pointers. Especially when using signals &amp;amp; slots, this is an often (and sometimes hard to spot!) source of bugs. With QWeakPointers, you can wrap your code with conditional guards like so:&lt;br /&gt;
&lt;br /&gt;
  if (SomeType *var = m_member.data()) {&lt;br /&gt;
      // Do sth with var&lt;br /&gt;
      // This block *never* gets executed even if m_member was deleted elsewhere.&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
* Assign 0 to pointers after deletion (or use QPointers).&lt;br /&gt;
* Always initialize type primitives (e.g., pointers) in the constructor's initializer list.&lt;br /&gt;
* Don't use forward declaration unless necessary. It is not your task to optimize the compiler. Also, Qt Creator gets confused by them.&lt;br /&gt;
&lt;br /&gt;
== How to keep code testable (and hopefully, mostly bugfree) ==&lt;br /&gt;
We want to have testable code, therefore we also want to follow some advanced coding standards.&lt;br /&gt;
&lt;br /&gt;
For that, I will try to list some really really useful advice from this document: http://www.research.att.com/~bs/JSF-AV-rules.pdf, a coding style document for C++. Bjarne Stroustrup worked on that one, yes.&lt;br /&gt;
 &lt;br /&gt;
* Keep methods at a reasonable length. In general: If it doesn't fit on a screen page it's probably too long. More precise: Every method longer than 200 lines of code (including comments, yes) is too long.&lt;br /&gt;
* Within a method, avoid a cyclomatic complexity higher than 20 (see Appendix A regarding AV Rule 3, pp. 65, &amp;quot;Cyclomatic complexity measures the amount of decision logic in a single software module.&amp;quot;, and the example).&lt;br /&gt;
* Avoid cyclic dependencies between classes/modules (Rationale: it's mostly an indicator for layer violations).&lt;/div&gt;</summary>
		<author><name>mikhas</name></author>	</entry>

	</feed>