Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
dev:coding_style [2013/07/21 11:44]
tedfelix [Comments] Add info about doxygen issue
dev:coding_style [2013/07/24 23:10]
tedfelix Add assertions section. Formatting.
Line 1: Line 1:
- 
 ====== Rosegarden Coding Style ====== ====== Rosegarden Coding Style ======
  
Line 55: Line 54:
  
 <code c++> <code c++>
-   if (something) somethingElse(); +if (something) somethingElse(); 
-   else someOtherThing(); +else someOtherThing(); 
-   if (something) { + 
-       somethingElse(); +if (something) { 
-   } else { +    somethingElse(); 
-       someOtherThing(); +} else { 
-   }+    someOtherThing(); 
 +}
 </code> </code>
  
Line 67: Line 67:
  
 <code c++> <code c++>
-   if (something) +if (something) 
-       somethingElse();+    somethingElse();
 </code> </code>
  
Line 75: Line 75:
 Whitespace is much as in the above examples (outside but not inside ''()'', after but not before '';'', etc.): Whitespace is much as in the above examples (outside but not inside ''()'', after but not before '';'', etc.):
 <code c++> <code c++>
-    connect(detailsButton, SIGNAL(clicked(bool)), this, SLOT(slotDetails()));+connect(detailsButton, SIGNAL(clicked(bool)), this, SLOT(slotDetails()));
 </code> </code>
  
Line 81: Line 81:
  
 <code c++> <code c++>
-    connect( detailsButton, SIGNAL( clicked(bool) ), this, SLOT( slotDetails() ) );+connect( detailsButton, SIGNAL( clicked(bool) ), this, SLOT( slotDetails() ) );
 </code> </code>
  
Line 87: Line 87:
  
 <code c++> <code c++>
-    if( something) +if( something) 
-    if(something)+if(something)
 </code> </code>
  
Line 100: Line 100:
  
   * If you have more arguments than will fit on a reasonable length line (80 characters is a good figure, but this is not a hard rule), align the extra arguments below and just after the opening ( rather than at a new level of indentation:   * If you have more arguments than will fit on a reasonable length line (80 characters is a good figure, but this is not a hard rule), align the extra arguments below and just after the opening ( rather than at a new level of indentation:
 +
 <code c++> <code c++>
-    connect(m_pluginList, SIGNAL(activated(int)), +connect(m_pluginList, SIGNAL(activated(int)), 
-            this, SLOT(slotPluginSelected(int)));+        this, SLOT(slotPluginSelected(int)));
 </code> </code>
  
Line 108: Line 109:
  
 <code c++> <code c++>
-    connect(m_pluginList, SIGNAL(activated(int)), +connect(m_pluginList, SIGNAL(activated(int)), 
-        this, SLOT(slotPluginSelected(int)));+    this, SLOT(slotPluginSelected(int)));
 </code> </code>
  
Line 115: Line 116:
  
 <code c++> <code c++>
-        CommandHistory::getInstance()->addCommand(new SegmentSyncCommand( +CommandHistory::getInstance()->addCommand(new SegmentSyncCommand( 
-                comp.getSegments(), selectedTrack, +        comp.getSegments(), selectedTrack, 
-                dialog.getTranspose(), +        dialog.getTranspose(), 
-                dialog.getLowRange(), +        dialog.getLowRange(), 
-                dialog.getHighRange(), +        dialog.getHighRange(), 
-                clefIndexToClef(dialog.getClef()))); +        clefIndexToClef(dialog.getClef())));
 </code> </code>
                                                          
Line 142: Line 142:
  
   * When commenting out a large block of text, it is preferable to use C++ style <code>//</code> comments instead of C-style <code>/* */</code> comments (except as applies to writing comments specific to [[dev:doxygen|doxygen]].)   * When commenting out a large block of text, it is preferable to use C++ style <code>//</code> comments instead of C-style <code>/* */</code> comments (except as applies to writing comments specific to [[dev:doxygen|doxygen]].)
 +
 <code c++> <code c++>
 //&&& This code was deleted temporarily //&&& This code was deleted temporarily
Line 159: Line 160:
     } */     } */
 </code> </code>
 +
 (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.) (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.)
  
Line 165: Line 167:
 Includes should try to follow the following pattern: Includes should try to follow the following pattern:
  
-<code>+<code c++>
 #include "MyHeader.h" #include "MyHeader.h"
  
Line 185: Line 187:
 Switch statements are all over the place, and we need to pick one style and use it, so we'll call this a good switch statement: Switch statements are all over the place, and we need to pick one style and use it, so we'll call this a good switch statement:
 <code c++> <code c++>
