Assignment 3 Code Review

Advanced Programming II - 20 March 2001


File Sizes - Newlines

1002003004005006007008009001000
01
10111
2011
3011
4021
50
602111
7021
801
9011
Totals4432230211


File Sizes - Semicolons

000100200300400
01
10
20
3022
402
50111
60312
701
80111
9011
Totals56461


Procedure Counts and Sizes

totalpages per procedure
procs123456789101112131415161718
11
11
11
211
211
211
431
52111
62211
6321
752
761
8422
8611
94212
14923
15123
1813221
191621
39314121
46451


Write Defensive Code


Define and Use Good Names


Don't Duplicate Code


void CharToMatchText(char ch) {
  char* Temp;
  int Length = 0;
  Length = strlen(match_text);
  Temp = new char[Length+1];
  Temp[0] = '\0';
  strcpy(Temp, match_text);
  Temp[Length] = '\0';
  delete [] match_text;
  match_text = new char[Length+2];
  match_text[0] = '\0';
  strcpy(match_text, Temp);
  match_text[Length] = ch;
  match_text[Length+1] = '\0';
  }

void CharToReplacementText(char ch) {
  char* Temp;
  int Length = 0;
  Length = strlen(replc_text);
  Temp = new char[Length+1];
  Temp[0] = '\0';
  strcpy(Temp, replc_text);
  Temp[Length] = '\0';
  delete [] replc_text;
  replc_text = new char[Length+2];
  replc_text[0] = '\0';
  strcpy(replc_text, Temp);
  replc_text[Length] = ch;
  replc_text[Length+1] = '\0';
  }

void CharToInterText(char ch) {
  char* Temp;
  int Length = 0;
  Length = strlen(inter_text);
  Temp = new char[Length+1];
  Temp[0] = '\0';
  strcpy(Temp, inter_text);
  Temp[Length] = '\0';
  delete [] inter_text;
  inter_text = new char[Length+2];
  inter_text[0] = '\0';
  strcpy(inter_text, Temp);
  inter_text[Length] = ch;
  inter_text[Length+1] = '\0';
  }

void CharToInText(char ch) {
  char* Temp;
  int Length = 0;
  Length = strlen(in_text);
  Temp = new char[Length+1];
  Temp[0] = '\0';
  strcpy(Temp, in_text);
  Temp[Length] = '\0';
  delete [] in_text;
  in_text = new char[Length+2];
  in_text[0] = '\0';
  strcpy(in_text, Temp);
  in_text[Length] = ch;
  in_text[Length+1] = '\0';
  }
vs

void CharToMatchText(char ch) {
  CharToText(match_text, ch);
  }

void CharToReplacementText(char ch) {
  CharToText(replc_text, ch);
  }

void CharToInterText(char ch) {
  CharToText(inter_text, ch);
  }

void CharToInText(char ch) {
  CharToText(in_text, ch);
  }

void CharToText(char * & text, char ch) {
  char* Temp;
  int Length = 0;
  Length = strlen(text);
  Temp = new char[Length+1];
  Temp[0] = '\0';
  strcpy(Temp, text);
  Temp[Length] = '\0';
  delete [] text;
  text = new char[Length+2];
  text[0] = '\0';
  strcpy(text, Temp);
  text[Length] = ch;
  text[Length+1] = '\0';
  }


Don't Use char * Strings

Compare
void optimize::CharToMatchText(char ch) {
  char* Temp;
  int Length = 0;
  Length = strlen(match_text);
  Temp = new char[Length+1];
  Temp[0] = '\0';
  strcpy(Temp, match_text);
  Temp[Length] = '\0';
  delete [] match_text;
  match_text = new char[Length+2];
  match_text[0] = '\0';
  strcpy(match_text, Temp);
  match_text[Length] = ch;
  match_text[Length+1] = '\0';
  }
to
void optimize::CharToMatchText(char ch) {
  match_text += ch;
  }


Write Good Code


Know Your Tools


Sometimes Even Strings Don't Help

string match_result;

match_result_length=match_result.length();

for (int check103 = 0; check103 <= match_result_length; check103++) {
  if ((match_result[check103] >= 65) && (match_result[check103] <= 90)) {
    // whatever
    }
  else
    output_result += match_result[check103];
  }


Dynamic Storage

bool readInst(List *L, char *&inst, int noInst) {

  char * line = new char[80];

  inst = new char[80];
  strcpy(inst, "\0");

  line = L->getNext();
  if ( line  == NULL ) {
    inst = NULL;
    return false;
    }

  // blah blah blah
  }


This page last modified on 27 March 2001.