![]() |
| | #31 |
| Embedded in C... Join Date: Sep 2008 Location: Basingstoke, Hampshire
Posts: 81
| --dave |
| droseman is offline | |
| | #32 |
| Registered User 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;
}
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);
};
Code: bool CMyAddressBook::lookup(string strKey, string &strResult)
{
bool bSuccess = false; // seed result with failure
return bSuccess;
}
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;
}
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. |
| jeffcobb is offline | |
| | #33 |
| Registered User Join Date: Dec 2009
Posts: 2
| Thanks for the steps of programing! |
| professor1991 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |