Changes between Version 1 and Version 2 of Coding Style Guidelines


Ignore:
Timestamp:
Nov 7, 2006 4:25:29 PM (17 years ago)
Author:
sam@webkit.org
Comment:

More Code Style Guidlines porting

Legend:

Unmodified
Added
Removed
Modified
  • Coding Style Guidelines

    v1 v2  
    99 * Function definitions — open and close braces should be on lines by themselves. Do not put the open brace on the same line as the function signature.
    1010
    11  Right:
    12  {{{
    13  #!cpp
    14  void foo()
    15  {
    16      // do stuff
    17  }
    18  }}}
    19 
    20  Wrong:
    21  {{{
    22  #!cpp
    23  void foo() {
    24      // do stuff
    25  }
     11 '''Right:'''
     12 {{{
     13 #!cpp
     14void foo()
     15{
     16    // do stuff
     17}
     18 }}}
     19
     20 '''Wrong:'''
     21 {{{
     22 #!cpp
     23void foo() {
     24    // do stuff
     25}
    2626 }}}
    2727
    2828 * Other braces, including for, while, do, switch statements and class definitions — the open brace should go on the same line as the as the control structure.
    2929
    30  Right:
    31  {{{
    32  #!cpp
    33  for (int i = 0; i < 10; i++) {
    34      // do stuff
    35  }
    36  }}}
    37 
    38  Wrong:
    39  {{{
    40  #!cpp
    41  for (int i = 0; i < 10; i++)
    42  {
    43      // do stuff
    44  }
     30 '''Right:'''
     31 {{{
     32 #!cpp
     33for (int i = 0; i < 10; i++) {
     34    // do stuff
     35}
     36 }}}
     37
     38 '''Wrong:'''
     39 {{{
     40 #!cpp
     41for (int i = 0; i < 10; i++)
     42{
     43    // do stuff
     44}
    4545 }}}
    4646
     
    4848 * If/else statements — as above, but if there is an else clause, the close brace should go on the same line as the else. Also, one-line if or else clauses should not get braces.
    4949
    50  Right:
    51  {{{
    52  #!cpp
    53  if (timeToGetCoffee) {
    54      buyCoffee(&coffee);
    55      chugIt(coffee);
    56  } else if (timeToGoHome)
    57      // comment on else case
    58      outtaHere = true;
    59  }}}
    60 
    61  Wrong:
    62  {{{
    63  #!cpp
    64  if (timeToGetCoffee)
    65  {
    66      buyCoffee(&coffee);
    67      chugIt(coffee);
    68  // comment on else case
    69  } else if (timeToGoHome)
    70  {
    71      outtaHere = true;
    72  }
    73    
    74  if (timeToGetCoffee) {
    75  }
    76  else
    77    
    78  // comment on else case
    79    
    80  if (timeToGoHome)
    81      outtaHere = true;
    82  }}}
     50 '''Right:'''
     51 {{{
     52 #!cpp
     53if (timeToGetCoffee) {
     54    buyCoffee(&coffee);
     55    chugIt(coffee);
     56} else if (timeToGoHome)
     57    // comment on else case
     58    outtaHere = true;
     59 }}}
     60
     61 '''Wrong:'''
     62 {{{
     63 #!cpp
     64if (timeToGetCoffee)
     65{
     66    buyCoffee(&coffee);
     67    chugIt(coffee);
     68// comment on else case
     69} else if (timeToGoHome)
     70{
     71    outtaHere = true;
     72}
     73 
     74if (timeToGetCoffee) {
     75}
     76else
     77 
     78// comment on else case
     79 
     80if (timeToGoHome)
     81    outtaHere = true;
     82 }}}
     83
     84
     85== Parentheses ==
     86
     87 * Function declarations and calls — do not use any spaces between the name and the open paren, inside the parentheses, or before commas that separate arguments. Do use a single space after commas that separate arguments.
     88
     89 '''Right:'''
     90 {{{
     91 #!cpp
     92int myFunction(int arg1, float arg2);
     93
     94void noArgFunction(); // for C++ or Objective-C++
     95
     96void noArgFunction(void); // for C or Objective-C
     97 }}}
     98
     99 '''Wrong:'''
     100 {{{
     101 #!cpp
     102int myFunction (int arg1, float arg2);
     103
     104int myFunction( int arg1 , float arg2 );
     105
     106void noArgFunction ();
     107 }}}
     108
     109 * Control structures, such as if, while, do and switch — use a single space before the open paren, but no spaces inside the parentheses.
     110
     111
     112== Null, false and 0 ==
     113
     114 * In C++, the null pointer value should be written as 0. In C it should be written as NULL. In Objective-C, it should be written as nil if it is being used as a null pointer of type id or another ObjC object type, otherwise NULL.
     115
     116 * True and false values of type bool (common in C and C++), or just generic true/false values, should be written as true and false. Values of the Objective-C BOOL type should be written as YES and NO.
     117
     118 * Tests for null pointers, false values and 0 values should all be done directly, not through an inqueality or equality comparison.
     119
     120 '''Right:'''
     121 {{{
     122 #!cpp
     123// test for true
     124if (foo->isSomething()) {
     125    // code
     126}
     127   
     128// test for false
     129if (!foo->isSomething()) {
     130    // code
     131}
     132   
     133// test for non-null
     134if (ptr) {
     135   // code
     136}
     137   
     138// test for null
     139if (!ptr) {
     140   // code
     141}
     142   
     143// test for nonzero
     144if (count) {
     145    // code
     146}
     147   
     148// test for zero
     149if (!count) {
     150    // code
     151}
     152 }}}
     153
     154 '''Wrong:'''
     155 {{{
     156 #!cpp
     157if (foo->isSomething() == true) {
     158    // code
     159}
     160   
     161if (foo->isSomething() != false) {
     162    // code
     163}
     164   
     165if (p == NULL) {
     166    // code
     167}
     168   
     169if (nil != p) {
     170    // code
     171}
     172   
     173if (count == 0) {
     174    // code
     175}
     176 }}}
     177
     178
     179== Names ==
     180
     181 * General Rule: With very few exceptions, prefer embedded capitals instead of underscores for class, function and variable names.
     182
     183 * C++ and Objective-C classes, interfaces and protocols, and other type names — these names should start with a capital letter and use InterCaps.
     184
     185 '''Right:'''
     186 {{{
     187 #!cpp
     188class MyImportantClass;
     189 }}}
     190
     191 '''Wrong:'''
     192 {{{
     193 #!cpp
     194class My_important_class;
     195
     196class myImportantClass;
     197 }}}
     198
     199 * Local variables should use interCaps, but the first word should start with a lowercase letter, like this:
     200
     201 '''Right:'''
     202 {{{
     203 #!cpp
     204int myInt;
     205 }}}
     206
     207 '''Wrong:'''
     208 {{{
     209 #!cpp
     210int MyInt;
     211
     212int my_int;
     213 }}}
     214
     215 * Free function names in C++ should follow the same naming conventions as local variables. Most functions should be named to sound like verb phrases, like “openDoor” or “walkAroundTheBlock”. (getters, setters, predicates?)
     216
     217 * C++ data members should be named like local variables, but with a prefix of m_.
     218
     219 * C++ member functions should follow the same naming convention as free functions.
     220
     221 * Objective-C methods should follow the usual Cocoa naming style — they should read like a phrase or sentence and each piece of the selector should start with a lowercase letter and use intercaps.
     222
     223 * Objective-C instance variables should be named like local variables but starting with an underscore.
     224
     225 * Enum members should user InterCaps with an initial capital letter.
     226
     227 * #defined constants should use all uppercase names with words separated by underscores.
     228
     229 * Macros that expand to function calls or other non-constant computation: these should be named like functions, and should have parentheses at the end, even if they take no arguments (with the exception of some special macros like ASSERT). Note that usually it is preferrable to use an inline function in such cases instead of a macro.
     230
     231 '''Right:'''
     232 {{{
     233 #!cpp
     234#define WBStopButtonTitle() \
     235        NSLocalizedString(@"Stop", @"Stop button title")
     236 }}}
     237
     238 '''Wrong:'''
     239 {{{
     240 #!cpp
     241#define WB_STOP_BUTTON_TITLE \
     242        NSLocalizedString(@"Stop", @"Stop button title")
     243
     244#define WBStopButtontitle \
     245        NSLocalizedString(@"Stop", @"Stop button title")
     246 }}}
     247
     248 * Acronyms in names: If an identifier includes an acronym, make the acronym all-uppercase or all-lowercase, depending on whether a word in that position would be capitalized or not.
     249
     250 '''Right:'''
     251 {{{
     252 #!cpp
     253urlVariable
     254myURLAccessor:
     255 }}}
     256
     257 '''Wrong:'''
     258 {{{
     259 #!cpp
     260uRLVariable
     261myUrlAccessor:
     262 }}}