| 
									
										
										
										
											2010-12-21 14:02:05 +01:00
										 |  |  | /**************************************************************************** | 
					
						
							|  |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:51:16 +01:00
										 |  |  | ** Copyright (C) 2016 The Qt Company Ltd. | 
					
						
							|  |  |  | ** Contact: https://www.qt.io/licensing/ | 
					
						
							| 
									
										
										
										
											2010-12-21 14:02:05 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:51:16 +01:00
										 |  |  | ** This file is part of the Qt Creator documentation. | 
					
						
							| 
									
										
										
										
											2010-12-21 14:02:05 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:51:16 +01:00
										 |  |  | ** Commercial License Usage | 
					
						
							|  |  |  | ** Licensees holding valid commercial Qt licenses may use this file in | 
					
						
							|  |  |  | ** accordance with the commercial license agreement provided with the | 
					
						
							|  |  |  | ** Software or, alternatively, in accordance with the terms contained in | 
					
						
							|  |  |  | ** a written agreement between you and The Qt Company. For licensing terms | 
					
						
							|  |  |  | ** and conditions see https://www.qt.io/terms-conditions. For further | 
					
						
							|  |  |  | ** information use the contact form at https://www.qt.io/contact-us. | 
					
						
							| 
									
										
										
										
											2010-12-21 14:02:05 +01:00
										 |  |  | ** | 
					
						
							| 
									
										
										
										
											2016-01-15 14:51:16 +01:00
										 |  |  | ** GNU Free Documentation License Usage | 
					
						
							| 
									
										
										
										
											2010-12-21 14:02:05 +01:00
										 |  |  | ** Alternatively, this file may be used under the terms of the GNU Free | 
					
						
							|  |  |  | ** Documentation License version 1.3 as published by the Free Software | 
					
						
							| 
									
										
										
										
											2016-01-15 14:51:16 +01:00
										 |  |  | ** Foundation and appearing in the file included in the packaging of | 
					
						
							|  |  |  | ** this file. Please review the following information to ensure | 
					
						
							|  |  |  | ** the GNU Free Documentation License version 1.3 requirements | 
					
						
							|  |  |  | ** will be met: https://www.gnu.org/licenses/fdl-1.3.html. | 
					
						
							| 
									
										
										
										
											2010-12-21 14:02:05 +01:00
										 |  |  | ** | 
					
						
							|  |  |  | ****************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | /*! | 
					
						
							| 
									
										
										
										
											2011-05-02 12:14:12 +02:00
										 |  |  |     \previouspage qtcreator-ui-text.html | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \page coding-style.html | 
					
						
							| 
									
										
										
										
											2011-04-27 17:35:17 +02:00
										 |  |  |     \nextpage qtcreator-api.html | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \title Qt Creator Coding Rules | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \note This document is work in progress. | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     The coding rules aim to guide Qt Creator developers, to help them write | 
					
						
							|  |  |  |     understandable and maintainable code, and to minimize confusion and surprises. | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     As usual, rules are not set in stone. If you have a good reason to break one, | 
					
						
							|  |  |  |     do so. But first make sure that at least some other developers agree with you. | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     To contribute to the main Qt Creator source, you should comply to the | 
					
						
							|  |  |  |     following rules: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li The most important rule is: KISS (keep it short and simple). Always | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             choose the simpler implementation option over the more complicated one. | 
					
						
							|  |  |  |             This makes maintenance a lot easier. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Write good C++ code. That is, readable, well commented when necessary, | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             and object-oriented. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Take advantage of Qt. Do not re-invent the wheel. Think about which parts | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             of your code are generic enough that they might be incorporated into | 
					
						
							|  |  |  |             Qt instead of Qt Creator. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Adapt the code to the existing structures in Qt Creator. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             If you have improvement ideas, discuss them with other developers | 
					
						
							|  |  |  |             before writing the code. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Follow the guidelines in \l{Code Constructs}, \l{Formatting}, and | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \l{Patterns and Practices}. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Document interfaces. Right now we use qdoc, but changing to doxygen | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             is being considered. | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section1 Submitting Code | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 14:46:55 +02:00
										 |  |  |     To submit code to Qt Creator, you must understand the tools and mechanics as well as | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     the philosophy behind Qt development. For more information about how to set up | 
					
						
							| 
									
										
										
										
											2011-10-24 14:46:55 +02:00
										 |  |  |     the development environment for working on Qt Creator and how to submit code | 
					
						
							|  |  |  |     and documentation for inclusion, see | 
					
						
							| 
									
										
										
										
											2015-03-03 14:20:24 +01:00
										 |  |  |     \l{https://wiki.qt.io/Qt_Contribution_Guidelines}{Qt Contribution Guidelines}. | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section1 Binary and Source Compatibility | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     The following list describes how the releases are numbered and defines | 
					
						
							|  |  |  |     \e {binary compatibility} and \e {source code compatibility} between | 
					
						
							|  |  |  |     releases: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-10-16 16:06:42 +02:00
										 |  |  |         \li \QC 3.0.0 is a \e {major release}, \QC 3.1.0 is a \e {minor | 
					
						
							|  |  |  |             release}, and \QC 3.1.3 is a \e {patch release}. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li \e {Backward binary compatibility} means that code linked to an | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             earlier version of the library still works. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li \e {Forward binary compatibility} means that code linked to a | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             newer version of the library works with an older library. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li \e {Source code compatibility} means that code compiles without | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             modification. | 
					
						
							|  |  |  |         \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:06:42 +02:00
										 |  |  |     We do not currently guarantee binary nor source code | 
					
						
							| 
									
										
										
										
											2011-10-24 12:53:10 +02:00
										 |  |  |     compatibility between major releases and minor releases. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:06:42 +02:00
										 |  |  |     However, we try to preserve backward binary compatibility and | 
					
						
							|  |  |  |     backward source code compatibility for patch releases within the same minor release: | 
					
						
							|  |  |  |     \list | 
					
						
							|  |  |  |         \li Soft API freeze: Starting shortly after the beta release of a minor release, | 
					
						
							|  |  |  |             we start keeping backward source code compatibility within that minor release, | 
					
						
							|  |  |  |             including its patch releases. This means that from that point, code that uses \QC API will | 
					
						
							|  |  |  |             compile against the API of all coming versions of this minor release, including its | 
					
						
							|  |  |  |             patch releases. There still might be occasional exceptions to this rule, which should | 
					
						
							|  |  |  |             be properly communicated. | 
					
						
							|  |  |  |         \li Hard API freeze: Starting with the release candidate of a minor release, | 
					
						
							|  |  |  |             we keep backward source code compatibility within that minor release, including its | 
					
						
							|  |  |  |             patch releases. We also start keeping backward binary compatibility, with the exception | 
					
						
							|  |  |  |             that this will not be reflected in the plugins' compatVersion setting. So, \QC plugins | 
					
						
							|  |  |  |             written against the release candidate will not actually load in the final release or | 
					
						
							|  |  |  |             later versions, even though the binary compatibility of the libraries would | 
					
						
							|  |  |  |             theoretically allow for it. See the section about plugin specs below. | 
					
						
							|  |  |  |         \li Hard ABI freeze: Starting with the final release of a minor release, | 
					
						
							|  |  |  |             we keep backward source code and binary compatibility for this release and all its | 
					
						
							|  |  |  |             patch releases. | 
					
						
							|  |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:06:42 +02:00
										 |  |  |     For preserving backward compatibility: | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-10-16 16:06:42 +02:00
										 |  |  |         \li Do not add or remove any public API (for example global functions, | 
					
						
							| 
									
										
										
										
											2013-10-07 13:34:40 +02:00
										 |  |  |             public/protected/private member functions). | 
					
						
							|  |  |  |         \li Do not reimplement functions (not even inlines, | 
					
						
							|  |  |  |             nor protected or private functions). | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Check | 
					
						
							| 
									
										
										
										
											2015-03-03 14:20:24 +01:00
										 |  |  |             \l {https://wiki.qt.io/Binary_Compatibility_Workarounds}{Binary Compatibility Workarounds} | 
					
						
							| 
									
										
										
										
											2011-10-24 12:53:10 +02:00
										 |  |  |             for ways to preserve binary compatibility. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     For more information on binary compatibility, see | 
					
						
							|  |  |  |     \l{http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C++}{Binary Compatibility Issues With C++}. | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-16 16:06:42 +02:00
										 |  |  |     From the viewpoint of \l{Plugin Specifications} this means that | 
					
						
							|  |  |  |     \list | 
					
						
							|  |  |  |         \li \QC plugins in patch releases will have the minor release as \c {compatVersion}. For | 
					
						
							|  |  |  |             example the plugins from version 3.1.2 will have \c {compatVersion="3.1.0"}. | 
					
						
							|  |  |  |         \li Pre-releases of the minor release, including the release candidate, | 
					
						
							|  |  |  |             will still have themselves as the \c {compatVersion}, meaning that plugins compiled | 
					
						
							|  |  |  |             against the final release will not load in the pre-releases. | 
					
						
							|  |  |  |         \li \QC plugin developers can decide if their plugin requires a certain patch release | 
					
						
							|  |  |  |             (or later) of other \QC plugins, or if they work with all patch releases of this minor | 
					
						
							|  |  |  |             version, by setting the corresponding \c {version} when declaring the dependency on | 
					
						
							|  |  |  |             the other plugin. The default for \QC plugins provided by the Qt Project is to require | 
					
						
							|  |  |  |             the latest patch release. | 
					
						
							|  |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For example, the \c {Find} plugin in \QC 3.1 beta (internal version number 3.0.82) will have | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |         <plugin name="Find" version="3.0.82" compatVersion="3.0.82"> | 
					
						
							|  |  |  |           <dependencyList> | 
					
						
							|  |  |  |             <dependency name="Core" version="3.0.82"/> | 
					
						
							|  |  |  |             .... | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  |     The \c {Find} plugin in \QC 3.1.0 final will have | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |         <plugin name="Find" version="3.1.0" compatVersion="3.1.0"> | 
					
						
							|  |  |  |           <dependencyList> | 
					
						
							|  |  |  |             <dependency name="Core" version="3.1.0"/> | 
					
						
							|  |  |  |             .... | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  |     The \c {Find} plugin in \QC 3.1.1 patch release will have version 3.1.1, will be | 
					
						
							|  |  |  |     backward binary compatible with \c {Find} plugin version 3.1.0 (\c {compatVersion="3.1.0"}), | 
					
						
							|  |  |  |     and will require a \c {Core} plugin that is binary backward compatible with \c {Core} | 
					
						
							|  |  |  |     plugin version 3.1.1: | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |         <plugin name="Find" version="3.1.1" compatVersion="3.1.0"> | 
					
						
							|  |  |  |           <dependencyList> | 
					
						
							|  |  |  |             <dependency name="Core" version="3.1.1"/> | 
					
						
							|  |  |  |             .... | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section1 Code Constructs | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Follow the guidelines for code constructs to make the code faster and | 
					
						
							|  |  |  |     clearer. In addition, the guidelines allow you to take advantage of the strong | 
					
						
							|  |  |  |     type checking in C++. | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Prefer preincrement to postincrement whenever possible. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             Preincrement is potentially faster than postincrement. Just | 
					
						
							|  |  |  |             think about the obvious implementations of pre/post-increment. This | 
					
						
							|  |  |  |             rule applies to decrement too: | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |             ++T; | 
					
						
							|  |  |  |             --U; | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             T++; | 
					
						
							|  |  |  |             U--; | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Try to minimize evaluation of the same code over and over. This is | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             aimed especially at loops: | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             Container::iterator end = large.end(); | 
					
						
							|  |  |  |             for (Container::iterator it = large.begin(); it != end; ++it) { | 
					
						
							|  |  |  |                     ...; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             for (Container::iterator it = large.begin(); | 
					
						
							|  |  |  |                  it != large.end(); ++it) { | 
					
						
							|  |  |  |                     ...; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2008-12-17 15:03:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li You can use the Qt \c foreach loop in non-time-critical code with a Qt | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             container. It is a nice way to keep line noise down and to give the | 
					
						
							|  |  |  |             loop variable a proper name: | 
					
						
							| 
									
										
										
										
											2008-12-17 15:03:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |                 foreach (QWidget *widget, container) | 
					
						
							|  |  |  |                     doSomething(widget); | 
					
						
							| 
									
										
										
										
											2010-01-29 21:33:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                 -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-17 15:03:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                 Container::iterator end = container.end(); | 
					
						
							|  |  |  |                 for (Container::iterator it = container.begin(); it != end; ++it) | 
					
						
							|  |  |  |                     doSomething(*it); | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2010-01-29 21:33:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             Make the loop variable const, if possible. This might prevent | 
					
						
							|  |  |  |             unnecessary detaching of shared data: | 
					
						
							| 
									
										
										
										
											2008-12-17 15:03:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |                 foreach (const QString &name, someListOfNames) | 
					
						
							|  |  |  |                     doSomething(name); | 
					
						
							| 
									
										
										
										
											2008-12-17 15:03:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                 - NOT - | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                 foreach (QString name, someListOfNames) | 
					
						
							|  |  |  |                     doSomething(name); | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |         \endlist | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section1 Formatting | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Capitalizing Identifiers | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Use \l{http://en.wikipedia.org/wiki/CamelCase}{camel case} in identifiers. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Capitalize the first word in an identifier as follows: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Class names begin with a capital letter. | 
					
						
							|  |  |  |         \li Function names begin with a lower case letter. | 
					
						
							|  |  |  |         \li Variable names begin with a lower case letter. | 
					
						
							| 
									
										
										
										
											2015-03-18 16:26:01 +01:00
										 |  |  |         \li Enum names and values begin with a capital letter. Unscoped Enum | 
					
						
							|  |  |  |             values contain some part of the name of the enum type. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 Whitespace | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Use four spaces for indentation, no tabs. | 
					
						
							|  |  |  |         \li Use blank lines to group statements together where suited. | 
					
						
							|  |  |  |         \li Always use only one blank line. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Pointers and References | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     For pointers or references, always use a single space before an asterisk (*) | 
					
						
							|  |  |  |     or an ampersand (&), but never after. | 
					
						
							|  |  |  |     Avoid C-style casts when possible: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     char *blockOfMemory = (char *)malloc(data.size()); | 
					
						
							|  |  |  |     char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     char* blockOfMemory = (char* ) malloc(data.size()); | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Of course, in this particulare case, using \c new might be an even better | 
					
						
							|  |  |  |     option. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3  Operator Names and Parentheses | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 14:14:11 +02:00
										 |  |  |     Do not use spaces between operator names and parentheses. The equation | 
					
						
							|  |  |  |     marks (==) are a part of the operator name, and therefore, spaces make the | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     declaration look like an expression: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     operator==(type) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     operator == (type) | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Function Names and Parentheses | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Do not use spaces between function names and parentheses: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     void mangle() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void mangle () | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Keywords | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Always use a single space after a keyword, and before a curly brace: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     if (foo) { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |     if(foo){ | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-14 12:58:14 +01:00
										 |  |  |     \section3 Comments | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     In general, put one space after "//". To align text in multiline | 
					
						
							|  |  |  |     comments, you can insert multiple spaces. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Braces | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     As a base rule, place the left curly brace on the same line as the | 
					
						
							|  |  |  |     start of the statement: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     if (codec) { | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (codec) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Exception: Function implementations and class declarations always have | 
					
						
							|  |  |  |     the left brace in the beginning of a line: | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |     static void foo(int g) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         qDebug("foo: %i", g); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     class Moo | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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. | 
					
						
							|  |  |  |     Otherwise, omit them: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     if (address.isEmpty()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < 10; ++i) | 
					
						
							|  |  |  |         qDebug("%i", i); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (address.isEmpty()) { | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (int i = 0; i < 10; ++i) { | 
					
						
							|  |  |  |         qDebug("%i", i); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Exception 1: Use braces also if the parent statement covers several | 
					
						
							|  |  |  |     lines or if it wraps: | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							| 
									
										
										
										
											2011-02-04 12:44:43 +01:00
										 |  |  |     if (address.isEmpty() | 
					
						
							|  |  |  |             || !isValid() | 
					
						
							| 
									
										
										
										
											2011-02-11 11:23:54 +01:00
										 |  |  |             || !codec) { | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2011-02-11 11:23:54 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \note This could be re-written as: | 
					
						
							| 
									
										
										
										
											2011-02-04 12:44:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							| 
									
										
										
										
											2011-02-04 12:44:43 +01:00
										 |  |  |     if (address.isEmpty()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!isValid()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!codec) | 
					
						
							|  |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2011-02-04 12:44:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Exception 2: Use braces also in if-then-else blocks where either the | 
					
						
							|  |  |  |     if-code or the else-code covers several lines: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     if (address.isEmpty()) { | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         --it; | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         qDebug("%s", qPrintable(address)); | 
					
						
							|  |  |  |         ++it; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (address.isEmpty()) | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         --it; | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         qDebug("%s", qPrintable(address)); | 
					
						
							|  |  |  |         ++it; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							|  |  |  |     if (a) { | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         if (b) | 
					
						
							|  |  |  |             ... | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             ... | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (a) | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |         if (b) | 
					
						
							|  |  |  |             ... | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |             ... | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Use curly braces when the body of a conditional statement is empty: | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |     while (a) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     while (a); | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 Parentheses | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Use parentheses to group expressions: | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |     if ((a && b) || c) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (a && b || c) | 
					
						
							|  |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  |     (a + b) & c | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     a + b & c | 
					
						
							|  |  |  |     \endcode | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Line Breaks | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Keep lines shorter than 100 characters. | 
					
						
							|  |  |  |         \li Insert line breaks if necessary. | 
					
						
							|  |  |  |         \li Commas go at the end of a broken line. | 
					
						
							|  |  |  |         \li Operators start at the beginning of the new line. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |             if (longExpression | 
					
						
							|  |  |  |                 || otherLongExpression | 
					
						
							|  |  |  |                 || otherOtherLongExpression) { | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             if (longExpression || | 
					
						
							|  |  |  |                 otherLongExpression || | 
					
						
							|  |  |  |                 otherOtherLongExpression) { | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Declarations | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Use this order for the access sections of your class: public, | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             protected, private. The public section is interesting for every | 
					
						
							|  |  |  |             user of the class. The private section is only of interest for the | 
					
						
							|  |  |  |             implementors of the class (you). | 
					
						
							| 
									
										
										
										
											2011-04-14 11:14:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid declaring global objects in the declaration file of the class. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             If the same variable is used for all objects, use a static member. | 
					
						
							| 
									
										
										
										
											2011-04-14 11:14:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Use \c{class} instead of \c{struct}. Some compilers mangle that | 
					
						
							| 
									
										
										
										
											2011-04-14 11:14:21 +02:00
										 |  |  |             difference into the symbol names and spit out warnings if a struct | 
					
						
							|  |  |  |             declaration is followed by a class definition. To avoid ongoing | 
					
						
							| 
									
										
										
										
											2012-09-21 16:05:58 +02:00
										 |  |  |             changes from one to the other we declare \c{class} the preferred way. | 
					
						
							| 
									
										
										
										
											2011-04-14 11:14:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section3 Declaring Variables | 
					
						
							| 
									
										
										
										
											2008-12-15 17:48:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid global variables of class type to rule out initialization order problems. | 
					
						
							| 
									
										
										
										
											2011-06-20 14:14:49 +02:00
										 |  |  |             Consider using \c Q_GLOBAL_STATIC if they cannot be avoided. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Declare global string literals as | 
					
						
							| 
									
										
										
										
											2011-06-20 14:14:49 +02:00
										 |  |  |             \code | 
					
						
							|  |  |  |             const char aString[] = "Hello"; | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid short names (such as, a, rbarr, nughdeget) whenever possible. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             Use single-character variable names only for counters and | 
					
						
							|  |  |  |             temporaries, where the purpose of the variable is obvious. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Declare each variable on a separate line: | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |             QString a = "Joe"; | 
					
						
							|  |  |  |             QString b = "Foo"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             QString a = "Joe", b = "Foo"; | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \note \c{QString a = "Joe"} formally calls a copy constructor on a | 
					
						
							|  |  |  |             temporary that is constructed from a string literal. Therefore, it is | 
					
						
							|  |  |  |             potentially more expensive than direct construction by | 
					
						
							|  |  |  |             \c {QString a("Joe")}. However, the compiler is allowed to elide the | 
					
						
							|  |  |  |             copy (even if this has side effects), and modern compilers typically do | 
					
						
							|  |  |  |             so. Given these equal costs, Qt Creator code favours the '=' idiom as | 
					
						
							|  |  |  |             it is in | 
					
						
							|  |  |  |             line with the traditional C-style initialization, it cannot be | 
					
						
							|  |  |  |             mistaken as function declaration, and it reduces the level of nested | 
					
						
							|  |  |  |             parantheses in more initializations. | 
					
						
							| 
									
										
										
										
											2009-01-21 13:58:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid abbreviations: | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |             int height; | 
					
						
							|  |  |  |             int width; | 
					
						
							|  |  |  |             char *nameOfThis; | 
					
						
							|  |  |  |             char *nameOfThat; | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int a, b; | 
					
						
							|  |  |  |             char *c, *d; | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Wait with declaring a variable until it is needed. This is especially | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             important when initialization is done at the same time. | 
					
						
							|  |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 16:24:30 +01:00
										 |  |  |     \section2 Namespaces | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2014-03-21 14:43:15 +01:00
										 |  |  |         \li Put the left curly brace on the same line as the \c namespace keyword. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not indent declarations or definitions inside. | 
					
						
							|  |  |  |         \li Optional, but recommended if the namespaces spans more than a few lines: | 
					
						
							|  |  |  |             Add a comment after the right curly brace repeating the namespace. | 
					
						
							| 
									
										
										
										
											2011-11-11 16:24:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         \code | 
					
						
							|  |  |  |         namespace MyPlugin { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void someFunction() { ... } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             }  // namespace MyPlugin | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2011-11-11 16:24:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li As an exception, if there is only a single class declaration inside | 
					
						
							|  |  |  |             the namespace, all can go on a single line: | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             namespace MyPlugin { class MyClass; } | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2014-10-15 15:14:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         \li Do not use using-directives in header files. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li Do not rely on using-directives when defining classes and | 
					
						
							|  |  |  |         functions, instead define it in a properly named declarative region. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li Do not rely on using-directives when accessing global functions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li In other cases, you are encouraged to use using-directives, | 
					
						
							|  |  |  |         as they help you avoid cluttering the code. Prefer putting all | 
					
						
							|  |  |  |         using-directives near the top of the file, after all includes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \code | 
					
						
							|  |  |  |         [in foo.cpp] | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         #include "foos.h" | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         #include <utils/filename.h> | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         using namespace Utils; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         namespace Foo { | 
					
						
							|  |  |  |         namespace Internal { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void SomeThing::bar() | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             FileName f;              // or Utils::FileName f | 
					
						
							|  |  |  |             ... | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         } // namespace Internal      // or only // Internal | 
					
						
							|  |  |  |         } // namespace Foo           // or only // Foo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         [in foo.h] | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         using namespace Utils;       // Wrong: no using-directives in headers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         class SomeThing | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ... | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         [in foo.cpp] | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         using namespace Utils; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         #include "bar.h"             // Wrong: #include after using-directive | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         [in foo.cpp] | 
					
						
							|  |  |  |         ... | 
					
						
							|  |  |  |         using namepace Foo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         void SomeThing::bar()        // Wrong if Something is in namespace Foo | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             ... | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-11 16:24:30 +01:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section1 Patterns and Practices | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-10 08:32:07 +02:00
										 |  |  |     \target coding-rules-namespacing | 
					
						
							| 
									
										
										
										
											2011-08-29 10:48:06 +02:00
										 |  |  |     \section2 Namespacing | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-03 14:20:24 +01:00
										 |  |  |     Read \l{https://wiki.qt.io/Qt_In_Namespace}{Qt In Namespace} | 
					
						
							| 
									
										
										
										
											2011-08-29 10:48:06 +02:00
										 |  |  |     and keep in mind that all of Qt Creator is \e{namespace aware} code. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The namespacing policy within Qt Creator is as follows: | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Classes/Symbols of a library or plugin that are exported for use of | 
					
						
							|  |  |  |             other libraries or plugins are in a namespace specific to that | 
					
						
							|  |  |  |             library/plugin, e.g. \c{MyPlugin}. | 
					
						
							|  |  |  |         \li Classes/Symbols of a library or plugin that are not exported are in | 
					
						
							|  |  |  |             an additional \c{Internal} namespace, e.g. \c{MyPlugin::Internal}. | 
					
						
							| 
									
										
										
										
											2011-08-29 10:48:06 +02:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Passing File Names | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Qt Creator API expects file names in portable format, that is, with slashes (/) | 
					
						
							|  |  |  |     instead of backslashes (\\) even on Windows. To pass a file name from the user | 
					
						
							|  |  |  |     to the API, convert it with QDir::fromNativeSeparators first. To present a file | 
					
						
							|  |  |  |     name to the user, convert it back to native format with | 
					
						
							| 
									
										
										
										
											2012-05-15 13:41:50 +02:00
										 |  |  |     QDir::toNativeSeparators. Consider using Utils::FileName::fromUserInput(QString) | 
					
						
							|  |  |  |     and Utils::FileName::toUserOutput() for these tasks. | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-15 13:41:50 +02:00
										 |  |  |     Use Utils::FileName when comparing file names, because that takes case sensitivity into account. | 
					
						
							|  |  |  |     Also make sure that you compare clean paths (QDir::cleanPath()). | 
					
						
							| 
									
										
										
										
											2011-02-04 12:44:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Plugin Extension Points | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     A plugin extension point is an interface that is provided by one plugin | 
					
						
							|  |  |  |     to be implemented by others. The plugin then retrieves all | 
					
						
							|  |  |  |     implementations of the interface and uses them. That is, they \e extend the | 
					
						
							|  |  |  |     functionality of the plugin. Typically, the | 
					
						
							|  |  |  |     implementations of the interface are put into the global object pool | 
					
						
							|  |  |  |     during plugin initialization, and the plugin retrieves them from the | 
					
						
							|  |  |  |     object pool at the end of plugin initialization. | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     For example, the Find plugin provides the FindFilter interface for | 
					
						
							|  |  |  |     other plugins to implement. With the FindFilter interface, additional search | 
					
						
							| 
									
										
										
										
											2014-12-17 17:15:42 +01:00
										 |  |  |     scopes can be added, that appear in the \uicontrol {Advanced Search} dialog. The | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Find plugin retrieves all FindFilter implementations from the global | 
					
						
							|  |  |  |     object pool and presents them in the dialog. The plugin forwards the | 
					
						
							|  |  |  |     actual search request to the correct FindFilter implementation, which | 
					
						
							|  |  |  |     then performs the search. | 
					
						
							| 
									
										
										
										
											2011-01-05 12:07:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Using the Global Object Pool | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     You can add objects to the global object pool via | 
					
						
							|  |  |  |     \l{ExtensionSystem::PluginManager::addObject()}, and retrieve objects | 
					
						
							|  |  |  |     of a specific type again via | 
					
						
							|  |  |  |     \l{ExtensionSystem::PluginManager::getObjects()}.  This should mostly | 
					
						
							|  |  |  |     be used for implementations of \l{Plugin Extension Points}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \note Do not put a singleton into the pool, and do not retrieve | 
					
						
							|  |  |  |     it from there. Use the singleton pattern instead. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 C++ Features | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2016-01-22 12:20:53 +01:00
										 |  |  |         \li Prefer \c {#pragma once} over header guards. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not use exceptions, unless you know what you do. | 
					
						
							| 
									
										
										
										
											2011-03-21 15:08:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not use RTTI (Run-Time Type Information; that is, the typeinfo | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             struct, the dynamic_cast or the typeid operators, including throwing | 
					
						
							|  |  |  |             exceptions), unless you know what you do. | 
					
						
							| 
									
										
										
										
											2011-03-21 15:08:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not use virtual inheritance, unless you know what you do. | 
					
						
							| 
									
										
										
										
											2011-03-21 15:08:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Use templates wisely, not just because you can. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             Hint: Use the compile autotest to see whether a C++ feature is supported | 
					
						
							|  |  |  |             by all compilers in the test farm. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li All code is ASCII only (7-bit characters only, run \c {man ascii} if unsure) | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |                 \li Rationale: We have too many locales inhouse and an unhealthy | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                     mix of UTF-8 and Latin1 systems. Usually, characters > 127 can | 
					
						
							|  |  |  |                     be broken without you even knowing by clicking Save in your | 
					
						
							|  |  |  |                     favourite editor. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |                 \li For strings: Use \\nnn (where nnn is the octal representation | 
					
						
							|  |  |  |                     of whatever locale you want your string in) or \\xnn (where nn | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                     is hexadecimal). | 
					
						
							|  |  |  |                     For example: QString s = QString::fromUtf8("\\213\\005"); | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |               \li For umlauts in documentation, or other non-ASCII characters, | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |                   either use the qdoc \c {\unicode} command or use the relevant macro. | 
					
						
							|  |  |  |                   For example: \c{\uuml} for \uuml. | 
					
						
							|  |  |  |            \endlist | 
					
						
							| 
									
										
										
										
											2011-03-21 15:08:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Use static keywords instead of anonymous namespaces whenever possible. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             A name localized to the compilation unit with static is | 
					
						
							|  |  |  |             guaranteed to have internal linkage. For names declared in anonymous | 
					
						
							|  |  |  |             namespaces, the C++ standard unfortunately mandates external linkage | 
					
						
							| 
									
										
										
										
											2011-03-21 15:08:36 +01:00
										 |  |  |             (ISO/IEC 14882, 7.1.1/6, or see various discussions about this on the gcc | 
					
						
							|  |  |  |             mailing lists). | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section3 Null Pointers | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Using a plain zero (0) for null pointer constants is always correct and | 
					
						
							|  |  |  |     least effort to type. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \code | 
					
						
							|  |  |  |     void *p = 0; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void *p = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void *p = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void *p = 42 - 7 * 6; | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \note As an exception, imported third party code as well as code | 
					
						
							|  |  |  |     interfacing the native APIs (src/support/os_*) can use NULL. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 17:10:57 +01:00
										 |  |  |     \section2 C++11 and C++14 Features | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 16:46:36 +01:00
										 |  |  |     Code should compile with Microsoft Visual Studio 2013, g++ 4.7, and Clang 3.1. | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     \section3 Lambdas | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 17:10:57 +01:00
										 |  |  |     When using lambdas, note the following: | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2015-03-18 17:10:57 +01:00
										 |  |  |         \li You do not have to explicitly specify the return type. If you are not using one | 
					
						
							|  |  |  |             of the previously mentioned compilers, do note that this is a C++14 feature and you | 
					
						
							|  |  |  |             might need to enable C++14 support in your compiler. | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  |             \code | 
					
						
							|  |  |  |             []() { | 
					
						
							|  |  |  |                 Foo *foo = activeFoo(); | 
					
						
							|  |  |  |                 return foo ? foo->displayName() : QString(); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li If you use static functions from the class that the lambda is located in, you have to | 
					
						
							|  |  |  |             explicitly capture \c this. Otherwise it does not compile with g++ 4.7 and earlier. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             void Foo::something() | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ... | 
					
						
							|  |  |  |                 [this]() { Foo::someStaticFunction(); } | 
					
						
							|  |  |  |                 ... | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             void Foo::something() | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 ... | 
					
						
							|  |  |  |                 []() { Foo::someStaticFunction(); } | 
					
						
							|  |  |  |                 ... | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Format the lambda according to the following rules: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							|  |  |  |         \li Place the capture-list, parameter list, return type, and opening brace on the first line, | 
					
						
							|  |  |  |             the body indented on the following lines, and the closing brace on a new line. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             []() -> bool { | 
					
						
							|  |  |  |                 something(); | 
					
						
							|  |  |  |                 return isSomethingElse(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             []() -> bool { something(); | 
					
						
							|  |  |  |             somethingElse(); } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li Place a closing parenthesis and semicolon of an enclosing function call on the same line | 
					
						
							|  |  |  |             as the closing brace of the lambda. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             foo([]() { | 
					
						
							|  |  |  |                 something(); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li If you are using a lambda in an 'if' statement, start the lambda on a new line, to | 
					
						
							|  |  |  |             avoid confusion between the opening brace for the lambda and the opening brace for the | 
					
						
							|  |  |  |             'if' statement. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             if (anyOf(fooList, | 
					
						
							|  |  |  |                     [](Foo foo) { | 
					
						
							|  |  |  |                         return foo.isGreat(); | 
					
						
							|  |  |  |                     }) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (anyOf(fooList, [](Foo foo) { | 
					
						
							|  |  |  |                         return foo.isGreat(); | 
					
						
							|  |  |  |                     }) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li Optionally, place the lambda completely on one line if it fits. | 
					
						
							|  |  |  |             \code | 
					
						
							| 
									
										
										
										
											2015-02-24 09:25:59 +01:00
										 |  |  |             foo([] { return true; }); | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 09:25:59 +01:00
										 |  |  |             if (foo([] { return true; })) { | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  |                 ... | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 \c auto Keyword | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Optionally, you can use the \c auto keyword in the following cases. If in doubt, | 
					
						
							|  |  |  |     for example if using \c auto could make the code less readable, do not use \c auto. | 
					
						
							|  |  |  |     Keep in mind that code is read much more often than written. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							|  |  |  |         \li When it avoids repetition of a type in the same statement. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             auto something = new MyCustomType; | 
					
						
							|  |  |  |             auto keyEvent = static_cast<QKeyEvent *>(event); | 
					
						
							| 
									
										
										
										
											2015-01-21 18:00:59 +01:00
										 |  |  |             auto myList = QStringList() << QLatin1String("FooThing") << QLatin1String("BarThing"); | 
					
						
							| 
									
										
										
										
											2014-07-04 14:42:44 +02:00
										 |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \li When assigning iterator types. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             auto it = myList.const_iterator(); | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-18 16:46:36 +01:00
										 |  |  |     \section3 Scoped enums | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     You can use scoped enums in places where the implicit conversion to int of unscoped enums is | 
					
						
							|  |  |  |     undesired or the additional scope is useful. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Delegating constructors | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Use delegating constructors if multiple constructors use essentially the same code. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Initializer list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Use initializer lists to initialize containers, for example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \code | 
					
						
							|  |  |  |     const QVector<int> values = {1, 2, 3, 4, 5}; | 
					
						
							|  |  |  |     \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-01 18:42:52 +02:00
										 |  |  |     \section3 Non-Static Data Member Initialization | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Use non-static data member initialization for trivial initializations, except in public | 
					
						
							|  |  |  |     exported classes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Defaulted and Deleted Functions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Consider using \c{=default} and \c{=delete} to control the special functions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Override | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     It is recommended to use the \c{override} keyword when overriding virtual functions. Do not | 
					
						
							|  |  |  |     use virtual on overridden functions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Make sure that a class uses \c{override} consistently, either for all overridden functions or | 
					
						
							|  |  |  |     for none. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Nullptr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     All compilers support \c{nullptr}, but there is no consensus on using it. If in doubt, ask | 
					
						
							|  |  |  |     the maintainer of the module whether they prefer using \c{nullptr}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section3 Range-Based for-Loop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     You may use range-based for-loops, but beware of the spurious detachment problem. | 
					
						
							|  |  |  |     If the for-loop only reads the container and it is not obvious whether the | 
					
						
							|  |  |  |     container is const or unshared, use \c{std::cref()} to ensure that the container | 
					
						
							|  |  |  |     is not unnecessarily detached. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Using QObject | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2015-01-05 17:21:27 +01:00
										 |  |  |         \li Remember to add the Q_OBJECT macro to QObject subclasses that rely | 
					
						
							|  |  |  |             on the meta object system. Meta object system related features are | 
					
						
							|  |  |  |             the definition of signals and slots, the use of \c{qobject_cast<>}, | 
					
						
							|  |  |  |             and others. See also \l{Casting}. | 
					
						
							| 
									
										
										
										
											2015-01-05 14:10:50 +01:00
										 |  |  |         \li Prefer Qt5-style \c{connect()} calls over Qt4-style. | 
					
						
							|  |  |  |         \li When using Qt4-style \c{connect()} calls, normalize the arguments | 
					
						
							|  |  |  |             for signals and slots inside connect statements to safely make | 
					
						
							|  |  |  |             signal and slot lookup a few cycles faster. You can use | 
					
						
							|  |  |  |             $QTDIR/util/normalize to normalize existing code. For more | 
					
						
							| 
									
										
										
										
											2015-02-25 10:15:28 +01:00
										 |  |  |             information, see QMetaObject::normalizedSignature. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 File Headers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If you create a new file, the top of the file should include a | 
					
						
							|  |  |  |     header comment equal to the one found in other source files of Qt Creator. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 Including Headers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Use the following format to include Qt headers: | 
					
						
							| 
									
										
										
										
											2012-05-11 10:20:42 +02:00
										 |  |  |             \c{#include <QWhatEver>}. Do not include the module as it might have changed between | 
					
						
							|  |  |  |             Qt4 and Qt5. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Arrange includes in an order that goes from specific to generic to | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             ensure that the headers are self-contained. For example: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  |         \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |             \li \c{#include "myclass.h"} | 
					
						
							|  |  |  |             \li \c{#include "otherclassinplugin.h"} | 
					
						
							|  |  |  |             \li \c{#include <otherplugin/someclass.h>} | 
					
						
							|  |  |  |             \li \c{#include <QtClass>} | 
					
						
							|  |  |  |             \li \c{#include <stdthing>} | 
					
						
							|  |  |  |             \li \c{#include <system.h>} | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |         \endlist | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Enclose headers from other plugins in square brackets (<>) rather than | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             quotation marks ("") to make it easier to spot external dependencies in | 
					
						
							|  |  |  |             the sources. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Add empty lines between long blocks of \e peer headers and try to | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             arrange the headers in alphabetic order within a block. | 
					
						
							|  |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 Casting | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid C casts, prefer C++ casts (\c static_cast, \c const_cast, | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \c reinterpret_cast) Both \c reinterpret_cast and | 
					
						
							|  |  |  |             C-style casts are dangerous, but at least \c reinterpret_cast | 
					
						
							|  |  |  |             will not remove the const modifier. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not use \c dynamic_cast, use \c {qobject_cast} for QObjects, or | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             refactor your design, for example by introducing a \c {type()} | 
					
						
							| 
									
										
										
										
											2013-10-07 13:34:40 +02:00
										 |  |  |             function (see QListWidgetItem), unless you know what you do. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \section2 Compiler and Platform-specific Issues | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Be extremely careful when using the question mark operator. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             If the returned types are not identical, some compilers generate | 
					
						
							|  |  |  |             code that crashes at runtime (you will not even get a compiler warning): | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             QString s; | 
					
						
							|  |  |  |             // crash at runtime - QString vs. const char * | 
					
						
							|  |  |  |             return condition ? s : "nothing"; | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Be extremely careful about alignment. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             Whenever a pointer is cast such that the required alignment of | 
					
						
							|  |  |  |             the target is increased, the resulting code might crash at runtime | 
					
						
							|  |  |  |             on some architectures. For example, if a \c {const char *} is cast to a | 
					
						
							|  |  |  |             \c {const int *}, it will crash on machines where integers have to be | 
					
						
							|  |  |  |             aligned at two-byte or four-byte boundaries. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Use a union to force the compiler to align variables correctly. | 
					
						
							|  |  |  |             In the example below, you can be sure that all instances of | 
					
						
							|  |  |  |             AlignHelper are aligned at integer-boundaries: | 
					
						
							|  |  |  |             \code | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  |             union AlignHelper | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 char c; | 
					
						
							|  |  |  |                 int i; | 
					
						
							|  |  |  |             }; | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 23:58:18 +03:00
										 |  |  |         \li Stick to integral types, arrays of integral types, and structs thereof | 
					
						
							|  |  |  |             for static declarations in headers. | 
					
						
							|  |  |  |             For example, \c {static float i[SIZE_CONSTANT];} will not be optimized | 
					
						
							|  |  |  |             and copied in every plugin in most cases, it would be good to avoid it. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Anything that has a constructor or needs to run code to be | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             initialized cannot be used as global object in library code, | 
					
						
							|  |  |  |             since it is undefined when that constructor or code will be run | 
					
						
							|  |  |  |             (on first usage, on library load, before \c {main()} or not at all). | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             Even if the execution time of the initializer is defined for | 
					
						
							|  |  |  |             shared libraries, you will get into trouble when moving that code | 
					
						
							|  |  |  |             in a plugin or if the library is compiled statically: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |             // global scope | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // Default constructor needs to be run to initialize x: | 
					
						
							|  |  |  |             static const QString x; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // Constructor that takes a const char * has to be run: | 
					
						
							|  |  |  |             static const QString y = "Hello"; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             QString z; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // Call time of foo() undefined, might not be called at all: | 
					
						
							|  |  |  |             static const int i = foo(); | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             Things you can do: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							|  |  |  |             // global scope | 
					
						
							|  |  |  |             // No constructor must be run, x set at compile time: | 
					
						
							|  |  |  |             static const char x[] = "someText"; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // y will be set at compile time: | 
					
						
							|  |  |  |             static int y = 7; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // Will be initialized statically, no code being run. | 
					
						
							|  |  |  |             static MyStruct s = {1, 2, 3}; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // Pointers to objects are OK, no code needed to be run to | 
					
						
							|  |  |  |             // initialize ptr: | 
					
						
							|  |  |  |             static QString *ptr = 0; | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             // Use Q_GLOBAL_STATIC to create static global objects instead: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-08 15:57:01 +04:00
										 |  |  |             Q_GLOBAL_STATIC(QString, s) | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             void foo() | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 s()->append("moo"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             \note Static objects in function scope are no problem. The constructor | 
					
						
							|  |  |  |             will be run the first time the function is entered. The code is not | 
					
						
							|  |  |  |             reentrant, though. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li A \c char is signed or unsigned dependent on the architecture. Use signed | 
					
						
							| 
									
										
										
										
											2012-09-21 16:05:58 +02:00
										 |  |  |             \c char or \c uchar if you explicitly want a signed or unsigned char. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             The following code will break on PowerPC, for example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             // Condition is always true on platforms where the | 
					
						
							|  |  |  |             // default is unsigned: | 
					
						
							|  |  |  |             if (c >= 0) { | 
					
						
							|  |  |  |                 ... | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid 64-bit enum values. The AAPCS (Procedure Call Standard | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             for the ARM Architecture) embedded ABI hard codes | 
					
						
							|  |  |  |             all enum values to a 32-bit integer. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not mix const and non-const iterators. This will silently crash | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             on broken compilers. | 
					
						
							|  |  |  |             \code | 
					
						
							|  |  |  |             for (Container::const_iterator it = c.constBegin(); it != c.constEnd(); ++it) | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             for (Container::const_iterator it = c.begin(); it != c.end(); ++it) | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2015-05-27 00:15:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         \li Do not inline virtual destructors in exported classes. This leads to duplicated vtables | 
					
						
							|  |  |  |             in dependent plugins and this can also break RTTI. See | 
					
						
							|  |  |  |             \l {https://bugreports.qt.io/browse/QTBUG-45582}{QTBUG-45582}. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-11 11:24:18 +01:00
										 |  |  |     \section2 Esthetics | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2015-03-18 16:26:01 +01:00
										 |  |  |         \li Prefer unscoped enums to define const over static const int or defines. | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |             Enumeration values will be replaced by the compiler at compile time, | 
					
						
							|  |  |  |             resulting in faster code. Defines are not namespace safe. | 
					
						
							|  |  |  |         \li Prefer verbose argument names in headers. | 
					
						
							|  |  |  |             Qt Creator will show the argument names in their completion box. | 
					
						
							|  |  |  |             It will look better in the documentation. | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Inheriting from Template or Tool Classes | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Inheriting from template or tool classes has the following potential | 
					
						
							|  |  |  |     pitfalls: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li The destructors are not virtual, which can lead to memory leaks. | 
					
						
							|  |  |  |         \li The symbols are not exported (and mostly inline), which can lead to | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             symbol clashes. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     For example, library A has class \c {Q_EXPORT X: public QList<QVariant> {};} | 
					
						
							|  |  |  |     and library B has class \c {Q_EXPORT Y: public QList<QVariant> {};}. | 
					
						
							|  |  |  |     Suddenly, QList symbols are exported from two libraries which results in a | 
					
						
							|  |  |  |     clash. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 14:28:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     \section2 Inheritance Versus Aggregation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     \list | 
					
						
							|  |  |  |         \li Use inheritance if there is a clear \e{is-a} relation. | 
					
						
							|  |  |  |         \li Use aggregation for re-use of orthogonal building blocks. | 
					
						
							|  |  |  |         \li Prefer aggregation over inheritance if there is a choice. | 
					
						
							|  |  |  |     \endlist | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section2 Conventions for Public Header Files | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     Our public header files have to survive the strict settings of | 
					
						
							|  |  |  |     some of our users. All installed headers have to follow these rules: | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \list | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li No C style casts (\c{-Wold-style-cast}). Use \c static_cast, \c const_cast | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             or \c reinterpret_cast, for basic types, use the constructor form: | 
					
						
							|  |  |  |             \c {int(a)} instead of \c {(int)a}. For more information, see \l{Casting}. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li No float comparisons (\c{-Wfloat-equal}). Use \c qFuzzyCompare to compare | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             values with a delta. Use \c qIsNull to check whether a float is | 
					
						
							| 
									
										
										
										
											2012-09-21 16:05:58 +02:00
										 |  |  |             binary 0, instead of comparing it to 0.0, or, preferred, move | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             such code into an implementation file. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-07 13:34:40 +02:00
										 |  |  |        \li Do not hide virtual functions in subclasses (\{-Woverloaded-virtual}). | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |            If the baseclass A has a virtual \c {int val()} and subclass B an | 
					
						
							|  |  |  |            overload with the same name, \c {int val(int x)}, the A \c val function | 
					
						
							|  |  |  |            is hidden. Use the \c using keyword to make it visible again, and | 
					
						
							|  |  |  |            add the following silly workaround for broken compilers: | 
					
						
							|  |  |  |            \code | 
					
						
							|  |  |  |                class B: public A | 
					
						
							|  |  |  |                { | 
					
						
							|  |  |  |                #ifdef Q_NO_USING_KEYWORD | 
					
						
							|  |  |  |                inline int val() { return A::val(); } | 
					
						
							|  |  |  |                #else | 
					
						
							|  |  |  |                using A::val; | 
					
						
							|  |  |  |                #endif | 
					
						
							|  |  |  |                }; | 
					
						
							|  |  |  |            \endcode | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not shadow variables (\c{-Wshadow}). | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Avoid things like \c {this->x = x;} if possible. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li Do not give variables the same name as functions declared in | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             your class. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |         \li To improve code readability, always check whether a preprocessor | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             variable is defined before probing its value (\c {-Wundef}). | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |             \code | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             #if defined(Foo) && Foo == 0 | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |               -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             #if Foo == 0 | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             -NOT- | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             #if Foo - 0 == 0 | 
					
						
							|  |  |  |             \endcode | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |         \li When checking for a preprocessor define using the \c {defined} | 
					
						
							| 
									
										
										
										
											2013-09-25 18:19:45 +02:00
										 |  |  |             operator, always include the variable name in parentheses. | 
					
						
							| 
									
										
										
										
											2011-11-10 11:35:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             \code | 
					
						
							| 
									
										
										
										
											2015-06-23 14:58:45 +02:00
										 |  |  |             #if defined(Foo) | 
					
						
							| 
									
										
										
										
											2011-11-10 11:35:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |               -NOT- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               #if defined Foo | 
					
						
							|  |  |  |            \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \endlist | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-06 13:18:18 +02:00
										 |  |  |     \section1 Class Member Names | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     We use the "m_" prefix convention, except for public struct members | 
					
						
							|  |  |  |     (typically in *Private classes and the very rare cases of really | 
					
						
							|  |  |  |     public structures). The \c{d} and \c{q} pointers are exempt from | 
					
						
							|  |  |  |     the "m_" rule. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The \c{d} pointers ("Pimpls") are named "d", not "m_d". The type of the | 
					
						
							|  |  |  |     \c{d} pointer in \c{class Foo} is \c{FooPrivate *}, where \c{FooPrivate} | 
					
						
							|  |  |  |     is declared in the same namespace as \c{Foo}, or if \c{Foo} is | 
					
						
							|  |  |  |     exported, in the corresponding \{Internal} namespace. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If needed (for example when the private object needs to emit signals of | 
					
						
							|  |  |  |     the proper class), \c{FooPrivate} can be a friend of \c{Foo}. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     If the private class needs a backreference to the real class, | 
					
						
							|  |  |  |     the pointer is named \c{q}, and its type is \c{Foo *}. (Same convention | 
					
						
							|  |  |  |     as in Qt: "q" looks like an inverted "d".) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Do not use smart pointers to guard the \c{d} pointer as it imposes | 
					
						
							|  |  |  |     a compile and link time overhead and creates fatter object | 
					
						
							|  |  |  |     code with more symbols, leading, for instance to slowed down | 
					
						
							|  |  |  |     debugger startup: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \code | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### bar.h | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #include <QScopedPointer> | 
					
						
							|  |  |  |             //#include <memory> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             struct BarPrivate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             struct Bar | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Bar(); | 
					
						
							|  |  |  |                 ~Bar(); | 
					
						
							|  |  |  |                 int value() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 QScopedPointer<BarPrivate> d; | 
					
						
							| 
									
										
										
										
											2015-05-26 23:31:45 +03:00
										 |  |  |                 //std::unique_ptr<BarPrivate> d; | 
					
						
							| 
									
										
										
										
											2011-07-06 13:18:18 +02:00
										 |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### bar.cpp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #include "bar.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             struct BarPrivate { BarPrivate() : i(23) {} int i; }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Bar::Bar() : d(new BarPrivate) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Bar::~Bar() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int Bar::value() const { return d->i; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### baruser.cpp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #include "bar.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int barUser() { Bar b; return b.value(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### baz.h | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             struct BazPrivate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             struct Baz | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 Baz(); | 
					
						
							|  |  |  |                 ~Baz(); | 
					
						
							|  |  |  |                 int value() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 BazPrivate *d; | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### baz.cpp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #include "baz.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             struct BazPrivate { BazPrivate() : i(23) {} int i; }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Baz::Baz() : d(new BazPrivate) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Baz::~Baz() { delete d; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int Baz::value() const { return d->i; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### bazuser.cpp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #include "baz.h" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int bazUser() { Baz b; return b.value(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             ############### main.cpp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int barUser(); | 
					
						
							|  |  |  |             int bazUser(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             int main() { return barUser() + bazUser(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Results: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         \code | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Object file size: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              14428 bar.o | 
					
						
							|  |  |  |               4744 baz.o | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |               8508 baruser.o | 
					
						
							|  |  |  |               2952 bazuser.o | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Symbols in bar.o: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 00000000 W _ZN3Foo10BarPrivateC1Ev | 
					
						
							|  |  |  |                 00000036 T _ZN3Foo3BarC1Ev | 
					
						
							|  |  |  |                 00000000 T _ZN3Foo3BarC2Ev | 
					
						
							|  |  |  |                 00000080 T _ZN3Foo3BarD1Ev | 
					
						
							|  |  |  |                 0000006c T _ZN3Foo3BarD2Ev | 
					
						
							|  |  |  |                 00000000 W _ZN14QScopedPointerIN3Foo10BarPrivateENS_21QScopedPointerDeleterIS2_EEEC1EPS2_ | 
					
						
							|  |  |  |                 00000000 W _ZN14QScopedPointerIN3Foo10BarPrivateENS_21QScopedPointerDeleterIS2_EEED1Ev | 
					
						
							|  |  |  |                 00000000 W _ZN21QScopedPointerDeleterIN3Foo10BarPrivateEE7cleanupEPS2_ | 
					
						
							|  |  |  |                 00000000 W _ZN7qt_noopEv | 
					
						
							|  |  |  |                          U _ZN9qt_assertEPKcS1_i | 
					
						
							|  |  |  |                 00000094 T _ZNK3Foo3Bar5valueEv | 
					
						
							|  |  |  |                 00000000 W _ZNK14QScopedPointerIN3Foo10BarPrivateENS_21QScopedPointerDeleterIS2_EEEptEv | 
					
						
							|  |  |  |                          U _ZdlPv | 
					
						
							|  |  |  |                          U _Znwj | 
					
						
							|  |  |  |                          U __gxx_personality_v0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Symbols in baz.o: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 00000000 W _ZN3Foo10BazPrivateC1Ev | 
					
						
							|  |  |  |                 0000002c T _ZN3Foo3BazC1Ev | 
					
						
							|  |  |  |                 00000000 T _ZN3Foo3BazC2Ev | 
					
						
							|  |  |  |                 0000006e T _ZN3Foo3BazD1Ev | 
					
						
							|  |  |  |                 00000058 T _ZN3Foo3BazD2Ev | 
					
						
							|  |  |  |                 00000084 T _ZNK3Foo3Baz5valueEv | 
					
						
							|  |  |  |                          U _ZdlPv | 
					
						
							|  |  |  |                          U _Znwj | 
					
						
							|  |  |  |                          U __gxx_personality_v0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           \endcode | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     \section1 Documentation | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     The documentation is generated from source and header files. You document | 
					
						
							|  |  |  |     for the other developers, not for yourself. In the header files, document | 
					
						
							|  |  |  |     interfaces. That is, what the function does, not the implementation. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-10 16:44:35 +01:00
										 |  |  |     In the .cpp files, you can document the implementation if the | 
					
						
							|  |  |  |     implementation is not obvious. | 
					
						
							| 
									
										
										
										
											2011-02-04 15:16:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-11 13:23:03 +01:00
										 |  |  | */ |