SVN_SILENT made messages (.desktop file)
[kdegames.git] / killbots / README.codestyle
blobcd73cf24f7800498beb508b0d121567164e47b95
1 ==========================
2  Killbots C++ Style Guide
3 ==========================
5 *******************************************************************************
6  Indentation
7 *******************************************************************************
9 Structural indentation is done with tabs. Formatting is done with spaces. All
10 code having the same nested depth has the same number of tabs. This separates
11 meaning from presentation.
13 Tabwidth is a matter of personal preference. If the above is followed source is
14 tabwidth independent.
16 Ensure your editor doesn't do automatic conversion from spaces to tabs, as this
17 will break things.
19 Labels (public, private, goto, case, etc.) do not get indented.
21 Empty lines do not get indentation. In fact, no lines should have trailing
22 whitespace.
24 If a pair of parentheses span multiple lines, the closing parenthesis should be
25 indented (with spaces) to appear in the same column as the opening parenthesis.
27 Examples:
28 -------------------------------------------------------------------------------
29 bool Killbots::Engine::cellIsValid( const QPoint & cell ) const
31 ____return ( 0 <= cell.x()
32 ____.........&& cell.x() < m_rules->columns()
33 ____.........&& 0 <= cell.y()
34 ____.........&& cell.y() < m_rules->rows()
35 ____.......);
37 -------------------------------------------------------------------------------
38 namespace Killbots
40 ____class RenderPrivate
41 ____{
42 ____public:
43 ________RenderPrivate()
44 ________..:.m_svgRenderer(),
45 ________....m_pixmapCache("killbots-cache"),
46 ________....m_cursors(),
47 ________....m_hasBeenLoaded( false )
48 ________{};
49 -------------------------------------------------------------------------------
50 ____if ( m_rulesetChanged )
51 ____{
52 ________if ( ! m_engine->gameHasStarted() ||
53 ________.....KMessageBox::questionYesNo( this,
54 ________.................................i18n("A new ruleset has been selected, but there is already a game in progress."),
55 ________.................................i18n("Rule Set Changed"),
56 ________.................................KGuiItem( i18n("Continue Current Game") ),
57 ________.................................KGuiItem( i18n("Start a New Game") )
58 ________...............................) == KMessageBox::No
59 ________...)
60 ________{
61 -------------------------------------------------------------------------------
64 *******************************************************************************
65  Braces
66 *******************************************************************************
68 Opening and closing braces are always on a new line.
70 For loop or conditional statements, braces can be omitted if the block is a
71 single-line statement and the conditional fits on a single line. If braces are
72 used in one block of an if-elseif-else statement, they should be used in the
73 other blocks even if those blocks are a single line.
75 Blocks never appear on the same line as the loop/conditional, even if braces
76 aren't used.
78 Empty blocks are represented on a single line as '{}'
80 Examples:
81 -------------------------------------------------------------------------------
82 void Killbots::Engine::refreshSpriteMap()
84     m_spriteMap.clear();
85     foreach( Sprite * bot, m_bots )
86         m_spriteMap.insert( bot->gridPos(), bot );
87     foreach( Sprite * junkheap, m_junkheaps )
88         m_spriteMap.insert( junkheap->gridPos(), junkheap );
90 -------------------------------------------------------------------------------
91 if ( m_rules->pushableJunkheaps() != Ruleset::None && cellIsValid( nextCell ) )
93     if ( spriteTypeAt( nextCell ) == NoSprite )
94         return true;
95     else if ( spriteTypeAt( nextCell ) == Junkheap )
96         return m_rules->pushableJunkheaps() == Ruleset::Many && canPushJunkheap( m_spriteMap.value( nextCell ), direction );
97     else
98         return m_rules->squaskKillsEnabled();
100 else
102     return false;
104 -------------------------------------------------------------------------------
105 while ( m_rulesetMap.contains( name ) )
106     name += '_';
107 -------------------------------------------------------------------------------
109 *******************************************************************************
110  Whitespace
111 *******************************************************************************
113 Lines should generally be kept to less than 100 characters, but don't make code
114 uglier just to avoid width.
116 Two empty lines should be used to separate function definitions.
118 Within a block, empty lines may be used to break up unrelated lines, but never
119 more than one.
121 No space appears between the function name and the opening bracket.
123 A single space appears between the loop/conditional statement name and the
124 opening bracket.
126 Padding spaces appear inside of all parentheses, unless the parentheses are
127 empty or contain only a string literal.
129 Binary operators are separated from their operands with a single space. Unary
130 operators are not.
132 Pointer and reference symbols have a space on either side.
134 No padding spaces appear inside angle brackets when using template functions
135 or classes.
137 No space appears between a label and the following colon.
139 No whitespace should appear inside Qt's SIGNAL and SLOT macros.
142 *******************************************************************************
143  Indentifiers
144 *******************************************************************************
146 All identifiers should use camel case.
148 Classnames and namespaces begin with uppercase letters. All other identifiers
149 begin with lowercase letters.
151 Class data members are prefixed with 'm_', unless they are public KConfigXT
152 widgets, in which case, they are prefixed with 'kfcg_'.
154 Indentifiers should favour descriptiveness over brevity. Single letter
155 variables are acceptable only for iterators or coordinates.
157 All indentifiers should use American English spelling. (Comments can be in your
158 choice of English.)
160 Examples:
161 -------------------------------------------------------------------------------
162 namespace Killbots
164     class Ruleset;
166     class RulesetSelector : public QWidget
167     {
168         Q_OBJECT
170     public: // functions
171         explicit RulesetSelector( QWidget * parent = 0 );
172         virtual ~RulesetSelector();
174     public: // data members
175         KLineEdit * kcfg_Ruleset;
177     private: // functions
178         void findRulesets();
180     private slots:
181         void selectionChanged( QString rulesetName );
183     private: // data members
184         QListWidget * m_listWidget;
185         QLabel * m_author;
186         QLabel * m_authorContact;
187         QLabel * m_description;
188         QMap< QString, Ruleset * > m_rulesetMap;
189     };
191 -------------------------------------------------------------------------------
194 *******************************************************************************
195  Comments
196 *******************************************************************************
198 Feel free to comment as much as possible.
200 Comments should describe the purpose of logic or give background details not
201 obvious from the code. Comments should not describe what the code is doing,
202 unless the code is extremely dense (in which case the code should probably be
203 rewritten/refactored anyway).
205 Comments appear on the line before the code it is commenting on.
207 Use '/**/' for comments longer than 3 lines.
210 *******************************************************************************
211  Class Definitions
212 *******************************************************************************
214 Classes should be given simple names and placed inside the Killbots namespace
215 instead of adding a prefix. (i.e. Killbots::MainWindow instead of
216 KillbotsMainWindow)
218 No inline function definitions in the header, even if they are a single
219 statement. Same goes for constructors.
221 Define destructors even if they're empty.
223 Class definitions should be layed out in the following order.
224   public: // types
225   public: // functions
226   public slots:
227   public: // data members
228   signals:
229   protected: // types
230   protected: // functions
231   protected slots:
232   protected: // data members
233   private: // types
234   private: // functions
235   private slots:
236   private: // data members
239 *******************************************************************************
240  Includes
241 *******************************************************************************
243 Include guards are of the form NAMESPACE_CLASSNAME_H.
245 Group includes in the following order with blank lines in between:
246   File's own header
247   Killbots headers
248   KDE headers
249   Qt headers
251 Inside of groups, sort includes alphabetically.
253 Use the "new" style includes with directory names where appropriate
255 Forward declare whenever possible in headers.
257 moc includes appear at the bottom of the source file.
259 Examples:
260 -------------------------------------------------------------------------------
261 #ifndef KILLBOTS_RULESETSELECTOR_H
262 #define KILLBOTS_RULESETSELECTOR_H
264 class KLineEdit;
266 #include <QtCore/QMap>
267 class QLabel;
268 class QListWidget;
269 #include <QtGui/QWidget>
271 namespace Killbots
273     class Ruleset;
274 -------------------------------------------------------------------------------
275 #include "rulesetselector.h"
277 #include "ruleset.h"
278 #include "settings.h"
280 #include <KDE/KDebug>
281 #include <KDE/KDialog>
282 #include <KDE/KLineEdit>
283 #include <KDE/KLocalizedString>
284 #include <KDE/KStandardDirs>
286 #include <QtGui/QGroupBox>
287 #include <QtGui/QLabel>
288 #include <QtGui/QLayout>
289 #include <QtGui/QListWidget>
290 #include <QtGui/QScrollArea>
291 -------------------------------------------------------------------------------
294 *******************************************************************************
295  Miscellaneous
296 *******************************************************************************
298 It is generally preferred that functions have a single return statement, but
299 multiple returns statements are exceptable if a single return would make the
300 code more complicated.
302 Use the "Q_UNUSED" macro on unused function parameters.
304 Use "static_cast<>" instead of the C or C++ style type casts when casting
305 pointers.
307 When converting one type to another use C++ or constructor style casts. For
308 example, use "int()" to convert floating types to integers.
310 Make sure code passes all the English Breakfast Network checks.