Submit Your Site For Free!

Email Address:
* URL:
*
*Indicates Mandatory Field

Terms & Conditions

CProgramTrends
FlashNewz
DevWebPro








Writing A Quality Stub Method

By Bryan Young
Expert Author
Article Date: 2010-11-18

When writing a program of any size, it is always a good idea to write incrementally. What I mean is that you want to write one piece of code and test it till you work out all the bugs. Then once it works the way it's supposed to, you add more code to it and test that section. Continuing in this fashion will eventually get you a complete program with minimum bugs in it.

The only problem you run into is when you have a custom function to write, and you need to test the section of code that calls that function. You don't want to have to write the whole function before testing your code, so you need to write a stub function which mimics the return value. For example, if you are writing this code:


if ( getGradeFor("Bryan") > 65 )
{
grade = "pass";
}
else
{
grade = "fail";
}

You will need to write a function that takes the name of the student (in this case "Bryan") and retrieve information to calculate his grade, then return that value. Instead of going ahead and writing the function to do that, you can write a stub for testing purposes.


int getGradeFor(string studentName)
{
// write function to retrieve grade later
return 83;
}

As you can see, we are simply returning an arbitrary value as a means of testing our main code. You can change 83 to any value to test passing and failing grades, and also to test and edge values. Once you actually write your function, you can be sure that any wrong values are a result of your function code, and not something in your main code, since it will have already been debugged.



About the Author:
Bryan Young is a staff writer for WebProNews.



Newsletter Archive | Article Archive | Submit Article | Advertising Information | About Us | Contact

C Programming Trends is an iEntry, Inc. ® publication - All Rights Reserved Privacy Policy and Legal