Stubs & Drivers in Programming
Sometimes, while creating a project it may happen that you want to test current functionality of the program, but that cannot be done because all of the required methods or functions have not been created or some of the functionality is still missing. Lets take a look into Stub.
What is a Stub?
A stub is a method with few or no code(only the declaration exists, no Implementation). The stub usually contains the default return value and parameters(which may or may not change in the future). The purpose of the stub is to let the program execute with default values in most of the places. By this you can check if the program works correctly or not.
How it works?
suppose I have a function which gets data from database but I haven’t written the code for fetching the data. I have another function display() which calls the method getData() and displays the returned arrayList to the User. But, IDK if display() function is working or not because the function getData() does not exist right now. Here, what I can do is create a dummy arrayList in getData() function and return it to display(). It will work like a normal function but, it won’t get data from the database. And I can also check the functionality of the display() method.
How does it Help?
By using stub you don’t need to change the code again and again. Just call the function getData() which will return the dummy data and won’t effect the execution flow when the getData() is actually written.
Personally I use stub a lot(it saves time in big projects).
Drivers
Drivers are an opposite thing compared to stub. Drivers are complete functions. Drivers are used to check the correctness of the program. By giving some dummy data as input and checking if the produced result is correct or not based on the requirements.
Drivers call other functions with predefined inputs and outputs. Which allow them to verify the correctness of the function or the code written.
Comments
Post a Comment