Quote:Original post by Nik02
My previous comment was based on the assumption that the people here knew the implications of deferencing in general; thus, I omitted the explanation of cases in which the overhead does matter. Whether or not it is an issue depends exactly on the application.
Dereferencing is mostly a technical detail. I was more referring to recent design debate.
Reasoning typically goes that a coordinate might represent an object which is not part of current world or scene. Or that algorithm might need to return "no matches found". And one day, your coordinate, a pair of x/y float values becomes AsynchronousCachingLockStepSimulationManager.
A coordinate is typically just that, x/y pair. It doesn't need methods, doesn't have invariants, it's just a piece of data.
Concepts like nullable in algorithms dealing with them aren't needed. While they may add syntactic sugar in case of (x,y)/0, they don't really add anything - the result of such operation is not null - it is a coordinate. And if sticking with OO, then something like a Normal is not related to Vector2D, though inheritance is a good fit - Normal is a specialization of Vector2D. But in practice that is bordering on overdesign for little to no gain. It's also something where OOD falls flat on the face and an area which is hugely unaddressed by popular languages.
And regardless of how such result is returned - only the caller can answer the question of what to do with invalid values and must anticipate them. null doesn't provide any of that, it's mostly a cop out as evidenced by NullObject concept, which is different from nullable.
Perhaps the only confusing thing here is thinking that coordinate is different from a number. It's exactly the same, and 2+3 = null does not make sense either.
Situations where result is invalid must be addressed a priori, not as magic behavior hidden by operations.
Just because something can be done using language syntax doesn't mean it should be.