2 ms·
1 point by charlysl 21 minutes ago | edit | delete [-] Wouldn't it be better to use data abstraction instead of abusing primitive types? For instance dates ar
by charlysl 9y ago
1 point by charlysl 21 minutes ago | edit | delete [-]
Wouldn't it be better to use data abstraction instead of abusing primitive types?
For instance dates are often abstracted as a Date type instead of directly manipulating a bare int or long, which can be used internally to encode a date.
So, age, which isn't an int conceptually (should age^3 be a legal operation on an age?), could be modelled with an Age type. This, on top of preventing nonsense operations, also allows automatic invariant checking (age > 0), and to encapsulate representation (for instance changing it from an int representing the current age to a date of birth).