Policies/Frameworks Coding Style: Difference between revisions

    From KDE TechBase
    (→‎Whitespace: Extend)
    (Put 'else' one one line with closing braces of if block.)
    Line 55: Line 55:


    == Braces ==
    == Braces ==
    As a base rule, the left curly brace goes on the same line as the start of the statement.
    * As a base rule, the left curly brace goes on the same line as the start of the statement.


    Example:
    Example:
    Line 70: Line 70:




    Exception: Function implementations, class, struct and namespace declarations always have the opening brace on the start of a line.
    * Exception: Function implementations, class, struct and namespace declarations always have the opening brace on the start of a line.


    Example:
    Example:
    Line 85: Line 85:




    Use curly braces even when the body of a conditional statement contains only one line.
    * Use curly braces even when the body of a conditional statement contains only one line.
    * Put 'else' on the same line as the closing brace of the corresponding if block


    Example:
    Example:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    // wrong
    // wrong
    if (true)
    if (active)
         return true;
         return true;
    else
        return j == 0;


    for (int i = 0; i < 10; ++i)
    for (int i = 0; i < 10; ++i)
    Line 97: Line 100:


    // correct
    // correct
    if (true) {
    if (active) {
         return true;
         return true;
    } else {
        return j == 0;
    }
    }



    Revision as of 19:08, 29 November 2015

    Note
    This is a style guide for KDE Frameworks 5. If you write code that is not targeted at KF5, but only at KDE SC 4, refer to that style guide.


    Indentation

    • 4 spaces
    • No tabs

    Variable Declarations

    • Each variable declaration on a new line
    • Each new word in a variable name starts with a capital letter (so-called camelCase)
    • Avoid abbreviations
    • Take useful names. No short names, except:
      • Single character variable names can denote counters and temporary variables whose purpose is obvious
    • Variables and functions start with a lowercase letter

    Example:

    // wrong
    KCategorizedView *catview;
    QString prtxt, Errstr;
    
    // correct
    KCategorizedView *groupedDeviceList;
    QString progressText;
    QString errorString;
    

    Whitespace

    • Use blank lines to group statements
    • Use only one empty line
    • One space after each keyword
      • Exception: No space between return and ';'
    • No space after left parentheses/before right parentheses
    • For pointers or references, use a single space before '*' or '&', but not after
    • No space after a cast
    • Single spaces around binary arithmetic operators '+', '-', '*', '/', '%'
    • In function headers and calls: No space before and one space after commas
      • Exception: Normalized SIGNAL/SLOT string descriptors in calls to QObject::connect()

    Example:

    // wrong
    QString* myString;
    if( a%7 > 3 ){
        setPos( x , y );
        return ;
    }
    
    // correct
    QString *myString;
    if (a % 7 > 3) {
        setPos(x, y);
        return;
    }
    

    Braces

    • As a base rule, the left curly brace goes on the same line as the start of the statement.

    Example:

    // wrong
    if (true)
    {
    }
    
    // correct
    if (true) {
    }
    


    • Exception: Function implementations, class, struct and namespace declarations always have the opening brace on the start of a line.

    Example:

    void debug(int i)
    {
        qDebug("foo: %i", i);
    }
    
    class Debug
    {
    };
    


    • Use curly braces even when the body of a conditional statement contains only one line.
    • Put 'else' on the same line as the closing brace of the corresponding if block

    Example:

    // wrong
    if (active)
        return true;
    else
        return j == 0;
    
    for (int i = 0; i < 10; ++i)
        qDebug("%i", i);
    
    // correct
    if (active) {
        return true;
    } else {
        return j == 0;
    }
    
    for (int i = 0; i < 10; ++i) {
        qDebug("%i", i);
    }
    

    Qt Includes

    • For Qt #includes omit the module name and only use the class name. That way chances are good that future migrations of Qt classes between different modules do not need any adjustments in the code.

    Example:

    // wrong
    #include <QtCore/QString>
    
    // correct
    #include <QString>
    


    Note
    This policy applies to KF5 and is the exact opposite of the preferred style for kdelibs in KDE SC 4.