I got an interesting lesson in code duplication today.
In reviewing some of our legacy code, I happened across this simple looking piece of code:
and it occurred to me to wonder... how many places are we calling that constructor?
And the answer is: a lot.
A quick scan of the various repositories I had cached locally showed that three different format strings were each being used to initialize the SimpleDateFormat at more than 50 different places in our code.
“Don’t Repeat Yourself” was never about code. It’s about knowledge. -- Mathias Verraes
Could it really be the case that this code was, in fact, DRY? Had we written the code the most effective way we could?
The strongest piece of evidence I have? Timestamps on the changes. The lines of code in question are old. They haven't needed to change in the years since they were written.
Why not? A large chunk of the credit must belong with the fact that SimpleDateFormat is part of the standard library, and the Java team has ensured that functionality has been sufficiently stable for our use.
A secondary possibility is that the date format is taking an in memory representation of a date time, and converting into an human readable representation to be included in a message, or vice versa. The adapters are stable because the message formats have been stable.
Because the message formats are stable, the code in question has very little reason to change. As a consequence, we don't get burned when we take what could be a call into a common method and instead choose to inline it.
What I think this suggests: when/if we decide to update the date formatting and parsing logic, we should be expecting to keep the actual format strings inline. Encapsulating the implementation details is, I think, easier to justify when you are in the middle of an exercise to change those details.