C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-28-2009, 04:24 AM   #31
Embedded in C...
 
Join Date: Sep 2008
Location: Basingstoke, Hampshire
Posts: 81
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
droseman is offline   Reply With Quote
Old 12-18-2009, 09:45 AM   #32
Registered User
 
jeffcobb's Avatar
 
Join Date: Dec 2009
Location: Henderson, NV
Posts: 541
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...
__________________
C/C++ Environment: GNU CC/Emacs
Make system: CMake
Debuggers: Valgrind/GDB

Last edited by jeffcobb; 12-18-2009 at 09:47 AM.
jeffcobb is offline   Reply With Quote
Old 12-19-2009, 10:15 AM   #33
Registered User
 
Join Date: Dec 2009
Posts: 2
Thanks for the steps of programing!
professor1991 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
init adopts zombie process? password636 Linux Programming 4 07-01-2009 10:05 AM
How can you make a parent process wait for a child? I'm gettin a seg fault. mr_coffee C Programming 3 10-15-2008 09:24 AM
Problem with forking a process Unitedroad C Programming 10 10-04-2007 01:43 AM
process programming St0rM-MaN Linux Programming 2 09-15-2007 07:53 AM
Happiest moment during development process.... Nutshell A Brief History of Cprogramming.com 16 04-02-2002 11:40 AM


All times are GMT -6. The time now is 09:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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