From kde-core-devel Thu Jul 20 15:32:07 2006 From: Zack Rusin Date: Thu, 20 Jul 2006 15:32:07 +0000 To: kde-core-devel Subject: kdelibs coding style Message-Id: <190401020443.21036.zack () kde ! org> X-MARC-Message: https://marc.info/?l=kde-core-devel&m=115340952719876 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--Boundary-00=_ZV92DvXQ608aNQ1" --Boundary-00=_ZV92DvXQ608aNQ1 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline With much love from Russia comes temperature checker (prediction: it's hot! flaming hot!) in the form of a proposal for a common coding style in kdelibs. It's something we talked about during the KDE Four meeting. The reason for it is that it's a pain in a butt to read through kdelibs code. Indention differs within files which makes a lot of them unreadable (oh, and btw shoutouts to George and Lubos for having indention styles that make grown may cry and baby jesus swear). Since KDE uses this thing called Qt, which also happens to be a library, it was agreed that it makes natural sense to adopt the style used in Qt. Attached is a file which describes Qt coding style. The idea is that it would be adopted for kdelibs. To make it clear, no mass reindenting would take place.. For already existing code the indention would be changed when a person would be editting it. So if you fix a bug in already existing code, you simply indent your code with the standard indention. This way history won't be messed up and we'll end up with consistant style in all files by the time KDE4 is out. The style is obviously mandatory for all new files. No exceptions. Either everything or nothing. Well, the only exception are libraries that are not maintained in the KDE SVN (for example, if it ever happens, integrated KHTML/WebKit would be maintained outside KDE SVN and the coding style that applies to it is one chosen for this project - interestingly enough WebKit coding style is basically exactly like Qt coding style so that's not going to be an issue). Oh, and this is of course only for kdelibs, in your apps/modules you can still use the 2.3 tabs indention or whatever your sick mind desires. with not a whole lot love but a lot of perseverance your boy toy -- If it's stupid but works, it isn't stupid. --Boundary-00=_ZV92DvXQ608aNQ1 Content-Type: text/html; charset="us-ascii"; name="QtCodingStyle.html" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="QtCodingStyle.html" QtCodingStyle < Main < TWiki

Qt Coding Style

This is a overview of the coding convention we use when writing Qt code. The data has been gathered by mining the Qt sources, discussion forums, email threads and through collaboration of the developers.

Indentation

  • 4 spaces are used for indentation
  • Spaces, not tabs!

Declaring variables

  • Declare each variable on a separate line
  • Avoid short (e.g., a,rbarr,nughdeget) names whenever possible
  • Single character variable names are only okay for counters and temporaries, where the purpose of the variable is obvious
  • Wait with declaring a variable until it is needed

    // Wrong
    int a, b;
    char *c, *d;

    // Correct
    int height;
    int width;
    char *nameOfThis;
    char *nameOfThat;

  • Variables and functions start with a small letter. Each consecive word in a variable's name starts with a capital letter
  • Avoid abbreviations

    // Wrong
    short Cntr;
    char ITEM_DELIM = '\t';

    // Correct
    short counter;
    char itemDelimiter = '\t';

  • Classes always start with a big letter. Public classes start with a 'Q' (QRgb). Public functions most often start with a 'q' (qRgb).

Whitespace

  • Use blank lines to group statements together where suited
  • Always use only one blank line
  • Always use a single space after a keyword, and before a curly brace.

    // Wrong
    if(foo){
    }

    // Correct
    if (foo) {
    }

  • For pointers or references, always use a single space before '*' or '&', but never after.
  • No space after a cast.
  • Avoid C-style casts when possible.

    // Wrong
    char* blockOfMemory = (char* ) malloc(data.size());

    // Correct
    char *blockOfMemory = (char *)malloc(data.size());
    char *blockOfMemory = reinterpret_cast<char *>(malloc(data.size()));

Braces

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

    // Wrong
    if (codec)
    {
    }

    // Correct
    if (codec) {
    }

  • Exception: Function implementations and class declarations always have the left brace on the start of a line:

    static void foo(int g)
    {
        qDebug("foo: %i", g);
    }

    class Moo
    {
    };

  • Use curly braces when the body of a conditional statement contains more than one line, and also if a single line statement is somewhat complex.

    // Wrong
    if (address.isEmpty()) {
        return false;
    }

    for (int i = 0; i < 10; ++i) {
        qDebug("%i", i);
    }

    // Correct
    if (address.isEmpty())
        return false;

    for (int i = 0; i < 10; ++i)
        qDebug("%i", i);

  • Exception 1: Use braces also if the parent statement covers several lines / wraps

    // Correct
    if (address.isEmpty() || !isValid()
        || !codec) {
        return false;
    }

  • Exception 2: Use braces also in if-then-else blocks where either the if-code or the else-code covers several lines

    // Wrong
    if (address.isEmpty())
        return false;
    else {
        qDebug("%s", qPrintable(address));
        ++it;
    }

    // Correct
    if (address.isEmpty()) {
        return false;
    } else {
        qDebug("%s", qPrintable(address));
        ++it;
    }

Switch statements

  • The case labels are on the same column as the switch
  • Every case must have a break (or return) statement at the end or a comment to indicate that there's intentionally no break

    switch (myEnum) {
    case Value1:
        doSomething();
        break;
    case Value2:
        doSomethingElse();
        // fall through
    default:
        defaultHandling();
        break;
    }

Line breaks

  • Keep lines shorter than 100 characters; insert line breaks if necessary.

General exception

  • Feel free to break a rule if it makes your code look bad.
 
Copyright by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
--Boundary-00=_ZV92DvXQ608aNQ1--