1 ==========================
2 Killbots C++ Style Guide
3 ==========================
5 *******************************************************************************
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
16 Ensure your editor doesn't do automatic conversion from spaces to tabs, as this
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
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.
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()
37 -------------------------------------------------------------------------------
40 ____class RenderPrivate
43 ________RenderPrivate()
44 ________..:.m_svgRenderer(),
45 ________....m_pixmapCache("killbots-cache"),
46 ________....m_cursors(),
47 ________....m_hasBeenLoaded( false )
49 -------------------------------------------------------------------------------
50 ____if ( m_rulesetChanged )
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
61 -------------------------------------------------------------------------------
64 *******************************************************************************
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
78 Empty blocks are represented on a single line as '{}'
81 -------------------------------------------------------------------------------
82 void Killbots::Engine::refreshSpriteMap()
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 )
95 else if ( spriteTypeAt( nextCell ) == Junkheap )
96 return m_rules->pushableJunkheaps() == Ruleset::Many && canPushJunkheap( m_spriteMap.value( nextCell ), direction );
98 return m_rules->squaskKillsEnabled();
104 -------------------------------------------------------------------------------
105 while ( m_rulesetMap.contains( name ) )
107 -------------------------------------------------------------------------------
109 *******************************************************************************
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
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
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
132 Pointer and reference symbols have a space on either side.
134 No padding spaces appear inside angle brackets when using template functions
137 No space appears between a label and the following colon.
139 No whitespace should appear inside Qt's SIGNAL and SLOT macros.
142 *******************************************************************************
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
161 -------------------------------------------------------------------------------
166 class RulesetSelector : public QWidget
171 explicit RulesetSelector( QWidget * parent = 0 );
172 virtual ~RulesetSelector();
174 public: // data members
175 KLineEdit * kcfg_Ruleset;
177 private: // functions
181 void selectionChanged( QString rulesetName );
183 private: // data members
184 QListWidget * m_listWidget;
186 QLabel * m_authorContact;
187 QLabel * m_description;
188 QMap< QString, Ruleset * > m_rulesetMap;
191 -------------------------------------------------------------------------------
194 *******************************************************************************
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 *******************************************************************************
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
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.
227 public: // data members
230 protected: // functions
232 protected: // data members
234 private: // functions
236 private: // data members
239 *******************************************************************************
241 *******************************************************************************
243 Include guards are of the form NAMESPACE_CLASSNAME_H.
245 Group includes in the following order with blank lines in between:
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.
260 -------------------------------------------------------------------------------
261 #ifndef KILLBOTS_RULESETSELECTOR_H
262 #define KILLBOTS_RULESETSELECTOR_H
266 #include <QtCore/QMap>
269 #include <QtGui/QWidget>
274 -------------------------------------------------------------------------------
275 #include "rulesetselector.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 *******************************************************************************
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
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.