Written by Manuel Fernández Sánchez de la Blanca

sábado, 14 de agosto de 2010

Is it a number?

Today, while browsing http://stackoverflow.com/, I found someone asking about how to store a phone number in an integer variable while not losing the leading zeros, i.e., he stored the value 02478829 in an integer, but when printing he got 2478829. Some users gave him "tricks" like using number formatters, implementing his own PhoneNumber class which takes care of it,... That's wrong!

Yes, it's true that the user can achieve his goal with the provided tricks, but the problem is more than a technical issue. The problem is misunderstanding what a phone number really is.
A phone number is not really a number, there is no sense in performing arithmetics operations with them. Phone numbers are codes, which usually happens to be represented like numbers, but that's just a convention. Maybe, in the future, phone numbers include letters too; in fact, international prefixes begin with a + (plus sign) or with 00, indistinctly. So, the adequate way to store a phone number is using a String.

Before coding, it's important to do some thinking about the nature of the things we want to translate into software, so we can choose the most suitable structures to represent them.

No hay comentarios:

Publicar un comentario