3 ms·
> The only problem is that you end up with a variable that fails to describe itself better than “I’m a number”. Our application servers need to know about upda
by EdwardDiego 13y ago
> The only problem is that you end up with a variable that fails to describe itself better than “I’m a number”.
Our application servers need to know about updated entities, so we broadcast notifications when an entity has been updated - but we prefer to leave it up to the individual applications to retrieve the updated entity as they see fit.
So we're often dealing with numerical ids of entities, and we've had a few bugs arise from code like so
public void something(long accountId, long websiteId)
When people have accidentally used the wrong long in the wrong place. So we've now moved to using simple wrapper types around aforementioned longs -
public void something(AccountId accountId, WebsiteId websiteId)
It does come at a little bit of cost as we're passing these things around a messaging system, so now we have to think about how they are serialized (Java serialization is problematic, and there are occasional religious wars about GPB vs. JSON), but it leverages the type system to make it every explicit what this number represents.
- eropple 13y agoThis method also create a lot of object churn. For reasonably small systems that can be okay, but you have to be mindful of it.
- gizmo686 13y agoWould it be possible to do this as a type-alias type thing that disapears at compile time? As far as I am aware, Java has no support for such type aliasing, but it should be possible to add a pre-compiler phase to your build process that replaces these types with what they are aliases of. The only part of this that seems complicated is the type checker, which would need to be aware of the difference between AccountId and long.
- yareally 13y agoI could see it being done with annotations + processing them perhaps, as some frameworks do for database properties such as index/uniqueness. An object for every scalar datatype is just overkill, even if it solves a legitimate problem.
- eropple 13y agoYou could make it a pre-compile step in Maven, maybe. But it'd be gross.