Changes between Initial Version and Version 1 of Guides/Contributing/CodingStyle


Ignore:
Timestamp:
Aug 7, 2012, 11:06:57 AM (12 years ago)
Author:
Micke Prag
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Guides/Contributing/CodingStyle

    v1 v1  
     1= Telldus Coding Style =
     2This is a document describing the preferred coding style for Telldus projects. Our coding style is based on [http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml Google C++ Style Guide] with some exceptions.
     3
     4== Indentation ==
     5Tabs are tabs! Don't use space to indent a line. If we use tabs for indentation everyone could have their editor show the preferred indentation depth.
     6There is one exception to this and it is if you break a line into multiple lines. Keep the tabs as indentation until you are at the same level as the starting line and use space for the rest of the indentation.
     7If we don't do this, different indentation-depths will break the nice-looking indentation:
     8{{{
     9#!c
     10 » int retval = function(
     11 » ······················1,
     12 » ······················"lemon"
     13 » ·····················);
     14}}}
     15
     16== Braces ==
     17Braces should always be included when writing code using if, for, while etc. blocks. There are no exceptions to this rule, even if the braces could be omitted. Leaving out braces makes code harder to maintain in the future and can also cause bugs that are very difficult to track down.
     18{{{
     19#!c++
     20/* These are all incorrect */
     21
     22if ( condition ) foo();
     23
     24if ( condition )
     25    foo();
     26
     27while ( condition )
     28    foo();
     29
     30for ( var i = 0; i < 10; i++ )
     31    foo(i);
     32
     33/* These are all correct */
     34
     35if ( condition ) {
     36    foo();
     37}
     38
     39while ( condition ) {
     40    foo();
     41}
     42
     43for ( i = 0; i < 10; i++ ) {
     44    foo(i);
     45}
     46}}}
     47=== Control Structures ===
     48Braces should always be placed on the first line of the block they are encoding.
     49{{{
     50#!c++
     51if ( condition ) {
     52    while ( condition ) {
     53        foo();
     54    }
     55}
     56}}}
     57
     58=== Functions ===
     59Function calls can be braced differently than conditional expressions.
     60{{{
     61#!c++
     62/* These are both correct */
     63
     64function myFunction(var1, var2) {
     65    return false;
     66}
     67
     68function myFunction(var1, var2)
     69{
     70    return false;
     71}
     72}}}
     73== Single vs. Double quotes ==
     74It is common among script languages to see different uses of single/double quotes for strings. In most interpreted languages single quotes are non-interpolated and double quotes are interpolated. Consider the following example:
     75{{{
     76#!php
     77<?php
     78$var = 'Hello world!';
     79
     80echo "$var\n";
     81echo '$var\n';
     82}}}
     83The output will be:
     84{{{
     85Hello world!
     86$var\n
     87}}}
     88This means that for strings that don't require interpolation, it is much faster to use single quotes. Double quotes should only be used when interpolation is required and not when defining regular strings.