Skip to content

CS Coding Style Guidelines

Brian "Beej Jorgensen" Hall edited this page Dec 18, 2018 · 3 revisions

Though individual programs might have their own guidelines, these are the style guidelines for the CS portion of the curriculum.

Python

Be compliant with PEP 8.

C

We'll use a variant of K&R style.

  • Function braces on the next line after the function definition.
  • Flow control braces on the same line as the flow control statement.
  • 4 spaces for an indent.
  • Spaces around operators for clarity.
    • Just a space after a ; or ,.
  • Asterisk next to the variable name in pointer declarations.
#include <stdio.h>

int mul2(int x)
{
    return x * 2;
}

int main(void)
{
    int i, j;
    char *p, *q;

    for (i = 0; i < 20; i++) {
        if (i > 10) {
            printf("Hi!\n");
        } else {
            printf("Hello!\n");
        }
    }

    return 0;
}