News for hackers. Stuff that matters.
Re:Write code. Not too much. Mostly functions. by Matheus28 on Monday December 21, 2020 @10:07PM (#25502420)
If you REALLY have to use all these different formats, I'd have one canonical one used everywhere in your app, and 2n methods to convert to different ones at api boundaries.
Re:Write code. Not too much. Mostly functions. by brundolf on Monday December 21, 2020 @10:32PM (#25502545)
I'd have one canonical one used everywhere, and 2n pure functions to convert at API boundaries. By making them pure functions you guarantee (or at least suggest by convention; depending on your language's type system) that those conversions will have no side-effects. As methods, you can never be sure as the caller whether there will be ramifications to calling them in new places.
Re:Write code. Not too much. Mostly functions. by bigbizisverywyz on Tuesday December 22, 2020 @09:26AM (#25506523)
Another advantage of that approach is that if you make sure you always persist the serial number in the canonical format, and immediately parse the incoming value into the canonical form, then comparing 2 serial numbers is easy and consistent - including e.g. when constructing db queries.

I encountered a similar problem where an identity number could be represented in different formats, and the solution had been to store the string representation in all its glorious permutations. Doing db queries to find anything by that key was then impossible until the representation had been made consistent.

Then when e.g. creating reports for different systems we could format the output as per that systems expectation.

Re:Write code. Not too much. Mostly functions. by cjohnson318 on Monday December 21, 2020 @10:12PM (#25502446)
Ah! Thank you, that is a very good middle road.