As Simon mentions, we maintain a library (actually a number of them) for this type of stuff. It's great.
In .NET for example, we have a base library which includes string manipulations, generic exceptions that seem to always be missing (BadFormatException
, others). This code is highly tested and acquires a serious amount of robust-ness as the years go on. (it also makes a nice template for other platforms/languages, because while it might not be the same programming syntax, you don't really lose the oh, remember to check this).
We go so far as to include separate projects within our "framework". Examples:
Dymeng // core, common
Dymeng.Data
Dymeng.Net
Dymeng.Web
Dymeng.Web.Mvc
Dymeng.Console // has our args management, etc
These are all common code that we call upon from all projects. If I'm in an MVC layer, I add Dymeng.Web.Mvc
and have a library of solid helpers to make things easier. In fact, I'm not sure how we'd live without it. By splitting all these little timersavers up into separate projects, we output the dlls and load only the ones needed in the working projects.
We have a system for whether these go into static or extension methods as well. For these helper methods (as opposed to helper classes), we write them as static methods. Extension methods simply call the "master" static method (similar to how we won't write logic code in an event handler method, but instead will use the event handler to call the "logical" method).
Often times I'll write a static method, then some time later I'll be working on something and think I know I've written this, where's that myString.PadLeft()
extension? - at which point it's a quick jump into the lib to add the extension method for the static method that already existed.
Really, in my mind, the best thing about this though is the fact that once you get on board a system of using a common library/set of libraries like this, the code within the libraries becomes very well used and highly reliable. You know if you write another GetLineFromFile(path, lineNumber)
(or whatever) you might miss something even though you've written it four times in the past three years: with a library, you know it works and you don't have to worry about it.