Kyle’s Reference Links

Contents External Links Friendly Links Marvin Links

The New C Rules

Disclaimer
  1. If consequences must use brackets unless the consequences are the same line as the if. (Rationale)
    Acceptable:
    • if (something) do_this;
    • if (something) { do_this; }
    • if (something) {
      do_this;
      }
    Unacceptable:
    • if (something)
      do_this;
  2. No more than 3 embedded ?: operators embedded in a single expression in regular code (5 are allowed for macros).
  3. Parenthesis are required for embedding ?:
    Acceptable
    • foo = something?(somethingelse?one:two):three;
    • foo = (something?(somethingelse?one:two):three);
    Unacceptable:
    • foo = something?somethingelse?one:two:three;
  4. A tab is a tab is a tab. For formatting purposes, your text editor should use TABS ('\t', ascii character 9). If you want to view documents with different tab widths, that's the perogative of your text editor - the actual file should be unaffected by your preference and should still use TABS.

Rationale

  1. For Rule 1: When multiple people edit the same file, some things can be changed without explicit testing. A file may start out with a simple line:

    do_this();

    One person may add an if (preference) before it, while someone else changes do_this(); into do_this_setup(); do_this(); do_this_cleanup(); The code will become:

    if (preference)
    do_this_setup();
    do_this();
    do_this_cleanup();

    And the logic will very suddenly BREAK without notice, even though both changes were valid changes. If we simply require if's to either be on the same line as their consequence or have brackets around their consequence, these changes will either conflict (and someone will notice this and can fix it) or they will Just Work ™
  2. For Rule 2: This rule is for clarity.
  3. For Rule 3: This rule is for clarity.
  4. For Rule 4: The problem with whitespace is that you can't see it. Because of that, spaces and tabs look exactly the same to the person setting up the file originally. Tabs are frequently used to justify code, which is useful for visual clarity. Tabs are good because they can be changed to different widths depending on the preference of the viewer. Spaces have a set width, and cannot be changed. Thus, replacing a tab with 8 (or however many) spaces means a loss of flexibility. In a sense, the tab is a meta character, meaning "this should be indented" without specifying how much.

    Many text editors have certain situations where they will use spaces instead of tabs for indenting. In vi, the method for "fixing" a file that has been mixed, use the command :retab!. You can also use the indent program.

Disclaimer

Of course, if you are the only person who will ever work on this code and you're very sure that changes from multiple places at multiple times could never possibly happen, and you don't mind spending the time to figure out what you were getting at when you originally wrote it, then you're free to do whatever you like with your code (including stick it up your nose). However, if other people may conceivably at some point be using or working with or looking at your code, please, follow these rules.
Valid XHTML 1.0 Strict! Valid CSS! Lovingly handcrafted with Vim