diff options
Diffstat (limited to 'Documentation/CodingStyle')
| -rw-r--r-- | Documentation/CodingStyle | 98 |
1 files changed, 49 insertions, 49 deletions
diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle index f103de7e2028..c25528d76af1 100644 --- a/Documentation/CodingStyle +++ b/Documentation/CodingStyle | |||
| @@ -37,8 +37,8 @@ benefit of warning you when you're nesting your functions too deep. | |||
| 37 | Heed that warning. | 37 | Heed that warning. |
| 38 | 38 | ||
| 39 | The preferred way to ease multiple indentation levels in a switch statement is | 39 | The preferred way to ease multiple indentation levels in a switch statement is |
| 40 | to align the "switch" and its subordinate "case" labels in the same column | 40 | to align the ``switch`` and its subordinate ``case`` labels in the same column |
| 41 | instead of "double-indenting" the "case" labels. E.g.: | 41 | instead of ``double-indenting`` the ``case`` labels. E.g.: |
| 42 | 42 | ||
| 43 | .. code-block:: c | 43 | .. code-block:: c |
| 44 | 44 | ||
| @@ -141,7 +141,7 @@ special anyway (you can't nest them in C). | |||
| 141 | 141 | ||
| 142 | Note that the closing brace is empty on a line of its own, _except_ in | 142 | Note that the closing brace is empty on a line of its own, _except_ in |
| 143 | the cases where it is followed by a continuation of the same statement, | 143 | the cases where it is followed by a continuation of the same statement, |
| 144 | ie a "while" in a do-statement or an "else" in an if-statement, like | 144 | ie a ``while`` in a do-statement or an ``else`` in an if-statement, like |
| 145 | this: | 145 | this: |
| 146 | 146 | ||
| 147 | .. code-block:: c | 147 | .. code-block:: c |
| @@ -228,7 +228,7 @@ Do not add spaces around (inside) parenthesized expressions. This example is | |||
| 228 | s = sizeof( struct file ); | 228 | s = sizeof( struct file ); |
| 229 | 229 | ||
| 230 | When declaring pointer data or a function that returns a pointer type, the | 230 | When declaring pointer data or a function that returns a pointer type, the |
| 231 | preferred use of '\*' is adjacent to the data name or function name and not | 231 | preferred use of ``*`` is adjacent to the data name or function name and not |
| 232 | adjacent to the type name. Examples: | 232 | adjacent to the type name. Examples: |
| 233 | 233 | ||
| 234 | .. code-block:: c | 234 | .. code-block:: c |
| @@ -255,10 +255,10 @@ no space after the prefix increment & decrement unary operators:: | |||
| 255 | 255 | ||
| 256 | ++ -- | 256 | ++ -- |
| 257 | 257 | ||
| 258 | and no space around the '.' and "->" structure member operators. | 258 | and no space around the ``.`` and ``->`` structure member operators. |
| 259 | 259 | ||
| 260 | Do not leave trailing whitespace at the ends of lines. Some editors with | 260 | Do not leave trailing whitespace at the ends of lines. Some editors with |
| 261 | "smart" indentation will insert whitespace at the beginning of new lines as | 261 | ``smart`` indentation will insert whitespace at the beginning of new lines as |
| 262 | appropriate, so you can start typing the next line of code right away. | 262 | appropriate, so you can start typing the next line of code right away. |
| 263 | However, some such editors do not remove the whitespace if you end up not | 263 | However, some such editors do not remove the whitespace if you end up not |
| 264 | putting a line of code there, such as if you leave a blank line. As a result, | 264 | putting a line of code there, such as if you leave a blank line. As a result, |
| @@ -276,17 +276,17 @@ context lines. | |||
| 276 | C is a Spartan language, and so should your naming be. Unlike Modula-2 | 276 | C is a Spartan language, and so should your naming be. Unlike Modula-2 |
| 277 | and Pascal programmers, C programmers do not use cute names like | 277 | and Pascal programmers, C programmers do not use cute names like |
| 278 | ThisVariableIsATemporaryCounter. A C programmer would call that | 278 | ThisVariableIsATemporaryCounter. A C programmer would call that |
| 279 | variable "tmp", which is much easier to write, and not the least more | 279 | variable ``tmp``, which is much easier to write, and not the least more |
| 280 | difficult to understand. | 280 | difficult to understand. |
| 281 | 281 | ||
| 282 | HOWEVER, while mixed-case names are frowned upon, descriptive names for | 282 | HOWEVER, while mixed-case names are frowned upon, descriptive names for |
| 283 | global variables are a must. To call a global function "foo" is a | 283 | global variables are a must. To call a global function ``foo`` is a |
| 284 | shooting offense. | 284 | shooting offense. |
| 285 | 285 | ||
| 286 | GLOBAL variables (to be used only if you _really_ need them) need to | 286 | GLOBAL variables (to be used only if you _really_ need them) need to |
| 287 | have descriptive names, as do global functions. If you have a function | 287 | have descriptive names, as do global functions. If you have a function |
| 288 | that counts the number of active users, you should call that | 288 | that counts the number of active users, you should call that |
| 289 | "count_active_users()" or similar, you should _not_ call it "cntusr()". | 289 | ``count_active_users()`` or similar, you should _not_ call it ``cntusr()``. |
| 290 | 290 | ||
| 291 | Encoding the type of a function into the name (so-called Hungarian | 291 | Encoding the type of a function into the name (so-called Hungarian |
| 292 | notation) is brain damaged - the compiler knows the types anyway and can | 292 | notation) is brain damaged - the compiler knows the types anyway and can |
| @@ -294,9 +294,9 @@ check those, and it only confuses the programmer. No wonder MicroSoft | |||
| 294 | makes buggy programs. | 294 | makes buggy programs. |
| 295 | 295 | ||
| 296 | LOCAL variable names should be short, and to the point. If you have | 296 | LOCAL variable names should be short, and to the point. If you have |
| 297 | some random integer loop counter, it should probably be called "i". | 297 | some random integer loop counter, it should probably be called ``i``. |
| 298 | Calling it "loop_counter" is non-productive, if there is no chance of it | 298 | Calling it ``loop_counter`` is non-productive, if there is no chance of it |
| 299 | being mis-understood. Similarly, "tmp" can be just about any type of | 299 | being mis-understood. Similarly, ``tmp`` can be just about any type of |
| 300 | variable that is used to hold a temporary value. | 300 | variable that is used to hold a temporary value. |
| 301 | 301 | ||
| 302 | If you are afraid to mix up your local variable names, you have another | 302 | If you are afraid to mix up your local variable names, you have another |
| @@ -307,7 +307,7 @@ See chapter 6 (Functions). | |||
| 307 | 5) Typedefs | 307 | 5) Typedefs |
| 308 | ----------- | 308 | ----------- |
| 309 | 309 | ||
| 310 | Please don't use things like "vps_t". | 310 | Please don't use things like ``vps_t``. |
| 311 | It's a _mistake_ to use typedef for structures and pointers. When you see a | 311 | It's a _mistake_ to use typedef for structures and pointers. When you see a |
| 312 | 312 | ||
| 313 | .. code-block:: c | 313 | .. code-block:: c |
| @@ -322,35 +322,35 @@ In contrast, if it says | |||
| 322 | 322 | ||
| 323 | struct virtual_container *a; | 323 | struct virtual_container *a; |
| 324 | 324 | ||
| 325 | you can actually tell what "a" is. | 325 | you can actually tell what ``a`` is. |
| 326 | 326 | ||
| 327 | Lots of people think that typedefs "help readability". Not so. They are | 327 | Lots of people think that typedefs ``help readability``. Not so. They are |
| 328 | useful only for: | 328 | useful only for: |
| 329 | 329 | ||
| 330 | (a) totally opaque objects (where the typedef is actively used to _hide_ | 330 | (a) totally opaque objects (where the typedef is actively used to _hide_ |
| 331 | what the object is). | 331 | what the object is). |
| 332 | 332 | ||
| 333 | Example: "pte_t" etc. opaque objects that you can only access using | 333 | Example: ``pte_t`` etc. opaque objects that you can only access using |
| 334 | the proper accessor functions. | 334 | the proper accessor functions. |
| 335 | 335 | ||
| 336 | NOTE! Opaqueness and "accessor functions" are not good in themselves. | 336 | NOTE! Opaqueness and ``accessor functions`` are not good in themselves. |
| 337 | The reason we have them for things like pte_t etc. is that there | 337 | The reason we have them for things like pte_t etc. is that there |
| 338 | really is absolutely _zero_ portably accessible information there. | 338 | really is absolutely _zero_ portably accessible information there. |
| 339 | 339 | ||
| 340 | (b) Clear integer types, where the abstraction _helps_ avoid confusion | 340 | (b) Clear integer types, where the abstraction _helps_ avoid confusion |
| 341 | whether it is "int" or "long". | 341 | whether it is ``int`` or ``long``. |
| 342 | 342 | ||
| 343 | u8/u16/u32 are perfectly fine typedefs, although they fit into | 343 | u8/u16/u32 are perfectly fine typedefs, although they fit into |
| 344 | category (d) better than here. | 344 | category (d) better than here. |
| 345 | 345 | ||
| 346 | NOTE! Again - there needs to be a _reason_ for this. If something is | 346 | NOTE! Again - there needs to be a _reason_ for this. If something is |
| 347 | "unsigned long", then there's no reason to do | 347 | ``unsigned long``, then there's no reason to do |
| 348 | 348 | ||
| 349 | typedef unsigned long myflags_t; | 349 | typedef unsigned long myflags_t; |
| 350 | 350 | ||
| 351 | but if there is a clear reason for why it under certain circumstances | 351 | but if there is a clear reason for why it under certain circumstances |
| 352 | might be an "unsigned int" and under other configurations might be | 352 | might be an ``unsigned int`` and under other configurations might be |
| 353 | "unsigned long", then by all means go ahead and use a typedef. | 353 | ``unsigned long``, then by all means go ahead and use a typedef. |
| 354 | 354 | ||
| 355 | (c) when you use sparse to literally create a _new_ type for | 355 | (c) when you use sparse to literally create a _new_ type for |
| 356 | type-checking. | 356 | type-checking. |
| @@ -359,10 +359,10 @@ useful only for: | |||
| 359 | exceptional circumstances. | 359 | exceptional circumstances. |
| 360 | 360 | ||
| 361 | Although it would only take a short amount of time for the eyes and | 361 | Although it would only take a short amount of time for the eyes and |
| 362 | brain to become accustomed to the standard types like 'uint32_t', | 362 | brain to become accustomed to the standard types like ``uint32_t``, |
| 363 | some people object to their use anyway. | 363 | some people object to their use anyway. |
| 364 | 364 | ||
| 365 | Therefore, the Linux-specific 'u8/u16/u32/u64' types and their | 365 | Therefore, the Linux-specific ``u8/u16/u32/u64`` types and their |
| 366 | signed equivalents which are identical to standard types are | 366 | signed equivalents which are identical to standard types are |
| 367 | permitted -- although they are not mandatory in new code of your | 367 | permitted -- although they are not mandatory in new code of your |
| 368 | own. | 368 | own. |
| @@ -373,7 +373,7 @@ useful only for: | |||
| 373 | (e) Types safe for use in userspace. | 373 | (e) Types safe for use in userspace. |
| 374 | 374 | ||
| 375 | In certain structures which are visible to userspace, we cannot | 375 | In certain structures which are visible to userspace, we cannot |
| 376 | require C99 types and cannot use the 'u32' form above. Thus, we | 376 | require C99 types and cannot use the ``u32`` form above. Thus, we |
| 377 | use __u32 and similar types in all structures which are shared | 377 | use __u32 and similar types in all structures which are shared |
| 378 | with userspace. | 378 | with userspace. |
| 379 | 379 | ||
| @@ -440,13 +440,13 @@ locations and some common work such as cleanup has to be done. If there is no | |||
| 440 | cleanup needed then just return directly. | 440 | cleanup needed then just return directly. |
| 441 | 441 | ||
| 442 | Choose label names which say what the goto does or why the goto exists. An | 442 | Choose label names which say what the goto does or why the goto exists. An |
| 443 | example of a good name could be "out_free_buffer:" if the goto frees "buffer". | 443 | example of a good name could be ``out_free_buffer:`` if the goto frees ``buffer``. |
| 444 | Avoid using GW-BASIC names like "err1:" and "err2:", as you would have to | 444 | Avoid using GW-BASIC names like ``err1:`` and ``err2:``, as you would have to |
