The increasing number of contributors require that we clearly define coding rules and guidelines. Although for historical reasons the current code of Stellarium does not always comply to these rules, they should now be respected for any addition or modification of the code.
Source code should use ASCII character set. Characters such as 'é' or 'ö' are not portable when hard-coded. Gettext translated strings should be used for such characters.
Variable names and comments should be in English.
Class names are nouns, in mixed-case, with an initial upper-case letter and the ﬁrst letter of each subsequent word capitalized (e.g. CoreFactory).
Method names are verbs or nouns in mixed-case, starting with a lower-case letter (e.g. update() or addElement()).
Methods that return a value should take the form getSize().
The names of local variables should be in mixed case, starting with a lower-case letter (e.g. packetSize). This also applies to the formal parameters of methods. Do not use names starting with underscore.
The names of macro or static const should be all upper-case words, separated by underscore:
#define MIN_WIDTH 3
static const QString VERSION = "0.10.1";
Indentation should be done with tabs, not spaces. This allows each developers to use his favorite indent size without changing the code.
When wrapping lines from long function calls, where the wrapped line does not start at the same level of indentation as the start of the function call, tab up to the start of the function call, and then use spaces to the opening parenthesis.
This method will handle different tab widths gracefully.
[--tab-][--tab-][--tab-][ spaces ]moreparams, ...);
Use the following layout for braces:
void MyClass::myMethod(int x)
cout << "You won." << endl;
Use blank lines as follows:
1 between methods, before (block or single line) comment
1 between logical sections of a method
2 between sections of a source ﬁle
enums should follow the QT conventions. i.e. CamelCase with First letter capitalization for both enum type and enum values. Document with doxygen. The //!< tag can be used to add descriptions on the same line as an enum value, e.g.
//! @enum EnumName Here is a description of the enum
EnumValueOne, //!< Some doxygen description of EnumValueOne
EnumValueTwo, //!< Some doxygen description of EnumValueTwo
EnumValueThree //!< Some doxygen description of EnumValueThree
You can use the astyle program to format your code according to these conventions. Use the following options:
Note that this command will replace the file source.cpp with the re-formatted one, and create a backup of the original with the .orig suffix. Also note that the -U option (used to un-pad parenthesis) is only available recent version of astyle.
astyle --style=ansi -tU source.cpp
The extensions are .hpp/.cpp for C++ headers/code, .h/.c for C headers/code. C++ files should have the same name and case than the class they contain. For example class StelFontMgr should be declared in file StelFontMgr.hpp and implemented in StelFontMgr.cpp.
Stellarium source code should be documented with Doxygen
. From Doxygen webpage:
"Doxygen is a documentation system for C++, C, Java, [...] It can generate an on-line documentation browser (in HTML) and/or an off-line reference manual (in LaTeX) from a set of documented source files. [...] The documentation is extracted directly from the sources, which makes it much easier to keep the documentation consistent with the source code. [...] You can also visualize the relations between the various elements by means of include dependency graphs, inheritance diagrams, and collaboration diagrams, which are all generated automatically.
All public and protected classes and methods from Stellarium should be fully documented in the headers (.hpp).
There are different ways to comment C++ code with Doxygen, in Stellarium use the following for headers files:
//! Find and return the list of at most maxNbItem objects auto-completing the passed object I18n name.
//! @param objPrefix the case insensitive first letters of the searched object.
//! @param maxNbItem the maximum number of returned object names.
//! @return a vector of matching object name by order of relevance, or an empty vector if nothing match.
QList<QString> listMatchingObjectsI18n(const QString& objPrefix, unsigned int maxNbItem=5) const;
Brief descriptions are single line only, and stop at the first full stop (period). Any subsequent sentences which occur before @param or a similar tag are considered to be part of a detailed description.
For methods definitions in .cpp files, a simpler comment for each method is sufficient:
QList<QString> listMatchingObjectsI18n(const QString& objPrefix, unsigned int maxNbItem=5) const
Use C++ replacement for C functions and Qt replacements for C++ function/STL wherever possible.
Use QString instead of
Use QIODevice instead of C file managment with
Pass objects as references when needed instead of using pointers.
Include standard headers the C++ way, it is more portable:
Use Qt containers instead of STL ones. They are easier to use, allow for the foreach keyword. Only if speed is really critical, use STL containers such as
std::map, they are extremely efficient. Documentation is there.
Avoid public global function and variable. Encapsulate them in classes or namespaces as static members/variable.
Avoid using C macro, use static const variables instead. It is safer because it is type safe.
#define RADIUS 12 // Bad
static const int RADIUS = 12; // Good
Use stdc++ math functions instead of C ones. There are more portable and are also overrided for float, thus may be faster.
double cosLat = cos(lat);
double cosLat = std::cos(lat);
Translatable strings are translated using the Translator class, which is a C++ wrapper around gettext. The strings should be marked using the
macro: it takes a QString in English and returns the translation as a QString using the current global language.
Translatable text in sources or data files should be written in English, encoded in ASCII or UTF-8 if needed.
Do not concatenate strings, use
QString::arg() instead. Concatenated strings are very hard (or even impossible) to translate.
text << q_("Distance: ") << distance << q_("AU");
text = q_("Distance: %1AU").arg(distance);
When using Qt format strings, if a letter follows a digit immediately, xgettext might erroneously mark the extracted string as a C format string. ''Depending on the actual translation, this might cause errors when uploading the message catalog to Rosetta, or when compiling it to binary format.'' To prevent this, add an
xgettext:no-c-format comment to the line preceding the format string:
text = q_("Distance: %1AU").arg(distance);
Translatable text should obey English typo conventions. For example, there should be no space before the colon:
QString myTranslatedText(q_("Distance of the planet :"));
QString myTranslatedText(q_("Distance of the planet:"));
Translatable strings should be modified only if really necessary. Any modification to one of them means that all the translators for all the langages will have to re-translate the new string.
In general no translated text should be output on the console because there are problems when string and wstring are output on the same console. This means that you should never use wcout, wcerr or wprintf(). Console output should be used for informations, errors and warnings which are not required by the user in nominal use.
Errors and warnings should be output in the stderr file, not stdout.
std::cout << "Error while opening file " << qPrintable(myFileName) << "." << std::endl;
std::cerr << "Error while opening file " << qPrintable(myFileName) << "." << std::endl;