Like Tree17Likes

A development process

This is a discussion on A development process within the C Programming forums, part of the General Programming Boards category; Originally Posted by brewbuck The result will probably be a thousand syntax errors From a beginners standpoint myself, learning to ...

  1. #31
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    Quote Originally Posted by brewbuck View Post
    The result will probably be a thousand syntax errors
    From a beginners standpoint myself, learning to write C for an embedded platform, i have found that having a number of syntax errors promotes understanding of the platform becaus you need to traverse through a number of peripheral libraries to find the function you have declared or written incorrectly.

    --dave

  2. #32
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Old thread but since it has a sticky...

    I tend to apply two principles that really have helped me in the past. The first is leverage test-driven development by writing a test for the application first, then the function skeletons so that the test at least compiles. For example that is overly simple I am simulating an address book (this is C++) so I write a simple test app; no actual code exists yet..

    Code:
    // test to look up address
    int main(int argc, char *argv[])
    {
       int nRC = 0;
       CMyAddressBook addressBook;
       const char *someName = "John Jones";
       string strResultingAddress;
       // Attempt address lookup
       if( addressBook.lookup(someName, &strResultingAddress))
       {
          // found it
          cout << "Found address: " << strResultingAddress << endl;
       }
       else
       {
          // not found...
          cout << "No address found" << endl;
       }
    
    
       return nRC;
    }
    Of course this will not compile; if for no other reason CMyAddressBook doesn't exist. However what this does is gives you exactly what you need in terms of API code. In this case, you need a single method for doing lookups. One advantage of this is that you only code as much API as you need and not more. Too many times beginners tend to overcode solutions and library, wasting time and money. This method reveals exactly what you need and not one line more.

    However as written this will not compile so the next step in test-driven development is to write enough of the code so it will compile but not necessarily run. Thus you add a class to your code:

    Code:
    #include <map>
    #include <string>
    using namespace sd;
    
    // class to do address lookups
    class CMyAddressBook
    {
       private:
          map<string, string> addresses;
       public:
          CMyAddressBook();
          // lookup
          bool lookup(string strKey, string *results);
    
    
    
    };
    Next I build out a skeletal version for the lookup method:
    Code:
    bool CMyAddressBook::lookup(string strKey, string &strResult)
    {
       bool bSuccess = false; // seed result with failure
    
    
       return bSuccess;
    }
    The code now compiles but obviously doesn't work. This is where my use of comments come in. While this example is trivial in the extreme, I use comments n the function body to describe how the problem is solved:
    Code:
    bool CMyAddressBook::lookup(string strKey, string &strResult)
    {
       bool bSuccess = false; // seed result with failure
       // create a pointer to the beginning of the map
       // perform lookup.
       // if found, set the result code
       // set the results
       // return
    
       return bSuccess;
    }
    
    And finally add the code to finish the lookup. 
    bool CMyAddressBook::lookup(string strKey, string &strResult)
    {
       bool bSuccess = false; // seed result with failure
       // create a pointer to the beginning of the map
       map<string, string>::iterator ptr = addresses.begin();
       // perform lookup.
       ptr = addresses.find(strKey);
       // if found, set the result code
       if( ptr != addresses.end())
       {
          bSuccess = true;
          // set the results
          strResult = (*ptr).second;
       }
       // return
    
       return bSuccess;
    }
    And you are done. You have satisfied the requirements of the program, done no excess code and the heart of the lookup is well-commented. To recap:
    1. Write code to tell the story of how you want the program/solution to act.
    2. Write enough code to get it to compile,
    3. Write enough code (based on #2) so the application runs as expected.

    Peace...

    This basic method of development has served me well for years...

    Peace...
    Last edited by jeffcobb; 12-18-2009 at 09:47 AM.
    stahta01 and astonecipher like this.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #33
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    Thanks for the steps of programing!

  4. #34
    Registered User
    Join Date
    May 2009
    Posts
    1
    thanks a lot

  5. #35
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    Nice

  6. #36
    Registered User
    Join Date
    Jun 2010
    Posts
    182
    This is really a good and clean beginner tutorial. Thanks a lot.
    The comments are useful as well, real people with real experience.
    Of course if your boss is not happy with all that thinking before
    coding, or he doesn't like your comments inside the source code
    it could be a probelm

  7. #37
    Registered User mixmagz's Avatar
    Join Date
    Jan 2011
    Location
    C:Windows\system_32
    Posts
    4
    Good Guide for Begginers Thank you!

  8. #38
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    If you like it, use the like links!
    Too many "me too" 1-posters and spammers on this thread - closed.
    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.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 11:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM
  5. Happiest moment during development process....
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-02-2002, 11:40 AM

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