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, such methods are often called mocks) 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 partially 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 declaring itself and the parameters it accepts and returning 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. In RMI nomenclature, a stub communicates on the server-side with a skeleton.
In the context of DOS and Windows, the term stub is also used in a fashion like shim to describe the small areas of interface code left in conventional memory by self-relocating resident drivers which move most of themselves into upper memory, the high memory area, expanded or extended memory as well as similar stubs to allow the relocated code to communicate with real-mode DOS in conjunction with DOS extenders (like DPMI, DPMS, CLOAKING or NIOS).
The small pieces of dummy code branched into to allow a graceful exit when invoking a fat binary in the wrong environment are also called (code) stubs.
- Nell B. Dale; Chip Weems (2004). Programming in C++. Jones & Bartlett Learning. p. 308. ISBN 978-0-7637-3234-9.
- "stub". WEBOPEDIA. Retrieved 2012-08-28.
- Freeman, Eric; Freeman, Elisabeth; Kathy, Sierra; Bert, Bates (2004). Hendrickson, Mike; Loukides, Mike (eds.). "Head First Design Patterns" (paperback). 1. O'REILLY: 440. ISBN 978-0-596-00712-6. Retrieved 2012-08-28.
- A Stub Generation System For C++ (PDF)
- Stub/mock frameworks for Java Review and comparison of stub & mock frameworks for Java
|This software-engineering-related article is a stub. You can help Wikipedia by expanding it.|