Rosegarden house style is as follows, in approximately descending order of importance:
namespace Rosegarden { // 1 // 2 class SomeUsefulClass { //code }; // 1 // 2 }
int SomeClass::someMethod(int f) { int result = 0; if (f >= 0) { for (int i = 0; i < f; ++i) { result += i; } } else { result = -1; } return result; }
if (something) somethingElse(); else someOtherThing(); if (something) { somethingElse(); } else { someOtherThing(); }
but not:
if (something) somethingElse();
(), after but not before ;, etc.):connect(detailsButton, SIGNAL(clicked(bool)), this, SLOT(slotDetails()));
but not:
connect( detailsButton, SIGNAL( clicked(bool) ), this, SLOT( slotDetails() ) );
and please avoid:
if( something) if(something)
No whitespace between if (and other C++ keywords) and the ( makes Michael's eyes hurt.
MyObject *ptr; not MyObject* ptr;connect(m_pluginList, SIGNAL(activated(int)), this, SLOT(slotPluginSelected(int)));
but not:
connect(m_pluginList, SIGNAL(activated(int)), this, SLOT(slotPluginSelected(int)));
( is so far to the right that following the above rule isn't practical, then indent the remainder of the () block by 8 spaces.CommandHistory::getInstance()->addCommand(new SegmentSyncCommand( comp.getSegments(), selectedTrack, dialog.getTranspose(), dialog.getLowRange(), dialog.getHighRange(), clefIndexToClef(dialog.getClef())));
| Code | Meaning |
|---|---|
//!!! | TODO, or “this code is probably crap” |
//&&& | Code temporarily disabled |
//@@@ | This code might not actually work |
//### | Qt4 port info |
//$$$ | Strings to change after a freeze ends |
//
comments instead of C-style
/* */
comments (except as applies to writing comments specific to doxygen.)
//&&& This code was deleted temporarily // for (int m = 0; m <= n; ++m) { // int x = s.x() + (int)((m * ((double)m * ax + bx)) / n); // int y = s.y() + (int)((m * ((double)m * ay + by)) / n); // }
but not:
//&&& This code was deleted temporarily /* for (int m = 0; m <= n; ++m) { int x = s.x() + (int)((m * ((double)m * ax + bx)) / n); int y = s.y() + (int)((m * ((double)m * ay + by)) / n); } */
(The reason C++-style comments are preferred is not arbitrary. C++-style comments are much more obvious when viewing diffs on the rosegarden-bugs list, because they force the entire block of text to be displayed, instead of only the starting and ending lines.)
#include "MyHeader.h" #include "MyCousinClassHeader.h" #include "MyOtherCousin.h" #include "RosegardenHeader.h" #include "base/SomeOtherHeader.h" #include <QSomeClass> #include <QSomeOtherClass> #include <some_stl_class> #include <some_other_stl_class>
switch (hfix) { case NoteStyle::Normal: case NoteStyle::Reversed: if (params.m_stemGoesUp ^ (hfix == NoteStyle::Reversed)) { s0.setX(m_noteBodyWidth - stemThickness); } else { s0.setX(0); } break; case NoteStyle::Central: if (params.m_stemGoesUp ^ (hfix == NoteStyle::Reversed)) { s0.setX(m_noteBodyWidth / 2 + 1); } else { s0.setX(m_noteBodyWidth / 2); } break; }
and this a bad one (note particularly the switch( here, grrrrr!):
switch(layoutMode) { case 0 : findAction("linear_mode")->setChecked(true); findAction("continuous_page_mode")->setChecked(false); findAction("multi_page_mode")->setChecked(false); slotLinearMode(); break; case 1 : findAction("linear_mode")->setChecked(false); findAction("continuous_page_mode")->setChecked(true); findAction("multi_page_mode")->setChecked(false); slotContinuousPageMode(); break; case 2 : findAction("linear_mode")->setChecked(false); findAction("continuous_page_mode")->setChecked(false); findAction("multi_page_mode")->setChecked(true); slotMultiPageMode(); break; }
int spacing = 3; bool useHoops = false; doSomething(spacing, useHoops);
avoid:
doSomething(3, false);
Note that you should not feel like you have to take this to ridiculous extremes. Just bear it in mind, especially when it's something obscure enough you've just had to go look at the header or the API yourself to figure out what some static parameter was for.