Method stub
Encyclopedia
A method stub or simply stub in software development
is a piece of code used to stand in for some other programming functionality. A stub may simulate
the behavior of existing code (such as a procedure
on a remote machine) or be a temporary substitute for yet-to-be-developed code. Stubs are therefore most useful in porting
, distributed computing
as well as general software development and testing
.
An example of a stub in pseudocode
might be as follows:
BEGIN
Temperature = ThermometerRead(Outside)
IF Temperature > 40 THEN
PRINT "It's HOT!"
END IF
END
BEGIN ThermometerRead(Source insideOrOutside)
RETURN 28
END ThermometerRead
The above pseudocode utilises the function ThermometerRead, which returns a temperature. While ThermometerRead would be intended to read some hardware device, this function currently does not contain the necessary code. So ThermometerRead does not, in essence, simulate
any process, yet it does return a legal value, allowing the main program to be at least partly tested. Also note that although it accepts the parameter
of type Source, which determines whether inside or outside temperature is needed, it does not use the actual value passed (argument insideOrOutside) by the caller in its logic.
A stub is a routine that doesn't actually do anything other than declare itself and the parameters it accepts and returns something that is usually the values expected in one of the "happy scenarios" for the caller. Stubs are used commonly as placeholders for implementation of a known interface, where the interface
is finalized/known but the implementation is not yet known/finalized. The stub contains just enough code to allow it to be compiled and linked with the rest of the program.
Software development
Software development is the development of a software product...
is a piece of code used to stand in for some other programming functionality. A stub may simulate
Simulation
Simulation is the imitation of some real thing available, state of affairs, or process. The act of simulating something generally entails representing certain key characteristics or behaviours of a selected physical or abstract system....
the behavior of existing code (such as a procedure
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
on a remote machine) or be a temporary substitute for yet-to-be-developed code. Stubs are therefore most useful in porting
Porting
In computer science, porting is the process of adapting software so that an executable program can be created for a computing environment that is different from the one for which it was originally designed...
, distributed computing
Distributed computing
Distributed computing is a field of computer science that studies distributed systems. A distributed system consists of multiple autonomous computers that communicate through a computer network. The computers interact with each other in order to achieve a common goal...
as well as general software development and testing
Software testing
Software testing is an investigation conducted to provide stakeholders with information about the quality of the product or service under test. Software testing can also provide an objective, independent view of the software to allow the business to appreciate and understand the risks of software...
.
An example of a stub in pseudocode
Pseudocode
In computer science and numerical computation, pseudocode is a compact and informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading...
might be as follows:
BEGIN
Temperature = ThermometerRead(Outside)
IF Temperature > 40 THEN
PRINT "It's HOT!"
END IF
END
BEGIN ThermometerRead(Source insideOrOutside)
RETURN 28
END ThermometerRead
The above pseudocode utilises the function ThermometerRead, which returns a temperature. While ThermometerRead would be intended to read some hardware device, this function currently does not contain the necessary code. So ThermometerRead does not, in essence, simulate
Simulation
Simulation is the imitation of some real thing available, state of affairs, or process. The act of simulating something generally entails representing certain key characteristics or behaviours of a selected physical or abstract system....
any process, yet it does return a legal value, allowing the main program to be at least partly tested. Also note that although it accepts the parameter
Parameter (computer science)
In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments...
of type Source, which determines whether inside or outside temperature is needed, it does not use the actual value passed (argument insideOrOutside) by the caller in its logic.
A stub is a routine that doesn't actually do anything other than declare itself and the parameters it accepts and returns something that is usually the values expected in one of the "happy scenarios" for the caller. Stubs are used commonly as placeholders for implementation of a known interface, where the interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...
is finalized/known but the implementation is not yet known/finalized. The stub contains just enough code to allow it to be compiled and linked with the rest of the program.
See also
- Abstract method
- Mock objectMock objectIn object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the...
- Dummy code
- Test stubsTest stubsIn computer science, test stubs are programs which simulate the behaviors of software components that are the dependent modules of the module being tested.Test Stubs are mainly used in incremental testing's Top-Down approach...
External links
- A Stub Generation System For C++ (PDF)
- Stub/mock frameworks for Java Review and comparison of stub & mock frameworks for Java