Style Guide

Using good style is is a habit that every good programmer should strive for. The programs you write in this class will be graded for style as well as correctness. In particular, I will be looking at the following items.

Indentation

You may choose how deeply to indent your code, as long as each indentation is at least 2 spaces and the indentation is consistent. Here are two blocks of code with different indentation depths, both of which would be acceptable:

4 spaces:

    for(int x = 0; x < 10; x++) {
        if(x % 2 == 0) {
            printf("%i\n", x);
        }
    }

2 spaces:

  for(int x = 0; x < 10; x++) {
    if(x % 2 == 0) {
      printf("%i\n", x);
    }
  }

It is not acceptable to mix indentation levels like this:

    for(int x = 0; x < 10; x++) {
          if(x % 2 == 0) {
            printf("%i\n", x);
          }
    }

Curly braces must be lined up with the beginning of the block that they complete. The following is not acceptable:

    for(int x = 0; x < 10; x++) {
        if(x % 2 == 0) {
            printf("%i\n", x);
    }
    }

Spacing

Put a space on either side of operators. This makes it much easier to tell where variables end and operators begin.

Do not do this:

    for(int x=0; x<10; x++) {
        if(x%2==0) {
            printf("%i\n", x);
        }
    }

Putting a space after keywords like for, if, etc. is optional, but be consistent. Same goes for a space between a closing parenthesis and an opening curly brace. Both of these are acceptable:

    for (int x = 0; x < 10; x++) {
        if (x % 2 == 0) {
            printf("%i\n", x);
        }
    }

and:

    for(int x = 0; x < 10; x++){
        if(x % 2 == 0){
            printf("%i\n", x);
        }
    }

But inconsistency is not ok:

    for(int x = 0; x < 10; x++) {
        if (x % 2 == 0){
            printf("%i\n", x);
        }
    }

You may place the opening curly braces on the next line if you wish:

    for(int x = 0; x < 10; x++)
    {
        if(x % 2 == 0)
        {
            printf("%i\n", x);
        }
    }

But you must be consistent throughout your program.

Variable Names

Some rules for variable names: