Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What do you consider good use of wrapper functions? When are they useful abstractions and in what cases harmful and unnecessary complexity?

share|improve this question

3 Answers 3

up vote 15 down vote accepted

Good uses for wrapper functions:

  1. Hiding the complexity of a horribly complex or unsafe, low-level API, for example, the Win32 API. However, your wrapper must actually reduce complexity and/or increase safety for the common cases.

  2. Making something cross-platform when you are writing a generic library or have a good reason to believe it needs to be cross-platform.

Bad uses:

  1. Because we can.

  2. Because maybe, at some point, we might switch to a new database in our quick n' dirty app we're writing.

  3. Because I personally don't like the API for some bikeshed reason even though the wrapper isn't actually hiding any complexity.

share|improve this answer
    
I've added some examples (using C++ STL as a base case) of cases where wrapping can makes sense in my answer programmers.stackexchange.com/questions/71181/… feel free to pick some up if you so wish. –  Matthieu M. Apr 26 '11 at 18:54

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:

template <typename RndIt>
void sort(RndIt begin, RndIt end);

Becomes:

template <typename Container>
void sort(Container& c) { std::sort(c.begin(), c.end()); }

Small gain, but still:

  • I cannot mess up and pass end first and begin second
  • It makes caller code more readable

It can also help introducing sanity checks, for example the includes algorithm can only be called if both collection are sorted...

template <typename Big, typename Small>
bool includes(Big const& big, Small const& small) {
  assert(std::is_sorted(big.begin(), big.end()) && "big not sorted");
  assert(std::is_sorted(small.begin(), small.end()) && "small not sorted");
  return std::includes(big.begin(), big.end(), small.begin(), small.end());
}

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:

c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());

This can be wrapped up in:

template <typename Container, typename Pred>
void erase_if(Container& c, Pred pred);

to simply caller code, and once again make sure it does not mess up the arguments/idiom.

share|improve this answer
    
With your fancy wrappers you seem to just duplicate what is already in boost::range. –  Benjamin Bannier Mar 29 '12 at 10:56
    
@honk: Which I guess support my propos: wrapping is useful ;) I like Boost.Range, however it does not add the "debug" guarantees. –  Matthieu M. Mar 29 '12 at 11:41

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.