What are the pros/cons of using black-box reuse over white-box reuse?
White-box:
pros:
- simple (very natural concept)
- you have more control over things
cons:
- requires intrinsic knowledge on component internals
- can be difficult to implement (OO inheritance constraints)
sometimes it leads to broken\incorrect inheritance chains
Black-box:
pros:
- low coupling (gives late binding and other goodies)
cons:
- not obvious (code is much harder to understand)
- interfaces are more fragile than classes (i.e. interfaces vs inheritance)
I'm not sure what those specific terms mean, so I'll take a stab at defining what they are before I continue:
- Black box reuse is using a class/function/code unmodified in a different project
- White box reuse is taking a class/function/code from one project and modifying it to suit the needs of another project.
The pros to black-box reuse are that once the code has been written, debugged, and tested, you can reuse it countless times in different circumstances. The downside is that truly black-box-reusable code is rare and can take time and effort to format the API and calling code and make it consistent with the black box approach (no context leaking).
The pros to white-box reuse are that you can indeed use your code more than once without having to first extricate it from the original project. You simply copy and modify and you're on your way. This type of reuse is much more common, but it also has a few downsides. Mostly, if you discover a bug in one implementation, you need to check to make sure that it's fixed in all the other implementations. This can be difficult if they diverge widely, as often happens.
@Kyle,
Black-Box reuse means that you use component without knowing it's internals. All you have is a component interface.
White-box reuse means that you know how component is implemented. Usually White-box reuse means class inheritance.
In my experience, White box reuse is normally done through inheritance and black box is done through composition.
White Box Reuse
Pro: You can customize the module to fit the specific situation, this allows reuse in more situations
Con: You now own the customized result, so it adds to your code complexity.
Black Box Reuse
Pro: Simplicity and Cleanliness
Con: Many times it is just not possible
verdict:
I prefer Black Box whenever possible.