1 CS 241 Coding Standards

2 Primary Reasons for Defined Standard

  1. A standard makes it easier for the instructors to read your code.
  2. A class standard makes it easier for a grader to recognize when a program does not use a consistent standard.

    Often when each student is allowed to define his or her own standard, students switch standards multiple times in a single project. It is tedious for a grader to deduce each persons standard and then check for self-consistency.

  3. It is good practice to learn to follow a standard.

3 Coding Standard: Naming

4 Coding Standard: Function Comments

At the top of every function, there must be a comment block with the following information:

/************************************* 
 * Each parameters type and name: 
 *  input and/or output, 
 *  its meaning, 
 *  its range of values. 
 * Functions return value. 
 * Description of what the function 
 *  does. 
 * Functions algorithm 
 *************************************/

5 Coding Standard: File Comments

At the top of .c source file, there must be a comment block with the following information:

/************************************* 
 * Your first and last name 
 * 
 * Description of what the file 
 *  is used for and how to use it. 
 * 
 *************************************/

6 Coding Standard Open Brackets

Open brackets will be placed at the beginning of a line (not at the end).

/* OK *
if (x == 5) 
{ 
  y = y+1; 
}
/* Not CS-241 standard *
if (x == 5) { 
  y = y+1; 
}

7 Coding Standard Closing Brackets

Closing brackets will be indented on a line with no other commands. The only exception being comments placed on the line with a closing bracket.

if (x == 5) 
{ 
  y=y+1; 
} /* Comment here OK *
else if (x == 7) 
{ 
  y=y+2; 
}
if (x == 5) 
{ 
  y=y+1; 
  /* Next line is     *
  /* not 241 standard *
} else if (x == 7) 
{ 
  y=y+2; 
}

8 Coding Standard Blocks and {}

Whenever a structure spans more than one line, brackets must be used. For example:

/* OK *
if (x == 5) y=y+1;
/* OK *
if (x == 5) 
{ 
  y=y+1; 
}
/* Not CS-241 standard *
if (x == 5) 
  y=y+1;

9 Coding Standard - Indenting

10 Coding Standard 80 Character Line Max

No line shall be more than 80 characters.

The best way to avoid overly long statements is by not doing too much in a single statement.

/* Way too long! *
if (getVolume(length1, width1, height1) > getVolume(length2, width2, height2)) printf(”box 1 is bigger\n”); else printf(”box 2 is bigger\n”);
/* Use temporary variables *
int volume1 = getVolume(length1, width1, height1); 
int volume2 = getVolume(length2, width2, height2); 
if (volume1 > volume2) 
{ 
  printf(”box 1 is bigger\n”); 
} 
else 
{ 
  printf(”box 2 is bigger\n”); 
}
/* Too long and repeats expression *
if (stack[topOfStack - 1] == ’*’ || stack[topOfStack - 1] == ’+’ || stack[topOfStack - 1] == ’-’ || stack[topOfStack - 1] == ’/’)
/* Much better! *
char c = stack[topOfStack - 1]; 
if (c == ’*’ || c == ’+’ || c == ’-’ || c == ’/’)
/* long line broken without indenting! *
if (commandOption ==’f’ || commandOption == ’c’ || 
commandOption == ’d’ || commandOption == ’g’)
/* indented to line up *
if (commandOption == ’f’ || commandOption == ’c’ || 
    commandOption == ’d’ || commandOption == ’g’)

11 Dead Code Elimination

Code that is unreachable or that does not affect the program should be eliminated. This includes:

int foo(void) 
{ 
  int x, i; /* i is never read *
  i = 1; /* dead store *
  x = 1; /* dead store *
  x = 2; 
  return x; 
  x = 3; /* unreachable *
}

12 Replace Needlessly Deep Nestings

if (c == ’*’) multiply(); 
else 
{ 
  if (c == ’+’) add(); 
  else 
  { 
    if (c == ’-’) subtract(); 
    else 
    { 
      if (c == ’/’) divide(); 
      else error(); 
    } 
  } 
}
if (c == ’*’) multiply(); 
else if (c == ’+’) add(); 
else if (c == ’-’) subtract(); 
else if (c == ’/’) divide(); 
else error();

13 Avoid Code Duplication

int sum1 = 0; 
int sum2 = 0; 
for (int i=0; i<4; i++) 
{ 
  sum1 += array1[i]; 
} 
average1 = sum1/4; 
 
for (int i=0; i<4; i++) 
{ 
  sum2 += array2[i]; 
} 
average2 = sum2/4;

The two loops should be rewritten as the single function:

int calcAverage (int array[]) 
{ 
  int sum = 0; 
  for (int i=0; i<4; i++) 
  { 
    sum += array[i]; 
  } 
  return sum/4; 
}

14 Keep Function Size Functional

Source code, as it appears on the display, becomes an extension of the programmers mind: it is used to organize, remember, and articulate thoughts.

It is fine for a list of constants or simple statements to scroll off the screen, but when complex logic spans more lines than fit on the display (usually about 40), then the code becomes difficult for a human to think with.

Best practice is to extract logical units from such code and place each unit in a function, even when such functions are only called from one place in the program.

15 Structs

struct Point 
{ 
  int x; 
  int y; 
}
struct Point pt;
struct Point 
{ 
  int x; 
  int y; 
} pt;
struct Point {int x; int y;} pt;
/* Not CS241 standard *
struct Point { 
  int x; int y; 
} pt;