-    switch (hfix) {+switch (hfix) {
  
-    case NoteStyle::Normal: +case NoteStyle::Normal: 
-    case NoteStyle::Reversed: +case NoteStyle::Reversed: 
-        if (params.m_stemGoesUp ^ (hfix == NoteStyle::Reversed)) { +    if (params.m_stemGoesUp ^ (hfix == NoteStyle::Reversed)) { 
-            s0.setX(m_noteBodyWidth - stemThickness); +        s0.setX(m_noteBodyWidth - stemThickness); 
-        } else { +    } else { 
-            s0.setX(0)+        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;+
     }     }
 +    break;
  
 +case NoteStyle::Central:
 +    if (params.m_stemGoesUp ^ (hfix == NoteStyle::Reversed)) {
 +        s0.setX(m_noteBodyWidth / 2 + 1);
 +    } else {
 +        s0.setX(m_noteBodyWidth / 2);
 +    }
 +    break;
 +}
 </code> </code>
  
Line 210: Line 211:
  
 <code c++> <code c++>
-    switch(layoutMode) { +switch(layoutMode) { 
-        case 0 : +    case 0 : 
-            findAction("linear_mode")->setChecked(true); +        findAction("linear_mode")->setChecked(true); 
-            findAction("continuous_page_mode")->setChecked(false); +        findAction("continuous_page_mode")->setChecked(false); 
-            findAction("multi_page_mode")->setChecked(false); +        findAction("multi_page_mode")->setChecked(false); 
-            slotLinearMode(); +        slotLinearMode(); 
-        break; +    break; 
-        case 1 : +    case 1 : 
-            findAction("linear_mode")->setChecked(false); +        findAction("linear_mode")->setChecked(false); 
-            findAction("continuous_page_mode")->setChecked(true); +        findAction("continuous_page_mode")->setChecked(true); 
-            findAction("multi_page_mode")->setChecked(false); +        findAction("multi_page_mode")->setChecked(false); 
-            slotContinuousPageMode(); +        slotContinuousPageMode(); 
-        break; +    break; 
-        case 2 : +    case 2 : 
-            findAction("linear_mode")->setChecked(false); +        findAction("linear_mode")->setChecked(false); 
-            findAction("continuous_page_mode")->setChecked(false); +        findAction("continuous_page_mode")->setChecked(false); 
-            findAction("multi_page_mode")->setChecked(true); +        findAction("multi_page_mode")->setChecked(true); 
-            slotMultiPageMode(); +        slotMultiPageMode(); 
-        break; +    break; 
-    }+}
 </code> </code>
  
-===== Varables =====+===== Variables =====
  
   * You should feel encouraged to use variables to make the code easier to understand without a look at the header or the API docs.  Good:   * You should feel encouraged to use variables to make the code easier to understand without a look at the header or the API docs.  Good:
  
 <code c++> <code c++>
-    int spacing = 3; +int spacing = 3; 
-    bool useHoops = false;+bool useHoops = false;
          
-    doSomething(spacing, useHoops);+doSomething(spacing, useHoops);
 </code> </code>
  
Line 246: Line 247:
  
 <code c++> <code c++>
-    doSomething(3, false);+doSomething(3, false);
 </code> </code>
  
Line 254: Line 255:
  
 <code c++> <code c++>
-    int i; +int i; 
-    int mi = -2; +int mi = -2; 
-    int md = getLineSpacing() * 2;+int md = getLineSpacing() * 2;
  
-    int testi = -2; +int testi = -2; 
-    int testMd = 1000;+int testMd = 1000;
  
-    for (i = -1; i <= 1; ++i) { +for (i = -1; i <= 1; ++i) { 
-        int d = y - getSceneYForHeight(ph + i, x, y); +    int d = y - getSceneYForHeight(ph + i, x, y); 
-        if (d < 0) { +    if (d < 0) { 
-            d = -d; +        d = -d;
-        } +
-        if (d < md) { +
-            md = d; +
-            mi = i; +
-        } +
-        if (d < testMd) { +
-            testMd = d; +
-            testi = i; +
-        }+
     }     }
 +    if (d < md) {
 +        md = d;
 +        mi = i;
 +    }
 +    if (d < testMd) {
 +        testMd = d;
 +        testi = i;
 +    }
 +}
 +</code>
 +
 +===== Assertions =====
 +Use Q_ASSERT_X() from <QtGlobal> for assertions.
 +
 +<code c++>
 +Q_ASSERT_X(id < m_refreshStatuses.size(),
 +           "RefreshStatusArray::getRefreshStatus()",  // where
 +           "ID out of bounds");                       // what
 +</code>
 +
 +Make sure errors are properly handled in a non-debug build.  Do not depend on assertions.
 +
 +<code c++>
 +Q_ASSERT_X(p, "foo()", "null pointer");
 +if (!p) {
 +    return;
 +}
 </code> </code>
  
Line 285: Line 304:
 For example: For example:
  
-<code> +<code xml
-    <Action name="general_move_events_up_staff"  +<Action name="general_move_events_up_staff"  
-     text="&amp;Move to Staff Above..." />+        text="&amp;Move to Staff Above..." />
 </code> </code>
  
 
 
dev/coding_style.txt · Last modified: 2022/05/06 16:07 (external edit)
Recent changes RSS feed Creative Commons License Valid XHTML 1.0 Valid CSS Driven by DokuWiki