Special Programming Topic: Stubs & Drivers

In software engineering & programming, we use the terms stub and driver to denote programmer-generated testing tools for early debugging and testing of program functionality.

Stubs

A stub is a brief section of compilable code that serves as a placeholder for future work. For example, suppose you are to implement a function called generateRandInt() that returns a random integer from 1 to 100. If you don't know the code yet to do that, you can simply "stub" the function with a temporary "dummy" return value as follows:

public int generateRandInt()
{
    return 1; // eventually need to return a random integer 1..100
}

The purpose of a stub is to allow your program to compile so that you can test other functionality without having to complete the entire program at once. By putting in this stub, you can test to see if other functions work, and even whether this function is correctly accessible.

When other parts are working correctly, you can come back to the stub and finish the correct logic. In the above case, you would eventually come back and enter the following:

public int generateRandInt()
{
    //return 1; // eventually need to return a random integer 1..100
    return (int)(Math.random()*100 + 1); // Correctly returns random integer
}

Notice we commented out the "stub" line instead of completely deleting it. This will help in future editing, as you can simply comment out the working code and resubstitute the stub at any time.

Drivers

A driver is a brief test program that demonstrates the functionality of a class or portions of a class. We use drivers in the same way we use stubs - for early testing and debugging. For example, let's say we're supposed to create a program that uses the above generateRandInt() function in the RandInt class to generate and display five random integers, then do something with them. It might be useful to test to see if we can get one random integer first, just for testing purposes, rather than having to wait until the whole program is finished to test each piece.

A driver program to get a single random integer using the above class might look like the following:

public class RandIntTest
{
    public static void main(String[] args)
    {
        RandInt myRand = new RandInt();
        System.out.println("My first rand int is :" + myRand.generateRandInt());
    }
}

By using stubs and drivers effectively, we can cut down our total debugging and testing time by testing small parts of a program individually, helping us narrow down problems before they expand.

Stubs and drivers can also be an effective tool for demonstrating progress in a business environment. For example, if you are to implement four specific methods in a class by the end of the week, you can insert stubs for any other methods and write a short driver program so that you can demonstrate to your manager or client that the requirement has been met. As you continue, you'll replace the stubs and drivers with real code to finish your program.