[ fromfile: style.xml id: style ]
C++ is a powerful language that supports many different programming styles. The coding style used in most Qt programs is not "pure" C++. Instead, it is common to see a combination of macros and preprocessor trickery to achieve a higher-level dynamic language that has a lot in common with Java and Python. In fact, to take full advantage of Qt's power and simplicity, we have abandoned the Standard Library entirely.
In any serious collaborative programming project there are likely to be style guidelines to improve the readability, reusability, maintainability, and reliability of the code produced. The semi-official Qt programming style guidelines are described in [qtapistyle] and [kdestyle]. Here is a summary of the style guidelines that we have adopted.
Names are sequences of letters and numerical digits, the first of which must not be a digit.
The underscore character ('_') can be used also but we discourage the use of that character except for class data members.
Class names begin with a capital letter: class Customer
Function names begin with a lowercase letter.
Use CamelCase to form multi-word names by joining
the words together and capitalizing the interior word beginnings; e.g., class MetaDataLoader
,
void getStudentInfo()
Constants are capitalized and created, preferably, as enum
values in a class
scope. Global constants and macros are usually all CAPS
.
Each class name should be a noun or a noun phrase: class LargeFurryMammal
for
example.
Each function name should be a verb or a verb phrase: processBookOrder()
for
example.
Each bool
variable name should produce a reasonable approximation of a
sentence when used in an if()
statement: bool isQualified
for example.
We have adopted a modified Hungarian notation for our data members, in which we use a common prefix so that data members are always clearly visible in the code:
data members: m_Color, m_Width
- prepend lowercase m_
static
data members: s_Singleton, s_ObjCount
-
prepend lowercase s_
For each attribute, we have naming conventions for their corresponding getters/setters.
Non-boolean getters: color()
or getColor()
[35]
Boolean getters: isChecked()
or isValid()
setter: setColor(const Color& newColor)
A consistent naming convention greatly improves the readability and maintainability of a program.
Following are some other coding standards that have been in widespread use. Keep in mind, the style most relevant to Qt programming is still [qtapistyle].
[35] The latter is Java style, the former is Qt style. Both conventions are widely used. Try to follow one convention consistently in your code. (We are not consistent in this book because we want to expose the reader to different conventions.)
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |