Subsections

- 6.1 Whitespace
- 6.2 Braces
- 6.3 Names
- 6.4 Miscellaneous

# 6 C coding style

If you want to submit patches to Notion, you must follow my coding style, even if you think it is the root of all evil. We don't want the code to be an incomprehensible mess of styles and I have better things to do than fix other people's style to match mine. The style should be obvious by studying the source, but here's a list of some things to take note of.

## 6.1 Whitespace

- Indentations of 4 with spaces.

- No extra spaces between operators, delimiters etc. except
  - around logical and, or (`&&`, `||`)
  - around the conditional `a ? b : c`
  - after commas and semicolons
  In my opinion this helps pointing out arithmetic or other expressions within logical expressions or parameter lists.

- All kinds of labels are out-tended to the level of the higher level block. For example:

      void foo()
      {
      again:
          switch(asdf){
          case 1:
              ...
              break;
          default:
              ...
              break;
          }
      }

## 6.2 Braces

- Opening brace is at the end of the line, except in function bodies, where it is at the beginning of the line following the definition.

- Never put the body of a control statement on the same line with the statement (e.g. `if(foo){ bar() }`).

  For example, the block

      void foo(int a, int b)
      {
          if(a==b && c+d==e){
              ...
          }
      }

  has correct style while the block

      void foo(int a,int b) {
          if (a == b && c + d == e) {
              ...
          }
      }

  does not.

- The `else` keyword follows immediately after the closing brace of previous `if`, if any. (This might change so I don't care if you put it on the next line.)

- I have used the convention that control statement bodies containing a single statement do not need braces around the block if, in case of the `if` all the blocks in `if ... else if ... else` contain just one statement. If you want to, just use braces in every case.

## 6.3 Names

- Function and variable names only have lower case letters. Type names are in mixed case while constants and macros (`#define`s) are in upper case letters.

## 6.4 Miscellaneous

- In the definition of a pointer variable, the asterisk is attached to the variable name: `char *s;`. (One could claim this an exception to the second rule.)

- You might optionally want to use Jed's foldings to group blocks of related code in a file to keep it organized:

      /*{{{ Many related functions */

      void code()
      {
          ...
      }

      ...

      /*}}}*/

I think that's mostly it. Study the source when in doubt.