C Coding Style Guidelines

CPE 101

Instructor: Julie Workman

 

 

A coding style is nothing more that a set of rules for formatting code to make it readable by people.  Using a consistent coding style makes it easier for you to read your own code making errors easier to find and fix.  Another benefit is that it makes your code more readable for others.  This quarter that will primarily be a benefit to your instructor and the graders that evaluate you code.  In the future it will benefit your coworkers and consumers of your programs.  You will find that programmers all have there favorite styles - over time you will realize that using a style, any style, is more important than which particular style.  For this class I am going to ask you to follow a few style rules - realize that a complete set of style guidelines would be considerably more complete that this one.

 

 

The following header MUST be included at the top of each of your source files:

/* Your name
   CPE-101-section number
*/

 

 

if (i == 5)

{

/* Do Something... */

i += 7;

}

 

            Never this! (even though it is syntactically correct)

 

if (i == 0) {

/* Do Something... */

}

 

 

if (i == 0)

{

j++;

}

 

Never this! (even though it is syntactically correct)

 

if (i == 0)

j++;

 

And never this! (even though it is syntactically correct)

 

if (i== 0) j++;

 

 

if (thing1 != thing2)

{

/* Do something useful */

}

else

{

/* Do something else useful */

}

 

if (condition1)

{

/* Do something */

}

else if (condition2)

{

/* Do something different and useful */

}

else

{

/* Do something completely different and useful */

}

 

for (i = 0; i < x; i++)

{

for (j = 0; j < z; j++)

{

/* Do something useful! */

}

 

if (isHardToMakeMeaningfulExamples)

{

/* Do something else useful when something is true */

}

 

whatever++;

}

 

 

 


Style Examples


Example of Good Style:

 

int test(int number)

{

   int count;

 

   for (count = 1; count < MAX; count++) 

   {

      if (number+1 > i && number-1 < count) 

      {

         printf("hello\n");

      }

      else  

      {

         printf("goodbye\n");

      }

      number = number + 1;

   }

 

   count = number;

   while (count > MAX)

   {

      count = count - DECREMENT;

   }

}

 

Example of Bad Style:

int Test( int n ) {         <-- uppercase name, { on same line, spaces before/after parentheses

   int i;                   <-- one letter name, no bland line after variables

   for (i=1;i<5;n=n+1,i++)  <-- bad horizontal space, use of constant       

   {                       

      if (n+1>i && n-1 < i) <-- Single letter variable names, poor spacing

         {                  <-- double indentation

            printf("hello\n");

         }

      else {                <-- brace on the same line

            printf("goodbye\n");

         }

   }

   i = n;

   while(i > 10)            <-- no space after "while", use of constant

      i = i - 2;            <-- no braces (compound statement)

}