Debian Codegraph @

docs/codingstyle.md

raw symbols SCIP
1 # Coding Style {#dev_codestyle}
2
3 Note: Alot of the current source doesn't yet follow alot of this style guide. But going forward efforts will be made to bring the source inline. So please try to stick to this guide with new code additions
4
5 ## Source files
6
7 * Indentations should be four spaces
8 * Source should be encoded in UTF-8
9 * Try to keep line length below 120 chars (this is not a hard limit if it makes sense to break it)
10 * All source files should begin with a license header similar to the [one found here](license.md)
11
12 ## Qt/C++
13
14 ### Namespaces
15
16 All Dekko related modules are located in the `Dekko::` namespace. All submodule namespaces should be part of the include guard.
17
18 Example
19 ```cpp
20 #ifndef ACCOUNTS_UTILS_H
21 #define ACCOUNTS_UTILS_H
22
23 #include <aheader>
24
25 namespace Dekko {
26 namepace Accounts { // first part of incl. guard
27 class Utils { // final part on incl. guard
28 public:
29 void blah();
30 };
31 }
32 }
33 #endif // ACCOUNTS_UTILS_H
34 ```
35
36 Changes modules should include the top level namespace in the include guard.
37
38 ### Includes & Foward declarations
39
40 Should be sorted alphabetically and includes **above** forward declarations.
41
42 ```cpp
43 #include <IncludeA>
44 #include <IncludeB>
45
46 class ForwardA;
47 class ForwardB;
48 ```
49 ### C++ Header full example
50
51 ```cpp
52 /* Copyright (C) 2014-2016 Dan Chapman [email protected]
53
54 This file is part of Dekko email client for Ubuntu devices
55
56 This program is free software; you can redistribute it and/or
57 modify it under the terms of the GNU General Public License as
58 published by the Free Software Foundation; either version 2 of
59 the License or (at your option) version 3
60
61 This program is distributed in the hope that it will be useful,
62 but WITHOUT ANY WARRANTY; without even the implied warranty of
63 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64 GNU General Public License for more details.
65
66 You should have received a copy of the GNU General Public License
67 along with this program. If not, see http://www.gnu.org/licenses/.
68 */
69 #ifndef ACCOUNTS_UTILS_H
70 #define ACCOUNTS_UTILS_H
71
72 #include <aheader>
73 #include <QObject>
74
75 namespace Dekko
76 {
77 namepace Accounts
78 {
79 class Utils: public QObject
80 {
81 Q_PROPERTY(Mode mode READ mode WRITE setMode NOTIFY modeChanged)
82 Q_ENUMS(Mode)
83
84 public:
85 explicit Utils(QObject *parent = 0);
86 ~Utils();
87 // public enums after constructor
88 enum Mode {
89 FIRST,
90 SECOND,
91 THIRD
92 }
93
94 // qproperty getters
95 Mode mode() const;
96
97 // QML invokable functions
98 Q_INVOKABLE int getAge(const Account *account);
99
100 // c++ public functions last
101 bool migrateAccount(const Account *account);
102
103 signals:
104 void modeChanged();
105
106 public slots:
107 // qproperty setters
108 void setMode(const Mode &mode);
109
110 private:
111 // private members start with m_*
112 Mode m_mode;
113 };
114 }
115 }
116 #endif // ACCOUNTS_UTILS_H
117 ```
118
119 ### C++ Code full example
120
121 ```cpp
122 /* Copyright (C) 2014-2016 Dan Chapman [email protected]
123
124 This file is part of Dekko email client for Ubuntu devices
125
126 This program is free software; you can redistribute it and/or
127 modify it under the terms of the GNU General Public License as
128 published by the Free Software Foundation; either version 2 of
129 the License or (at your option) version 3
130
131 This program is distributed in the hope that it will be useful,
132 but WITHOUT ANY WARRANTY; without even the implied warranty of
133 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134 GNU General Public License for more details.
135
136 You should have received a copy of the GNU General Public License
137 along with this program. If not, see http://www.gnu.org/licenses/.
138 */
139
140 #include Utils.h
141
142 namespace Dekko
143 {
144 namespace Accounts
145 {
146
147 Utils(QObject *parent): QObject(parent), m_mode(Mode::FIRST)
148 {
149 }
150
151 Mode mode() const {
152 return m_mode;
153 }
154
155 int getAge(const Account *account) {
156 Q_UNUSED(account);
157 return 100; // haha!
158 }
159
160 bool migrateAccount(const Account *account) {
161 if (account.isValid()) {
162 return account->migrate();
163 } else {
164 qFatal("Use a valid account stupid!");
165 }
166 Q_UNREACHABLE(true)
167 // Trivial blocks can use the compact format & drop the curly braces
168 if (account.isValid())
169 return;
170 }
171
172 void setMode(const Mode &mode) {
173 if (mode != m_mode) {
174 m_mode = mode;
175 emit modeChanged();
176 }
177 }
178 }
179 }
180 ```
181
182 ## QML
183
184 Dekko follows [Qt's QML coding conventions](http://doc.qt.io/qt-4.8/qml-coding-conventions.html) with a few additions. I've include it all her for offline viewing.
185
186 ### QML Objects
187
188 Through our documentation and examples, QML objects are always structured in the following order:
189
190 * id
191 * property declarations
192 * signal declarations
193 * JavaScript functions
194 * object properties
195 * child objects
196 * states
197 * transitions
198
199 For better readability, we separate these different parts with an empty line.
200
201 For example, a hypothetical photo QML object would look like this:
202
203 ```javascript
204 Rectangle {
205 id: photo // id on the first line
206
207 // ###### property declarations ######
208 property bool thumbnail: false
209 property alias image: photoImage.source
210
211 // ###### signal declarations ######
212 signal clicked() // Dekko includes the brackets
213
214 // ###### javascript functions ######
215 function doSomething(x){ // opening brace inline
216 return x + photoImage.width
217 }
218
219 // ###### object properties ######
220 color: "gray"
221 x: 20; y: 20; height: 150 // try to group related properties together
222 width: {
223 if(photoImage.width > 200){
224 photoImage.width;
225 }else{
226 200;
227 }
228 }
229
230 // ###### child objects ######
231 Rectangle {
232 id: border
233 anchors.centerIn: parent; color: "white"
234
235 Image { id: photoImage; anchors.centerIn: parent }
236 }
237
238 // ###### states ######
239 states: State {
240 name: "selected"
241 PropertyChanges { target: border; color: "red" }
242 }
243
244 // ###### transitions ######
245 transitions: Transition {
246 from: ""; to: "selected"
247 ColorAnimation { target: border; duration: 200 }
248 }
249 }
250 ```
251 ### Grouped properties
252
253 If using multiple properties from a group of properties, we use the group notation rather than the dot notation to improve readability.
254
255 ```javascript
256 Rectangle {
257 anchors { left: parent.left; top: parent.top; right: parent.right; leftMargin: 20 }
258 }
259
260 Text {
261 text: "hello"
262 font { bold: true; italic: true; pixelSize: 20; capitalization: Font.AllUppercase }
263 }
264 ```
265 ### Private properties
266
267 QML and JavaScript do not enforce private properties like C++. There is a need to hide these private properties, for example, when the properties are part of the implementation. To effectively gain private properties in a QML Item, the convention is to add a QtObject{} child to contain the properties. This shields them from being accessed outside the file in QML and JavaScript. As it involves the creation of another object, it is more expensive than just creating a property. To minimize the performance cost, try to group all private properties in one file into the same QtObject.
268
269 ```javascript
270 Item {
271 id: component
272 width: 40; height: 50
273 QtObject {
274 id: d
275 property real area: width * height * 0.5 //not meant for outside use
276 }
277 }
278 ```
279 ### Lists
280
281 If a list contains only one element, we generally omit the square brackets.
282
283 ```javascript
284 states: State {
285 name: "open"
286 PropertyChanges { target: container; width: 200 }
287 }
288 ```
289
290 ## Javascript
291
292 If the script is a single expression, we recommend writing it inline:
293
294 ```javascript
295 Rectangle { color: "blue"; width: parent.width / 3 }
296 ```
297
298 If the script is only a couple of lines long, we generally use a block:
299
300 ```javascript
301 Rectangle {
302 color: "blue"
303 width: {
304 var w = parent.width / 3
305 console.debug(w)
306 return w
307 }
308 }
309 ```
310
311 If the script is more than a couple of lines long or can be used by different objects, we recommend creating a function and calling it like this:
312
313 ```javascript
314 function calculateWidth(object){
315 var w = object.width / 3
316 // ...
317 // more javascript code
318 // ...
319 console.debug(w)
320 return w
321 }
322
323 Rectangle { color: "blue"; width: calculateWidth(parent) }
324 ```
325 For long scripts, we will put the functions in their own JavaScript file and import it like this:
326
327 ```javascript
328 import "myscript.js" as Script
329
330 Rectangle { color: "blue"; width: Script.calculateWidth(parent) }
331 ```
332
333 In Dekko we try to use as little javascript as possible. And a general rule is if a javscript function is getting too large that it should probably go into a seperate file like above. Then it is better to do it in c++ and expose it via singleton class or a utility class that's already exposed to QML.