obscure code and clear code

This is a discussion on obscure code and clear code within the C Programming forums, part of the General Programming Boards category; I have a real problem with programmers that write obscure code. Just to illustrate what I mean, here are two ...

  1. #1
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11

    Unhappy obscure code and clear code

    I have a real problem with programmers that write obscure code. Just to illustrate what I mean, here are two programs that do exactly the same thing. They calculate the powers of numbers one through ten from the first to the fourth power, and then print the results on the screen.

    Tell me which coding is easier to understand: the first one or the second one.

    First one:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int pwr(int a, int b);
    
    int main(void)
    {
      /* Declare a pointer to an array that has 10
         ints in each row. */
      int (*p)[10]; 
    
      register int i, j;
    
      /* allocate memory to hold a 4 x 10 array */
      p = malloc(40*sizeof(int));
    
      if(!p) {
        printf("Memory request failed.\n");
        exit(1);
      }
    
      for(j=1; j<11; j++)
        for(i=1; i<5; i++) p[i-1][j-1] = pwr(j, i);
    
      for(j=1; j<11; j++) {
        for(i=1; i<5; i++) printf("%10d ", p[i-1][j-1]);
        printf("\n");
      }
      system("PAUSE");
      return 0;
    }
    
    /* Raise an integer to the specified power. */
    pwr(int a, int b)
    {
      register int  t=1;
    
      for(; b; b--) t = t*a;
      return t;
    }
    Second one:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* function declaration */
    int pwr(int power, int base);
    
    int main(void)
    {
      /* declare a pointer to an array that has 10
         ints in each row. */
    
      int (*p)[10]; 
    
      /* declare row and column variables, respectively */
      int i, j;
    
      /* allocate memory to hold a 4 x 10 array */
      /*arrays are arranged as array[row][column]*/
    
      p = malloc(40*sizeof(int));
    
      if(!p)
      {
        printf("Memory request failed.\n");
        exit(1);
      }
    
      /* In the following for loop, the power is represented by i,
         the row of the array; the base is represented by j,
         the column of the array.*/
    
      for(i=1; i<5; i++) /* outer loop --> row */
      {
        for(j=1; j<11; j++) /* inner loop --> column */
        {
          p[i-1][j-1] = pwr(i, j);
        } /* end of inner loop */
      } /* end of outer loop */
    
      for(i=1; i<5; i++) /* outer loop --> row */
      {
        for(j=1; j<11; j++) /* inner loop --> column */
        {
         printf("%6d ", p[i-1][j-1]);
        } /* end of inner loop */
        printf("\n");
      } /* end of outer loop */
    
      printf("\n\n");
    
      system("PAUSE");
      return 0;
    }
    
    /* function definition */
    
    pwr(int power, int base)
    {
      int  t=1;
    
      for(; power != 0; power--) t = t*base;
      return t;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Well the 2nd one contains better use of {} to mark the limits of the for loops, but contains excessive pointless comments (stating the obvious "this declares a variable" for example)

    int row, col;
    would be better than
    int i,j;

    then the need for any loop comments goes away

    The call to malloc should be
    p = malloc(4*sizeof *p);

    pwr() is needlessly brief, considering power() isn't that much longer, but is far more readable
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11
    Thanks for the suggestions, Salem. I took you up on them. The very first code listing of this thread came straight out of a book on C! The second one was a rough draft trying to improve the readability of the code so future programmers wouldn't have to struggle with it.

    Here's the new code listing:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* function declaration */
    int power(int power, int base);
    
    int main(void)
    {
      int (*p)[10];
    
      int row, col;
    
      /* allocate memory to hold a 4 x 10 array */
    
      p = malloc(4*sizeof *p);
    
      if(!p)
      {
        printf("Memory request failed.\n");
        exit(1);
      }
    
    
      for(row=1; row<5; row++)
      {
        for(col=1; col<11; col++)
        {
          p[row-1][col-1] = power(row, col);
        }
      }
    
    
      for(row=1; row<5; row++)
      {
        for(col=1; col<11; col++)
        {
         printf("%6d ", p[row-1][col-1]);
        }
        printf("\n");
      }
    
      printf("\n\n");
    
      system("PAUSE");
      return 0;
    }
    
    /* function definition */
    
    power(int power, int base)
    {
      int  t=1;
    
      for(; power != 0; power--) t = t*base;
      return t;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The easiest way to program for clarity is to remove anything unnecessary and use intuitive names for you variables:
    Code:
    #include <stdio.h>
    
    int power ( int base, int n );
    
    int main ( void )
    {
      int base, pow;
    
      /*
        Print a table of powers 1 through 4
        with a base of 1 through 10 each.
      */
      for ( pow = 1; pow < 5; pow++ ) {
        for ( base = 1; base < 11; base++ ) {
          printf ( "%6d ", power ( base, pow ) );
        }
        printf ( "\n" );
      }
    
      return 0;
    }
    
    /*
      Raise base to the nth power
    */
    int power ( int base, int n )
    {
      int result = 1;
    
      while ( n != 0 ) {
        result *= base;
        n--;
      }
    
      return result;
    }
    My best code is written with the delete key.

  5. #5
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11
    Wow, prelude! I am very impressed! Your C coding acumen leaves me dazzled and awed.

    It never occurred to me that the program coding didn't need an array assignment or anything like that. The book I got the first code listing from was just trying to illustrate the use of pointers. So I took the pointers for granted without realizing that it was merely used for the sake of illustration.

    Kudos to ya, prelude!

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I suggest looking at as many different sources of code as you can, like here on the boards. After I branched out from my first book I was able to see things I liked and didnt like about it, as well as good and bad things. Dont always consider that a book will have good code, I've seen many examples of either completely incomprehensible or broken code in books from people such as Deitl and Deitl (main book used to teach corporate employees) and Bjarne Stroutrup ( the man who created C++).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i am not clear about peace of code..
    By shwetha_siddu in forum C Programming
    Replies: 3
    Last Post: 07-14-2008, 07:00 AM
  2. Quicksort problem, now with clear code
    By rvanbeusichem in forum C Programming
    Replies: 1
    Last Post: 11-25-2004, 12:54 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Clear up this code if you have time!
    By Crypto2002 in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2002, 10:07 AM
  5. clear screen code, can't get it to work
    By Unregistered in forum C++ Programming
    Replies: 11
    Last Post: 01-25-2002, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21