Thread: How good Design a program

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    4

    How good Design a program

    Hi
    This is my first post on this forum,before anything, thanks to all for contributing on this forum.
    I am newbie in C programming.Before starting to write a program I want to know more about "How good design a program".
    May you give me some hints and basic references about that ?

    Thanks for any help or guidance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ISBN-10: 0471204595

    There are plenty of books on the subject. Think of what your program needs to do. Think of the steps needed to do it. Build a bit, test a bit. Repeat until done.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Too broad a question! A good design for one program might be a bad one for another, it depends on what you're trying to accomplish.

    I admire your diligence in thinking about design up front, but I'd say it's pretty tough to think about design in C before you have a familiarity with the language.

    Basically you need to make your program functional, and then trade off between performance and readability. I'd say always go for maximum readability as the 1st goal. Well named variables, common code split into functions, indentation.... little style things that'll make your life easier. Don't compromise on readability! As soon as you feel like you're hacking away at the code you probably are, and maybe should back off and think some more. You can always tweak for performance later -- this depends on the architecture/core you're running on too.

    Don't be shy of aggregate types (arrays, structures etc) - they help in keeping data organised and are worth the fairly short time investment to learn about. Get comfortable with pointers, life in C is hard if you only half understand pointers. Likewise the preprocessor - it's simple but will trip you up over and over if you don't understand it.

    Hmm, that's all I can think of off the top of my head. There are lots of common 'design patterns' in C, not formally like for OO languages, but common patterns that emerge that can give rise to elegant ways of doing things. Best way to find out about that kind of thing is to look at other people's code, though that can be really intimidating and time consuming.

    Enjoy!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    #1 problem with beginners is that they start immediately writing code, before they understand the problem in the detailed level needed, for programming.

    Work through the process the program needs to undertake, by hand. Use a very simple test case, of course. After 2 times or so, you'll start seeing the patterns you are following to do it. Note them, in small steps, and in order. That's hopefully the backbone of your pseudo code.

    Now take your pseudo code from above, and fill it out with any additional steps it appears to need. Use this version of your pseudo code, to start your program with. Just work on the functions you want or need, and the basic data structures that make it easy to do the task.

    Now get the basic flow of the program working - and leave out ALL the irrelevant details (especially output formatting, etc.), and get the core of your program logic worked out - what order the functions should be called in, their parameters, return types, etc. You should be testing your code by frequently compiling and running your little test case, at the end of this part of the design process. Make sure your warnings from the compiler, are being solved, if possible.

    When you have the rough logic working OK, then start adding in the final details. Testing as you go, for both accuracy and syntax/logic errors, as much as possible. The worst thing in the world of programming is to have a huge amount of code, that has not been tested. Then, when you try to compile it, you get hundreds of errors and warnings!! That is a real PITA!

    Except for large declared data structures where you need the global memory, keep your variables local. Use prototypes for your functions, so no "default" parameters are used or needed. Declare any struct above main(), so every function can easily declare a pointer or a struct with that definition, easily.

    Use functions based on their own functional use in the program: input, computations, output, etc.

    Strive for accuracy first, then clarity, then speed and brevity. Avoid any useless logic or variables, in favor of brief and clear code. Give your variables names that relate to their function in the program - i,j,and k are common iterators. Anything else, give it a descriptive name, and don't use l (ell) for a variable.

    That should get you started.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    The first step is to understand the problem of what you are trying to solve. Then break that problem into smaller problems. Then solve each smaller problem.

    As for writing code, follow the ABCs, Always Be Cleaning. Meaning always keep your code clean and readable. Readable as in someone from the street knows what such and such variable is used for. In the long run it will save you a lot of time. Use symbols and words that make sense, for example, if you are writing a program that uses the "Equation of a Straight Line," then use the standard notation y=mx+b and not something crazy like g=rl+a.

    For writing actual code, be consistent in your spacing and usage of brackets, like the following.

    Code:
     
    
    #include <stdio.h>
    
    int main()
    {
      int a=1;
      int b=2;
      int c=3;
    
      if ( c < b )
      {
        printf("C is greater than B!\n");
    
        if ( a < b )
        {
          printf("A is less than B!\n");
        }
      }
    
      printf("Hellow World!\n);
    
      return 0;
    }
    As you can see, it is easy to read and there is no question as to what is happening. The above code could have been written as

    Code:
    #include <stdio.h>  int main() { int a=1; int b=2; int c=3; if ( c < b ) { printf("C is greater than B!\n"); if ( a < b ) { printf("A is less than B!\n"); } } printf("Hellow World!\n); return 0; }
    But it is harder to read and understand, and I can promise you if you write code like the above, other programmers will want to hurt you in a bad way.
    Last edited by Strahd; 04-01-2012 at 01:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program design ideas
    By KBriggs in forum C Programming
    Replies: 2
    Last Post: 06-11-2010, 08:45 AM
  2. Program design in C
    By Bob Austin in forum C Programming
    Replies: 4
    Last Post: 10-19-2006, 05:06 AM
  3. Good architecture/design needed for "sample pipeline"
    By johny145 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 10:43 AM
  4. Help with design of program
    By beyondsociety in forum C Programming
    Replies: 12
    Last Post: 08-07-2003, 01:34 PM
  5. Unicode & Good Program Design :: C++
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-05-2002, 04:09 PM