Question

I'm working on an application that saves phone numbers, the mask for the phone number is (99) 9999-9999.

Should I save the whole string on the database. i.e.:

(99) 9999-9999

or just the data i.e.:

9999999999

and only format it in the UI?

I'm leading towards the second one but I couldn't give good reasons on why is that. My coleague argument was that the first one (the one with the mask) would be easier, since it's not necessary to apply the mask in different UIs (reports, webpage).

Was it helpful?

Solution

Separate data and presentation logic - this is good practice.

I suggest you to store only number, as number is data, and formatting is not (store only data in database).

Second, maybe for this moment you have 1 format, but believe me - in some time you will need another format and then you will have to re-format it (some kind of murphy's law)

Of course, for performance reasons you can cache visual presentation - create additional field(s) for it and use it for display, update it when main "data" field is updated

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top