COMP 530: Lab Style Guide

Writing code with good style means that someone else should be able to be read and understood the intent behind your code. This "someone else" can often be you at a later point, needing to come back and understand what's going on, make changes, or extend functionality. For the purposes of grading, Style is split into 5 parts: compiler warnings, formatting, documentation, mechanics, and style. 1 point is assigned to each and then scaled to an appropriate percentage of the assignment grade.

What follows is a description of each graded part.


Consider the following programs which create a simple ascii cipher table. They are functionally identical except for the fact that one uses a function call rather than putting everything in main. The first program has multiple instances of bad style, while the second one is cleaner. The second may not be the best way to write this particular piece of code, but it is more modular and extensible than the first.

//No file purpose and use comment (Documentation)
#define _GNU_SOURCE

int main(int argc, char ** argv){
  char ct[128]; //Magic Number Use (Style), bad variable name (Documentation)
  int i;
  for(i=0; i<128; i++){ //Magic Number Use (Style)
    ct[i]=127-i; //No comment on why this needs to be done (Documentation)
  //The following comments say what the code is doing functionally, but don't
  //give an idea as to the reason that it's being done (Documentation)
  /* Randomizes the string */
  /* sets i to random table position */
  i = rand()%128;
  //printf("%d\n", i); //Commented out line of code (Style)
  /* sets last character to character at i */
  ct[127] = ct[i];
  /* sets character at i to '\0' */
  ct[i] = '\0';
  for(i=0; i<128; i++){
  printf("%03d", ct[i]); //Non-indented line (Formatting)
    printf(" "); //Defining specific behavior for the end of loop (Mechanics)
/* This program generates an ascii cipher table and prints it to standard out.
 * Compile with make and run with ./cipherGen */
#define _GNU_SOURCE

#define asciiLength 128

char * genCipherTable(char * cipherTable);

int main(int argc, char ** argv){
  char * cipherTable = genCipherTable(NULL);
  int i;
  if(cipherTable == NULL)
    return -1;
  /* Prints cipherTable with a space between each character code followed by
   * a newline */
  printf("%03d", cipherTable[0]);
  for(i = 1; i < asciiLength; i++){
    printf(" %03d", cipherTable[i]);

/* Either populates the existing cipherTable passed in as an argument or in
 * the case of a NULL arg, allocates space for a new table before populating it */
char * genCipherTable(char * cipherTable){
  int i;
  /* If no cipherTable was passed in, allocates space for one
   * Returns NULL on a failed allocation */
  if (cipherTable == NULL){
    cipherTable = malloc(asciiLength*sizeof(char));
    if (cipherTable == NULL)
      return NULL;
  /* Fills cipherTable in reverse order. This is necessary because we are
   * passing it to strfry which needs a null-terminated string. We then
   * call strfry before swapping the null terminator with an arbitrary
   * element of the table, which is necessary for complete randomness. */ 
  for(i=0; i < asciiLength; i++){
    cipherTable[i]=(char) (asciiLength-1-i);
  i = rand()%asciiLength;
  cipherTable[asciiLength-1] = cipherTable[i];
  cipherTable[i] = '\0';
  return cipherTable;


This style guide includes text from Jacob Fisher.

Last updated: 2023-12-13 08:53:52 -0500 [validate xhtml]