4 Please respect these guidelines when coding for Amarok, thanks!
6 * Where this document isn't clear, refer to Amarok code.
9 This C++ FAQ is a life saver in many situations, so you want to keep it handy:
11 http://www.parashift.com/c++-faq-lite/
17 * Indentation is 4 spaces
18 * Lines should be limited to 90 characters
19 * Spaces between brackets and argument functions
20 * For pointer and reference variable declarations put a space between the type
21 and the * or & and no space before the variable name.
22 * For if, else, while and similar statements put the brackets on the next line,
23 although brackets are not needed for single statements.
24 * Function and class definitions have their brackets on separate lines
25 * A function implementation's return type is on its own line.
26 * amarok.h contains some helpful macros, foreach and foreachType. Use them,
27 they improve coding style and readability.
28 * CamelCase.{cpp,h} style file names.
33 | MyClass::myMethod( QPtrList<QListViewItem> items, const QString &name )
35 | if( items.isEmpty() )
38 | foreachType( QPtrList<QListViewItem>, items )
40 | (*it)->setText( 0, name );
41 | debug() << "Setting item name: " << name << endl;
45 Header includes should be listed in the following order:
50 They should also be sorted alphabetically, for ease of locating them. A small comment
51 if applicable is also helpful.
53 Includes in a header file should be kept to the absolute minimum, as to keep compile times
54 low. This can be achieved by using "forward declarations" instead of includes, like
55 "class QListView;" Forward declarations work for pointers and const references.
58 Kate/KDevelop users can sort the headers automatically. Select the lines you want to sort,
59 then Tools -> Filter Selection Through Command -> "sort".
66 | #include "playlist.h"
68 | #include <KDialogBase> //baseclass
69 | #include <KPushButton> //see function...
71 | #include <QGraphicsView>
77 Comment your code. Don't comment what the code does, comment on the purpose of the code. It's
78 good for others reading your code, and ultimately it's good for you too.
80 Comments are essential when adding a strange hack, like the following example:
82 | /** Due to xine-lib, we have to make KProcess close all fds, otherwise we get "device is busy" messages
83 | * Used by AmarokProcIO and AmarokProcess, exploiting commSetupDoneC(), a virtual method that
84 | * happens to be called in the forked process
85 | * See bug #103750 for more information.
87 | class AmarokProcIO : public K3ProcIO
90 | virtual int commSetupDoneC() {
91 | const int i = K3ProcIO::commSetupDoneC();
92 | Amarok::closeOpenFiles(K3ProcIO::out[0],K3ProcIO::in[0],K3ProcIO::err[0]);
98 For headers, use the Doxygen syntax. See: http://www.stack.nl/~dimitri/doxygen/
104 | * @param offset Start playing at @p msec position.
105 | * @return True for success.
107 | virtual bool play( uint offset = 0 ) = 0;
112 General rules apply here. Please keep header function definitions aligned nicely,
113 if possible. It helps greatly when looking through the code. Sorted methods,
114 either by name or by their function (ie, group all related methods together) is
118 | #ifndef AMAROK_QUEUEMANAGER_H
119 | #define AMAROK_QUEUEMANAGER_H
121 | class QueueList : public K3ListView
126 | Queuelist( QWidget *parent, const char *name = 0 );
130 | void moveSelectedUp();
131 | void moveSelectedDown();
134 #endif /* AMAROK_QUEUEMANAGER_H */
139 The use of 0 to express a null pointer is preferred over the use of NULL.
140 0 is not a magic value, it's the defined value of the null pointer in C++.
141 NULL, on the other hand, is a preprocessor directive (#define) and not only is
142 it more typing than '0' but preprocessor directives are less elegant.
144 | SomeClass *instance = 0;
149 Try to keep your code const correct. Declare methods const if they don't mutate the object,
150 and use const variables. It improves safety, and also makes it easier to understand the code.
152 See: http://www.parashift.com/c++-faq-lite/const-correctness.html
158 | MyClass::isValidFile( const QString& path ) const
160 | const bool valid = QFile::exist( path );
168 debug.h contains some handy functions for our debug console output.
169 Please use them instead of kDebug().
175 | debug() << "Something is happening" << endl;
176 | warning() << "Something bad may happen" << endl;
177 | error() << "Something bad did happen!" << endl;
179 Additionally, there are some macros for debugging functions:
187 AMAROK_NOTIMPLEMENTED
190 threadweaver.h has two additional macros:
191 DEBUG_THREAD_FUNC_INFO outputs the memory address of the current QThread or 'none'
192 if its the original GUI thread.
193 SHOULD_BE_GUI outputs a warning message if it occurs in a thread that isn't in
194 the original "GUI Thread", otherwise it is silent. Useful for documenting
195 functions and to prevent problems in the future.
198 Usage of Amarok::config()
199 -------------------------
200 We provide this method for convenience, but it is important to use it properly. By
201 inspection, we can see that we may produce very obscure bugs in the wrong case:
204 | *config( const QString &group )
206 | //Slightly more useful config() that allows setting the group simultaneously
207 | KGlobal::config()->setGroup( group );
208 | return KGlobal::config();
211 Take the following example:
216 | KConfig *config = Amarok::config( "Group 2" );
217 | config->writeEntry( "Group 2 Variable", true );
223 | KConfig *config = Amarok::config( "Group 1" );
225 | config->writeEntry( "Group 1 Variable", true );
228 We would expect the following results:
231 | Group 1 Variable = true
234 | Group 2 Variable = true
236 However because the config group is changed before writing the entry:
240 | Group 1 Variable = true
241 | Group 2 Variable = true
243 Which is clearly incorrect. And hard to see when your wondering why f1() is not
244 working. So do not store a value of Amarok::config, make it a habit to just
245 always call writeEntry or readEntry directly.
248 | amarok::config( "Group 1" )->writeEntry( "Group 1 Variable", true );
253 *Never use assert() or fatal(). There must be a better option than crashing a user's
254 application (its not uncommon for end-users to have debugging enabled).
256 *KMessageBox is fine to use to prompt the user, but do not use it to display errors
257 or informational messages. Instead, KDE::StatusBar has a few handy methods. Refer to
258 amarok/src/statusbar/statusBarBase.h
263 To comply with the GPL, add your name, email address & the year to the top of any file
264 that you edit. If you bring in code or files from elsewhere, make sure its
265 GPL-compatible and to put the authors name, email & copyright year to the top of
269 Thanks, now have fun!
270 -- the Amarok developers