Warning, /education/labplot/src/doc/coding_style.dox is written in an unsupported language. File is not indexed.
0001 /**\page coding_style Coding Style 0002 0003 The following rules are not used everywhere (yet), but are intended as guidelines for new code and eventually 0004 old code should be adapted as well. They apply to C++ and C code. The standards are C++11 and C99. 0005 0006 \section files Files 0007 0008 - Files use Unix-style line endings ('\\n'). 0009 - C++ source files use “.cpp” as extension, C source code use "*.c" and header files use “.h”. 0010 - The code is documented using Doxygen comments which are placed in the source files, not the header files. 0011 - Every file should be named exactly like the class inside and there should be only one class per file, with the exception of really short classes. 0012 Very short classes can be bundled in one file which then is named using all lower case letters. 0013 0014 \section identifier Identifier names 0015 0016 - Class names start with a capital letter and use CamelCase, acronyms in class names are use like normal words. Example: MySuperHtmlToPdfConverter 0017 - Function/method names start with a lower case letter and use CamelCase Example: doSomethingImportant() 0018 - Variable/object names start with a lower case letter and use CamelCase, underscores are used for special prefixes only. Example: numberOfPoints 0019 - Private class member variables are prefixed with “m_” to distinguish them easily. d-pointer and UI-widgets are called d and ui, respectively, i.e. without prefix. 0020 - Property access methods use Qt style: property() and setProperty(), except for boolean properties (isVisible(), hasChanged()). Accessor functions (getter/setter) can be done using macros. 0021 - Avoid abbreviations, except for local counters and temporaries whose purpose is obvious. 0022 0023 \section indent Indentation, spacing and line breaks 0024 0025 - Tabs are used for indentation because they allow everyone to choose the indentation depth for him/herself. 0026 - Try to keep lines shorter than 100 characters, inserting line breaks as necessary and indent the following lines to improved readability. 0027 - included headers should be in order: own header, local header, Qt/KDE header, system header, extern header 0028 - Opening braces (‘{‘) are placed behind the statement and are preceded by a space. This also goes for function implementations, class, struct and namespace declarations, which are exceptions in other coding styles. Example: 0029 @code 0030 void MyClass::doSomething() { 0031 if (condition) { 0032 ... 0033 } 0034 ... 0035 } 0036 @endcode 0037 - Opening brackets (‘(‘) are preceded by a space in for/switch/if/while statements, but not for function calls. 0038 Example: 0039 @code 0040 if (condition) { 0041 doSomething(myData); 0042 ... 0043 } 0044 @endcode 0045 - For pointers or references, use a single space after ‘*’ or ‘&’ (i.e. specifier is bound to the data type not the name). Example: 0046 @code 0047 void doSomething(int* dataPointer, const QString& name); 0048 ... = static_cast(...) 0049 @endcode 0050 “public” and namespace enclosures are not indented. Example: 0051 @code 0052 class MyClass: public QObject { 0053 public: 0054 void doSomething(); 0055 @endcode 0056 “case” of switch is not indented. “default” should be present only if data type is not an enum. Example: 0057 @code 0058 switch (condition) { 0059 case 1: 0060 handleCaseOne(); 0061 break; 0062 case 2: { 0063 int i=0; 0064 ... 0065 break; 0066 } 0067 ... 0068 default: 0069 ... 0070 } 0071 @endcode 0072 - Each comma in a function call or semicolon in a for statement is followed by a space character; no space before the first and after the last argument. Example: 0073 @code 0074 for (int i = 0; i < 10; i++) { 0075 ... 0076 doSomething(arg1, arg2, arg3); 0077 } 0078 @endcode 0079 "else" (and "catch" if it is ever used) is put after the closing brace like this: "} else {" 0080 - Use as many brackets in conditions/math terms as you see fit for optimum readability. All operators ('=', '==', '<', '+', '-', '<<', etc.) and castings should always be surrounded by spaces. Examples: 0081 @code 0082 foo/2 + bar/4 + baz/3 0083 for (int i = 0; i < bar+1; i++) 0084 var = (foo - 1) + (bar - 2) + (baz - 3) 0085 char *s = (char*) malloc(LENGTH * sizeof(char)); 0086 @endcode 0087 - enum and structs should be defined first in a class. use strongly typed enums ("enum class") 0088 - parameter names in a method definition should only be used to explains the usage of the parameter 0089 - In SIGNAL() and SLOT() macros, use as little whitespace as possible. This gives a little speed up since Qt does not have to normalize the signal/slot name. 0090 0091 \section constructs Usage of specific constructs 0092 0093 * Qt version features can be checked with "#if QT_VERSION >= 0x050403" (meaning Qt 5.4.3) 0094 * For integer data types int is preferred for indices and normal numbers, size_t for big, unsigned values and quint64 for datetime values. Use double (qreal) as floating point type. 0095 Compare floating point numbers with qFuzzyCompare() or qFuzzyIsNull(). 0096 * Use C++ casting (static_cast, const_cast, dynamic_cast) and qobject_cast in Qt classes since they include checks 0097 see https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Statements/Variables/Type_Casting 0098 * Use Qt container instead of STL container 0099 https://marcmutz.wordpress.com/effective-qt/containers/ 0100 See also "Hands-On High Performance Programming with Qt5" 0101 * QVector: size() instead of count(), resize() for simple types (bool, int, double) and reserve() else, use at() for ro access, to release memory use squeeze() 0102 * QString: https://wiki.qt.io/Using_QString_Effectively, https://forum.qt.io/topic/78540/qstringliteral-vs-qlatin1string/3, https://www.qt.io/blog/2014/06/13/qt-weekly-13-qstringliteral, https://woboq.com/blog/qstringliteral.html 0103 * Use range-based loops instead of foreach/Q_FOREACH (but avoid detach: https://www.kdab.com/goodbye-q_foreach/ - "Avoid Detach") 0104 https://www.kdab.com/goodbye-q_foreach/ 0105 * The 'auto' keyword should be used in range-based loops and for variables initialized by casting or with the 'new' operator but only for non-basic types (int,double,Spreadsheet). Do not omit '*', '&' and 'const' to keep readability. 0106 * use smart pointers unique_ptr when possible and shared_ptr otherwise. 0107 * Avoid const pass-by-value parameters in function declarations. Still make the parameter const in the same function's definition if it won't be modified. 0108 * Use the 'override' specifier when overriding virtual functions from the base class 0109 http://en.cppreference.com/w/cpp/language/override 0110 * Use braces to enclose a single statement only for readability 0111 * In C++ nullptr should be used instead of bug-prone NULL and 0. 0112 * Use brace initializing for default values (but avoid default initialization like bool/int/double = false/0/0.0) and use them when reading config settings: see TextLabel::init() 0113 Examples: int{0}, double{0.0}, color{Qt::black}, font{"Times", 12}, point{QPoint(1, 1)} 0114 Run time settings like QApplication::desktop()->physicalDpiX() can be used too. 0115 Attention: v{2} initializes a vector to one element of value 2, use v{0., 0.} for two values initalized to 0. 0116 * Direct brace initialization (QString fileName{name};) is preferred over assignment initialization. (see also https://www.learncpp.com/cpp-tutorial/variable-assignment-and-initialization/) 0117 * #include <...> vs. #include "...": Include headers from external libraries using angle brackets (as in #include <QDebug>) and headers from LabPlot/SciDAVis using double quotes (as in #include "core/AbstractAspect.h"). Rationale: Headers of external libraries are never in the same directory as the including file, so it makes sense to use the angle bracket form (which searches only in directories specified using -I). If you work with a build system that does not include the current source directory, or disable CMAKE_INCLUDE_CURRENT_DIR, then all angle-bracket-includes referencing LabPlot/SciDAVis headers will break. Excluding the current directory from -I dirs may be desirable one day when using a new library or a new version of a library containing a header file with the same name as one of our headers. 0118 * Use DEBUG() macro for debugging code when possible and QDEBUG() only for special Qt data types. DEBUG() works on all supported systems. 0119 @code 0120 QString string; 0121 DEBUG(" string : " << STDSTRING(string)) 0122 @endcode 0123 * Use Qt functions for user messages: qDebug(), qWarning(), qCritical(), qFatal(). Check conditions with Q_ASSERT(cond) or Q_ASSERT_X(cond, where, what) and pointers with Q_CHECK_PTR(ptr). 0124 * Numeric limits should be using cmath +/-INFINITY and NAN for floating point types and C++ +/-std::numeric_limits<T>::min() & +/-std::numeric_limits<T>::max() for all types 0125 * Import C header (from GSL etc.) with extern statement. We use "std::" prefix (C++11) and try to use c-header <cmath> (std::abs(), DBL_MAX -> std::numeric_limits<double>::max(), std::round(), max() -> std::max(), std::isfinite(), std::isnan(), std::isinf()). Example: 0126 @code 0127 extern "C" { 0128 #include <gsl/gsl_version.h> 0129 } 0130 0131 if (std::isnan(x)) { 0132 ... 0133 } 0134 @endcode 0135 * Use QOverload<int>::of instead of (for instance) static_cast<void (QComboBox::*)(int)> in connect() 0136 \section links Links 0137 0138 Apart from that, the following links are recommended as guidelines for the coding style: 0139 0140 https://wiki.qt.io/Qt_Coding_Style 0141 0142 https://wiki.qt.io/Coding_Conventions 0143 0144 https://techbase.kde.org/Policies/Frameworks_Coding_Style 0145 0146 https://community.kde.org/Policies/Library_Code_Policy 0147 0148 https://techbase.kde.org/Development/Tutorials/Common_Programming_Mistakes 0149 0150 https://community.kde.org/Krita/C%2B%2B11 0151 */