I often hear the term “stub”, “stub something out”, “stubs”, and so forth.

What does stubbing mean in programming, and where does the word come from? In what contexts can it be used?

2

A stub method is a method that just returns a simple but valid (though not necessarily correct) result.

They are typically made when building the infrastructure and you don’t want to spend time on every method needed right now. Instead you create stubs so everything compiles and your IDE’s auto-complete knows about the methods you are planning to use.

Another place where stubs are used is in mocking when testing, You supply stub methods instead of the normal dependency through dependency injection which return fixed results and then ensure that the code does the right thing with them. This isolates testing to the code you are trying to test and means you don’t need to spin up a database just to run those tests.

2

Perhaps this is best answered with a concrete example. Let’s consider the case where you’re creating a desktop application. You’ve decided to have a function named createMenubar, createToolbar and createMain so that your application constructor is neat and tidy.

Your main code will look something like this (this example is in python):

class ExampleApp():
    def __init__(self):
        self.createMenubar()
        self.createToolbar()
        self.createMain()

Until you have everything working, your main function will fail, so how can you test your app during development? For example, if you start out working on createMenubar, the initialization will fail after it creates the menubar since you haven’t implemented the toolbar or main area of the GUI.

This is where stubs come in. You can create a function named createToolbar and createMain that satisfy the requirement of the app constructor (namely, that these functions exist), without having to actually implement them. These functions are stubs:

def createToolbar():
    pass

def createMain():
    pass

With those stubs in place you can begin to implement the main program and the menubar, without having to worry about the details of the toolbar or main part of the program. Eventually you’ll move on to the toolbar, and replace the stub with the actual function. And finally, you’ll remove the stub for createMain and give that a real implementation.

Note that stubs don’t necessarily have to do nothing. These stub could, for example, create an empty container widget that the app constructor can lay out. The point is, you create dummy functions or objects that do just enough to let your other code run while you are developing it. Over time you replace the stubs one-by-one until your application is complete.

A “stub” is used in “top down” programming. That’s when you are building your application from the general to the specific. In so doing, you will use “stubs” as “placeholders” for the specific applications that you haven’t had time to build. They often return things like dummy or random values to the more general part of the program. That’s all right, the stub feeds enough of a value to make the more “senior” part of the program work. Until it is time for the stub to be replaced or fitted with its true computation or value.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật