Changes between Version 25 and Version 26 of Hacking


Ignore:
Timestamp:
01/03/16 13:58:07 (8 years ago)
Author:
andrew_b
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Hacking

    v25 v26  
    22= HACKING = 
    33 
    4 == Code Style == 
     4== Coding Style == 
    55 
    66Please use the same indentation as other developers. 
    77 * Maximum line width is 100 characters. The reason is not about people with low-res screens but rather sticking to 100 columns prevents you from easily nesting more than one level of if statements or other code blocks. 
    8  * Use 4 space tabs with whitespace fillers to Indent. Never use tabs. 
    9  * No trailing whitespace  
    10  * Follow the GNU-Style guidelines.  
     8 * No tabs. Use 4 space tabs with whitespace fillers to indent. 
     9 * No trailing whitespace. 
     10 * Follow the GNU-Style guidelines. 
    1111 
    1212To format the code, use the '''indent''' utility with following options: 
     
    2323 
    2424or in short notation: 
    25  
    2625{{{ 
    2726indent -gnu -fc1 -i4 -bli0 -nut -bap -l100 
     
    3433Comments should always use the standard C syntax. C++ style comments are not currently allowed. 
    3534 
    36 The lines before a comment should be empty. If the comment directly belongs to the following code, there should be no empty line after the comment, except if the comment contains a summary of multiple following code blocks. 
     35Lines before a comment should be empty. If the comment directly belongs to the following code, there should be no empty line after the comment, except if the comment contains a summary of multiple following code blocks. 
    3736 
    3837'''This is right:''' 
     
    7473}}} 
    7574 
    76 === Conditions in Code blocks (if, for, case...) === 
    77  
    78 Always follow an 'if' keyword with a space but don't include additional 
    79 spaces following or preceding the parentheses in the conditional. 
     75=== Conditions in code blocks (if, for, case...) === 
     76 
     77Always follow an 'if' keyword with a space but don't include additional spaces following or preceding the parentheses in the conditional. 
    8078 
    8179'''This is right:''' 
     
    142140    path = strip_home_and_password (current_panel->cwd); } 
    143141 
    144     for (k = 0; k < 10; k++) 
    145         for (j = 0; j < 10; j++) 
    146             for (i = 0; str_options[i].opt_name != NULL; i++) 
    147                 g_free (*str_options[i].opt_addr); 
     142for (k = 0; k < 10; k++) 
     143     for (j = 0; j < 10; j++) 
     144        for (i = 0; str_options[i].opt_name != NULL; i++) 
     145            g_free (*str_options[i].opt_addr); 
    148146}}} 
    149147 
     
    160158        dest = input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, ""); 
    161159 
    162         if (!dest || !*dest) 
     160        if (dest == NULL || *dest == '\0') 
    163161            goto cleanup; 
    164162        ... 
     
    176174=== Readable Code === 
    177175 
    178 Use your best judgement and choose the more readable option. 
    179 Remember that many other persons will review it: 
     176Use your best judgement and choose the more readable option. Remember that many other persons will review it: 
    180177 
    181178'''This is right:''' 
    182179{{{ 
    183180bytes = read (fd, &routine.pointer, sizeof (routine)); 
    184 if (bytes == -1 || (size_t) bytes < (sizeof (routine))) 
    185 }}} 
    186  
    187 '''This is wrong:''' 
    188 {{{ 
    189 if (bytes = read (fd, &routine.pointer, sizeof (routine)) == -1 || (size_t) bytes < (sizeof (routine))) 
    190  
    191 if (read (from_parent_fd, &i, sizeof (int)) != sizeof (int)) 
    192     return NULL; 
     181if (bytes == -1 || (size_t) bytes < sizeof (routine)) 
     182    ... 
     183}}} 
     184 
     185'''This is wrong:''' 
     186{{{ 
     187if ((bytes = read (fd, &routine.pointer, sizeof (routine))) == -1 || (size_t) bytes < sizeof (routine)) 
     188    ... 
     189}}} 
     190 
     191Don not place more than one statement in one line. 
     192 
     193'''This is right:''' 
     194{{{ 
     195a = 0; 
     196b = 2; 
     197 
     198a = f(); 
     199if (a == 2) 
     200    b = 5; 
     201 
     202}}} 
     203 
     204'''This is wrong:''' 
     205{{{ 
     206a = 0; b = 2; 
     207 
     208if ((a = f()) == 2) 
     209    b = 5; 
     210 
     211if (a == 2) b = 5; 
    193212}}} 
    194213 
     
    227246}}} 
    228247 
     248Do not check boolean values for equality: 
     249 
     250'''This is right:''' 
     251{{{ 
     252gboolean b1, b2; 
     253 
     254if (b1) 
     255if (!b2) 
     256}}} 
     257 
     258'''This is wrong:''' 
     259{{{ 
     260gboolean b1, b2; 
     261 
     262if (b1 == TRUE) 
     263if (b2 == FALSE) 
     264}}} 
     265 
     266 
    229267=== Variables === 
    230268 
    231 Declare variable only in the beginning of block. Split variable declaration and code using one empty line. Try to avoid using functions to initialize variables. Split variable declaration and value assignment. 
     269Reduce variable scope as much as possible: declare local variable in that block where it is used. 
     270 
     271Don't mix variable declaration and code, declare variable only in the beginning of block. 
     272 
     273Split variable declaration and code using one empty line. Avoid using function to initialize variable. Split variable declaration and value assignment. 
    232274 
    233275'''This is right:''' 
     
    239281 
    240282'''This is wrong:''' 
    241  
    242283{{{ 
    243284vfs_path_t *vpath = vfs_path_from_str (filename); 
    244285}}} 
    245286 
    246 === Make use of helper variables === 
    247  
    248 Please try to avoid passing function calls as function parameters in new code. This makes the code much easier to read and it's also easier to use the "step" command within gdb. 
     287=== Helper variables === 
     288 
     289Try to avoid passing function calls as function parameters in new code. This makes the code much easier to read and it's also easier to use the "step" command within gdb. 
    249290 
    250291'''This is right:''' 
     
    326367 
    327368Do not mix global and local variables and functions. 
    328 Do not mix static and non-static statments. 
     369Do not mix static and non-static statements. 
    329370 
    330371Follow typical structure of *.c file: