Policies/Kdelibs Coding Style/fi: Difference between revisions

    From KDE TechBase
    (Created page with "/ / Väärin")
    (Updating to match new version of source page)
     
    (42 intermediate revisions by 3 users not shown)
    Line 1: Line 1:
    This document describes the recommended coding style for kdelibs. Nobody is forced to use this style, but to have consistent formatting of the source code files it is recommended to make use of it.
    <languages />
    Tämä dokumentti kuvaa kdelibs-koodin suositellun koodaustyylin. Kukaan ei pakota käyttämään tätä tyyliä, mutta on suositeltavaa käyttää muodoltaan yhtenäisiä lähdekooditiedostoja.


    '''In short: Kdelibs coding style follows the Qt 4 coding style.'''
    '''In short: Kdelibs coding style follows the [http://wiki.qt.io/Qt_Coding_Style Qt coding style], with one main difference: using curly braces even when the body of a conditional statement contains only one line.'''


    == Indentation ==
    == Sisennys ==
    * No tabs
    * Ei sarkainmerkkejä
    * 4 Spaces instead of one tab
    * 4 välilyöntiä yhden sarkainmerkin sijasta


    == Variable declaration ==
    == Muuttujaesittely ==
    * Each variable declaration on a new line
    * Jokainen muuttujaesittely uudella rivillä
    * Each new word in a variable name starts with a capital letter (so-called camelCase)
    * Jokainen uusi sana muuttujanimessä alkaa isolla kirjaimelle (niin kutsuttu kamelityyli)
    * Avoid abbreviations
    * Vältä lyhennyksiä
    * Take useful names. No short names, except:
    * Käytä hyödyllisiä nimiä. Ei lyhyitä nimiä, paitsi:
    ** Single character variable names can denote counters and temporary variables whose purpose is obvious
    ** Yksimerkkiset muutujanimet voivat ilmaista laskureita ja tilapäisiä muuttujia, joiden tarkoitus on ilmeinen
    ** Variables and functions start with a lowercase letter
    ** Muuttujat ja funktiot (metodit) alkavat pienellä kirjaimella


    Esimerkki:
    Esimerkki:
    Line 21: Line 22:
    QString prtxt, errstr;
    QString prtxt, errstr;


    // correct
    / / Oikein
    KProgressBar *downloadProgressBar;
    KProgressBar *downloadProgressBar;
    QString progressText;
    QString progressText;
    Line 27: Line 28:
    </syntaxhighlight>
    </syntaxhighlight>


    == 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:
    == Tyhjemerkki ==
    * Käytä tyhjiä rivejä ryhmälauseissa
    * Käytä vain yhtä tyhjää riviä
    * Käytä yhtä välilyöntiä jokaisen avainsanan jälkeen
    * Käytä yhtä välilyöntiä ennen osoitin- ja viitemerkkejä '*' tai '&', mutta ei niiden jälkeen
    * Ei välilyöntiä tyyppimuunnoksen jälkeen
     
    Esimerkki:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    // wrong
    / / Väärin
    QString* myString;
    QString* myString;
    if(true){
    if(true){
    }
    }


    // correct
    / / Oikein
    QString *myString;
    QString *myString;
    if (true) {
    if (true) {
    Line 47: Line 49:
    </syntaxhighlight>
    </syntaxhighlight>


    == Braces ==
    == Aaltosulkeet ==
    As a base rule, the left curly brace goes on the same line as the start of the statement.
    Perussääntönä vasen aaltosulje tulee samalle riville kuin lauseen alku.


    Example:
    Esimerkki:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    // wrong
    / / Väärin
    if (true)
    if (true)
    {
    {
    }
    }


    // correct
    / / Oikein
    if (true) {
    if (true) {
    }
    }
    </syntaxhighlight>
    </syntaxhighlight>


    Exception: Function implementations, class, struct and namespace declarations always have the opening brace on the start of a line.
    Poikkeus: Funktiototeutuksissa, luokka-, struct-rakenne- ja nimiavaruusesittelyissä on aina avaava aaltosulje aloitusrivillä.


    Example:
    Esimerkki:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    void debug(int i)
    void debug(int i)
    Line 76: Line 78:
    </syntaxhighlight>
    </syntaxhighlight>


    Use curly braces even when the body of a conditional statement contains only one line.
    Käytä aaltosulkeita jopa silloin kun ehdollisen lauseen runko sisältää vain yhden rivin.


    Example:
    Esimerkki:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    // wrong
    / / Väärin
    if (true)
    if (true)
         return true;
         return true;
    Line 87: Line 89:
         qDebug("%i", i);
         qDebug("%i", i);


    // correct
    / / Oikein
    if (true) {
    if (true) {
         return true;
         return true;
    Line 97: Line 99:
    </syntaxhighlight>
    </syntaxhighlight>


    == Switch statements ==
    == Switch-lausekkeet ==
    Case labels are on the same column as the switch
    Case-nimiöt ovat samassa sarakkeessa kuin switch-lause


    Example:
    Esimerkki:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    switch (myEnum) {
    switch (myEnum) {
    Line 115: Line 117:
    </syntaxhighlight>
    </syntaxhighlight>


    == Line breaks ==
    Yritä pitää rivit lyhyempinä kuin 100 merkkiä lisäämällä rivinvaihtoja tarvittaessa.
    Try to keep lines shorter than 100 characters, inserting line breaks as necessary.


    == Qt Includes ==
    == Qt includes-lauseet ==
    * If you add #includes for Qt classes, use both the module and class nameThis allows library code to be used by applications without excessive compiler include paths.
    * Jos lisäät #includes-rivin Qt-luokkiin, käytä sekä moduulinimeä että luokkanimeäTämä sallii sovellusten käyttää kirjastokoodia ilman liiallisia kääntäjän include-polkuja.


    Example:
    Esimerkki:
    <syntaxhighlight lang="cpp-qt">
    <syntaxhighlight lang="cpp-qt">
    // wrong
    / / Väärin
    #include <QString>
    #include <QString>


    // correct
    / / Oikein
    #include <QtCore/QString>
    #include <QtCore/QString>
    </syntaxhighlight>
    </syntaxhighlight>


    == Artistic Style (astyle) automatic code formatting ==
    == Artistic-tyylinen (astyle) automaattinen koodimuotoilu ==
    You can use [http://astyle.sourceforge.net/ astyle] (>=1.23) to format code or to test if you have followed this document. Run the following command:
    Voit käyttää [http://astyle.sourceforge.net/ astyle] (>=1.23) koodin muotoiluun tai sen testaamiseen, että olet noudattanut tätä dokumenttia. Suorita seuraava komento:
    <syntaxhighlight lang="text">
    <syntaxhighlight lang="text">
    astyle --indent=spaces=4 --brackets=linux \
    astyle --indent=spaces=4 --brackets=linux \
           --indent-labels --pad-oper --unpad-paren \
           --indent-labels --pad=oper --unpad=paren \
           --one-line=keep-statements --convert-tabs \
           --one-line=keep-statements --convert-tabs \
           --indent-preprocessor \
           --indent-preprocessor \
           `find -type f -name '*.cpp'` `find -type f -name '*.cc'` `find -type f -name '*.h'`
           `find -type f -name '*.cpp'-or -name '*.cc' -or -name '*.h'`
    </syntaxhighlight>
    </syntaxhighlight>


    With astyle (>=2.01) you need to run the following command:
    Käyttäessäsi astyle (>=2.01) -muotoilua sinun on suoritettava seuraava komento:
    <syntaxhighlight lang="text">
    <syntaxhighlight lang="text">
    astyle --indent=spaces=4 --brackets=linux \
    astyle --indent=spaces=4 --brackets=linux \
           --indent-labels --pad-oper --unpad-paren \
           --indent-labels --pad-oper --unpad-paren --pad-header \
           --keep-one-line-statements --convert-tabs \
           --keep-one-line-statements --convert-tabs \
           --indent-preprocessor \
           --indent-preprocessor \
           `find -type f -name '*.cpp'` `find -type f -name '*.cc'` `find -type f -name '*.h'`
           `find -type f -name '*.cpp' -or -name '*.cc' -or -name '*.h'`
    </syntaxhighlight>
    </syntaxhighlight>


    A related shell script could be found for unix in [http://websvn.kde.org/*checkout*/trunk/KDE/kdesdk/scripts/astyle-kdelibs kdesdk/scripts/astyle-kdelibs] and for windows in [http://websvn.kde.org/*checkout*/trunk/KDE/kdesdk/scripts/astyle-kdelibs.bat kdesdk/scripts/astyle-kdelibs.bat].
    Note: With more recent astyle --brackets has become --style, so change --brackets=linux to --style=linux.
     
    You can find a shell script to run this command in:
     
    * [https://projects.kde.org/projects/kde/kdesdk/kde-dev-scripts/repository/revisions/master/raw/astyle-kdelibs kde-dev-scripts/astyle-kdelibs] (POSIX)
    * [https://projects.kde.org/projects/kde/kdesdk/kde-dev-scripts/repository/revisions/master/raw/astyle-kdelibs.bat kde-dev-scripts/astyle-kdelibs.bat] (Windows)


    == Emacs and Vim scripts ==
    == Emacs and Vim scripts ==
    The "scripts" directory in the kdesdk module contains, among other useful things, some useful additions to the Emacs and Vim text editors that make it easier to edit KDE code with them.
    The [https://projects.kde.org/projects/kde/kdesdk/kde-dev-scripts/repository/revisions/master/show kde-dev-scripts] directory in the kdesdk module contains, among other useful things, some useful additions to the Emacs and Vim text editors that make it easier to edit KDE code with them.
       
       
    === Emacs ===
    === Emacs ===
    The [http://websvn.kde.org/trunk/KDE/kdesdk/scripts/kde-emacs kde-emacs] directory contains a set of key bindings, macros and general useful code. It is compatible with both GNU Emacs and XEmacs.
    The [https://projects.kde.org/projects/kde/kdesdk/kde-dev-scripts/repository/revisions/master/show/kde-emacs kde-emacs] directory contains a set of key bindings, macros and general useful code. It is compatible with both GNU Emacs and XEmacs.


    To start using kde-emacs, add the following to your .emacs:
    Kde-emacs -käytön aloittamiseksi lisää seuraava .emacs-tiedostoosi:


    <syntaxhighlight lang="text">
    <syntaxhighlight lang="text">
    Line 164: Line 170:
    </syntaxhighlight>
    </syntaxhighlight>


    Many settings can be changed by editing the "kde-emacs" group via <tt>M-x customize-group</tt>.
    Monia asetuksia voidaan muuttaa muokkaamalla "kde-emacs"-ryhmää <tt>M-x customize-group</tt> -kautta.


    For more information, including what the key bindings are and what additional settings you could add to your .emacs, please check <tt>kde-emacs.el</tt> itself.
    Tarkista lisätietoja varten, mukaanlukien mitä ovat näppäinsidokset ja mitä lisäasetuksia voisit lisätä .emacs-tiedostoosi, itse tiedosto <tt>kde-emacs.el</tt>.


    === Vim ===
    === Vim ===
    You can find a vim script in [http://websvn.kde.org/*checkout*/trunk/KDE/kdesdk/scripts/kde-devel-vim.vim kdesdk/scripts/kde-devel-vim.vim] that helps you to keep the coding style correct. In addition to defaulting to the kdelibs coding style it will automatically use the correct style for Solid and kdepim code. If you want to add rules for other projects feel free to add them in the SetCodingStyle function.
    You can find a vim script in [https://projects.kde.org/projects/kde/kdesdk/kde-dev-scripts/repository/revisions/master/raw/kde-devel-vim.vim kde-devel-vim.vim] that helps you to keep the coding style correct. In addition to defaulting to the kdelibs coding style it will automatically use the correct style for Solid and kdepim code. If you want to add rules for other projects feel free to add them in the SetCodingStyle function.


    To use the script, include it in your {{path|~/.vimrc}} like this:
    Skriptin käyttämiseksi sisällytä se {{path|~/.vimrc}}-tiedostoosi kuten tämä:
    <syntaxhighlight lang="text">
    <syntaxhighlight lang="text">
    source /path/to/kde/sources/kdesdk/scripts/kde-devel-vim.vim
    source /path/to/kde/sources/kdesdk/scripts/kde-devel-vim.vim
    Line 177: Line 183:




    Document started by Urs Wolfer. Some parts of this document have been adopted from the Qt Coding Style document posted by Zack Rusin on kde-core-devel.
    Urs Wolfer aloitti dokumentin. Joitakin tämän dokumentin osia on otettu Zack Rusinin lähettämästä Qt-koodaustyylidokumentista kohteesta kde-core-devel.


    [[Category:Policies]] [[Category:C++]]
    [[Category:Policies]] [[Category:C++]]

    Latest revision as of 10:25, 11 March 2016

    Tämä dokumentti kuvaa kdelibs-koodin suositellun koodaustyylin. Kukaan ei pakota käyttämään tätä tyyliä, mutta on suositeltavaa käyttää muodoltaan yhtenäisiä lähdekooditiedostoja.

    In short: Kdelibs coding style follows the Qt coding style, with one main difference: using curly braces even when the body of a conditional statement contains only one line.

    Sisennys

    • Ei sarkainmerkkejä
    • 4 välilyöntiä yhden sarkainmerkin sijasta

    Muuttujaesittely

    • Jokainen muuttujaesittely uudella rivillä
    • Jokainen uusi sana muuttujanimessä alkaa isolla kirjaimelle (niin kutsuttu kamelityyli)
    • Vältä lyhennyksiä
    • Käytä hyödyllisiä nimiä. Ei lyhyitä nimiä, paitsi:
      • Yksimerkkiset muutujanimet voivat ilmaista laskureita ja tilapäisiä muuttujia, joiden tarkoitus on ilmeinen
      • Muuttujat ja funktiot (metodit) alkavat pienellä kirjaimella

    Esimerkki:

    / / Väärin
    KProgressBar *prbar;
    QString prtxt, errstr;
    
    / / Oikein
    KProgressBar *downloadProgressBar;
    QString progressText;
    QString errorString;
    


    Tyhjemerkki

    • Käytä tyhjiä rivejä ryhmälauseissa
    • Käytä vain yhtä tyhjää riviä
    • Käytä yhtä välilyöntiä jokaisen avainsanan jälkeen
    • Käytä yhtä välilyöntiä ennen osoitin- ja viitemerkkejä '*' tai '&', mutta ei niiden jälkeen
    • Ei välilyöntiä tyyppimuunnoksen jälkeen

    Esimerkki:

    / / Väärin
    QString* myString;
    if(true){
    }
    
    / / Oikein
    QString *myString;
    if (true) {
    }
    

    Aaltosulkeet

    Perussääntönä vasen aaltosulje tulee samalle riville kuin lauseen alku.

    Esimerkki:

    / / Väärin
    if (true)
    {
    }
    
    / / Oikein
    if (true) {
    }
    

    Poikkeus: Funktiototeutuksissa, luokka-, struct-rakenne- ja nimiavaruusesittelyissä on aina avaava aaltosulje aloitusrivillä.

    Esimerkki:

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

    Käytä aaltosulkeita jopa silloin kun ehdollisen lauseen runko sisältää vain yhden rivin.

    Esimerkki:

    / / Väärin
    if (true)
        return true;
    
    for (int i = 0; i < 10; ++i)
        qDebug("%i", i);
    
    / / Oikein
    if (true) {
        return true;
    }
    
    for (int i = 0; i < 10; ++i) {
        qDebug("%i", i);
    }
    

    Switch-lausekkeet

    Case-nimiöt ovat samassa sarakkeessa kuin switch-lause

    Esimerkki:

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

    Yritä pitää rivit lyhyempinä kuin 100 merkkiä lisäämällä rivinvaihtoja tarvittaessa.

    Qt includes-lauseet

    • Jos lisäät #includes-rivin Qt-luokkiin, käytä sekä moduulinimeä että luokkanimeä. Tämä sallii sovellusten käyttää kirjastokoodia ilman liiallisia kääntäjän include-polkuja.

    Esimerkki:

    / / Väärin
    #include <QString>
    
    / / Oikein
    #include <QtCore/QString>
    

    Artistic-tyylinen (astyle) automaattinen koodimuotoilu

    Voit käyttää astyle (>=1.23) koodin muotoiluun tai sen testaamiseen, että olet noudattanut tätä dokumenttia. Suorita seuraava komento:

    astyle --indent=spaces=4 --brackets=linux \
           --indent-labels --pad=oper --unpad=paren \
           --one-line=keep-statements --convert-tabs \
           --indent-preprocessor \
           `find -type f -name '*.cpp'-or -name '*.cc' -or -name '*.h'`
    

    Käyttäessäsi astyle (>=2.01) -muotoilua sinun on suoritettava seuraava komento:

    astyle --indent=spaces=4 --brackets=linux \
           --indent-labels --pad-oper --unpad-paren --pad-header \
           --keep-one-line-statements --convert-tabs \
           --indent-preprocessor \
           `find -type f -name '*.cpp' -or -name '*.cc' -or -name '*.h'`
    

    Note: With more recent astyle --brackets has become --style, so change --brackets=linux to --style=linux.

    You can find a shell script to run this command in:

    Emacs and Vim scripts

    The kde-dev-scripts directory in the kdesdk module contains, among other useful things, some useful additions to the Emacs and Vim text editors that make it easier to edit KDE code with them.

    Emacs

    The kde-emacs directory contains a set of key bindings, macros and general useful code. It is compatible with both GNU Emacs and XEmacs.

    Kde-emacs -käytön aloittamiseksi lisää seuraava .emacs-tiedostoosi:

    (add-to-list 'load-path "/path/to/kde-emacs")
    (require 'kde-emacs)
    

    Monia asetuksia voidaan muuttaa muokkaamalla "kde-emacs"-ryhmää M-x customize-group -kautta.

    Tarkista lisätietoja varten, mukaanlukien mitä ovat näppäinsidokset ja mitä lisäasetuksia voisit lisätä .emacs-tiedostoosi, itse tiedosto kde-emacs.el.

    Vim

    You can find a vim script in kde-devel-vim.vim that helps you to keep the coding style correct. In addition to defaulting to the kdelibs coding style it will automatically use the correct style for Solid and kdepim code. If you want to add rules for other projects feel free to add them in the SetCodingStyle function.

    Skriptin käyttämiseksi sisällytä se ~/.vimrc-tiedostoosi kuten tämä:

    source /path/to/kde/sources/kdesdk/scripts/kde-devel-vim.vim
    


    Urs Wolfer aloitti dokumentin. Joitakin tämän dokumentin osia on otettu Zack Rusinin lähettämästä Qt-koodaustyylidokumentista kohteesta kde-core-devel.