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)
.
Couldn't we benefit from having function(OrderId, ExternalOrderId, UserId, SalesManId)
isntead and benefit from type checking, even though none of these alone justifies having its own class?
Obviously, the answer is "it depends". But, what do you think, and what do you normally do?