When to use basic types (Integer, String), and when to write a new class?
- by belgarat
Stackoverflow users: A lot of things can be represented in programs by using the basic types, or we can create a new class for it.
Example: A social security number can be a number, string or its own object.
(Other common examples: Phone numbers, names, zip codes, user id, order id and other id's.)
My question is: When should the basic types be used, and when should we write ourselves a new class?
I see that when you need to add behavior, you'll want to create a class (example, social security number parsing, validation, formatting, etc). But is this the only criteria?
I have come across cases where many of these things are represented as java Integers and/or Strings. We loose the benefit of type-checking, and I have often seen bugs caused by parameters being mixed in calls to function(Intever, Integer, Integer, Integer). On the other hand, some programmers are opposed to over-designing by creating classes for "eveything".
Obviously, the answer is "it depends". But, what do you think, and what do you normally do?