In programming there are many, many principles. One of the more useful ones that I’ve discovered lately is DRY: Don’t Repeat Yourself. It’s simple really, never ever copy code and don’t write the same code twice. In the real world it might not be as easy though…
Today I was about to check in some code and I went through my regular check-in routine (you have one as well I presume?) and discovered that I was violating DRY! At least if you’re being anal about it… The code in question was a little like this: (slightly altered to protect the innocents)
(I didn’t really return the language, but no need to include all of the source code)
So I was doing the double using in 4-5 places; a clear violation of DRY in my book. So what I did was this:
This method I can use like this:
Pretty neat, no? It might be overkill, but fun code to write anyway!