For classes and functions, it is clear cut: each should do only one thing.

For libraries though, this is less clear. If you have a library with collections, it might have multiple collections. It might have useful functions like sorting, which aren't strictly collection based but users would expect. Each of these results in a 'wider' library.

On the other side is having a library for the specific collection type and/or with little built-in functionality. If you want a queue, it gives you a queue. If you want to sort that list, then the library lets you do that yourself.

What is the best practice here (if any)? I can see arguments for each side.


Wide enough to make the main elements of the library usable.

Your example with sorting is a good one. If your target language/environment has default sorting functions for collections and you, are implementing collections that cannot be sorted with the normal sort functions, then I would think it would be desirable to include sorting functions.

If you can implement the collections to sort with the default functions of your target environment then adding helper functions probably is not necessary and the library can be lighter.

Occasionally it is useful to add a convenience method even when the default functions do work if your library is a specific enough that there would be specific custom sorts that would always be needed like a job title seniority sort on an employee collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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