What do you consider good use of wrapper functions? When are they useful abstractions and in what cases harmful and unnecessary complexity?
|
Good uses for wrapper functions:
Bad uses:
|
||||
|
|
To expand on @dsimcha answer (just giving examples) I usually wrap a number of C++ STL algorithms, simply because they are all designed in term of iterators, but I find myself calling them over whole collection most of the time. Therefore:
Becomes:
Small gain, but still:
It can also help introducing sanity checks, for example the
Of course using the STL in debug mode would probably perform this check, depending on the implementation... or so I can hope... but I'd rather make sure :) And there is, finally, the embodiement of simple idioms. For example, erasing elements according to a predicate in C++ can be done using:
This can be wrapped up in:
to simply caller code, and once again make sure it does not mess up the arguments/idiom. |
|||||
|
|
Just look at any template from the Spring framework That is good wrapping in action. They have taken all the common use cases for a wide variety of standard JEE technologies (JPA, JMS, JAX-RS, JAXB etc) and made a neat set of templates that act as simple base classes to extend from. These template classes greatly simplify the simple use cases of the libraries and allow developers to get the basics done really easily. Once you've found your feet with the required technology, then you can explore the other base classes provided which wrap the more sophisticated classes. What makes them so good is that they all follow a very similar approach which means that once you've got the hang of one template then you have a good head start on the next. |
|||
|
|