QStarDict coding style

From QStarDict wiki
Jump to: navigation, search

This document describes the recommended coding style for QStarDict. Nobody is forced to use this style, but to have consistent formating of the source code files it is recommended to make use of it.

Contents

Indentation

  • No tabs
  • 4 Spaces instead of one tab

Variable declaration

  • Each variable declaration on a new line
  • Each new word in a variable name starts with a capital letter
  • 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 small letter

    Example:

    // wrong
    QProgressBar *prbar;
    QString prtxt, errstr;
    // correct
    QProgressBar *downloadProgressBar;
    QString progressText;
    QString errorString;

    Whitespace

    • Use blank lines to group statements
    • Use only one empty line
    • Use one space after each keyword
    • For pointers or references, use a single space before '*' or '&', but not after
    • No space after a cast

    Example:

    // wrong
    QString* myString;
    if(true)
    {
    }
    // correct
    QString *myString;
    if (true)
    {
    }

    Braces

    As a base rule, the left curly brace goes on the start of a line. Example:

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

    Line breaks

    Try to keep lines shorter than 120 characters, inserting line breaks as necessary.

    Head

    Add head from this template to every source file:

    /*****************************************************************************
     * filename - QStarDict, a StarDict clone written using Qt                   *
     * Copyright (C) year Author                                                 *
     *                                                                           *
     * This program is free software; you can redistribute it and/or modify      *
     * it under the terms of the GNU General Public License as published by      *
     * the Free Software Foundation; either version 2 of the License, or         *
     * (at your option) any later version.                                       *
     *                                                                           *
     * This program is distributed in the hope that it will be useful,           *
     * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
     * GNU General Public License for more details.                              *
     *                                                                           *
     * You should have received a copy of the GNU General Public License along   *
     * with this program; if not, write to the Free Software Foundation, Inc.,   *
     * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
     *****************************************************************************/

    Vim modeline

    In the end of every file add this vim modeline:

    // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
Personal tools