Hacker News new | past | comments | ask | show | jobs | submit login

> The law of Demeter is a guideline

I'm not really sure that much more had to be said than that. There are many instances (especially when you have a data structure that is also a class) where the law of Demeter is a guideline and just that.

However, the caveat is that this:

    int positionX = player.Position.X;
Should really look like this:

    if(player.Position != null) {
      int positionX = player.Position.X;
    }
In which case having a getter eliminates the need for error handling to be spittled all over the codebase. Having an accessor means you can nip checking for a null reference in the bud. There are other benefits as well.



... or you could just use a language which doesn't allow "null".


Or use a null safe member access operator like the one in c#.

    player?.position.x


If it's C#, you could also make Position be a struct that can't be null. Generally things like Points and Vectors are structs anyway in a lot of cases for performance reasons.


That doesn't really solve the problem. What if "x" is a nullable type (i.e. not a primitive type)?


The nulls get coalesced. In what way is it not solved?


"x" could be of a nullable type.


And the result of the entire expression is still nullable. I'm not seeing any problem.


Hm. What do you think "42 + null" is?


According to c# (the language I'm talking about) I think it's null. That seems correct to me. Arithmetic on null operands logically seems to have a null result to me. I'm not sure what you're getting at.

https://dotnetfiddle.net/nQoEKu


That's OK, I.. guess. I don't think I should even be allowed to ask the question because it's an absurd question[1]. Does that position make sense to you?

[1] Not for frivolous reasons. It's almost always nonsensical to ask this type of question with "null" values. For the sensical cases we have Maybe/Option.


I think I see what you want now. In order to get the value out, you'd have to use pattern matching or a type guard or something, inside of which you'd know there was a value.

That makes sense to me too. But compared to Maybe, a null-safe member accessing operator, along with null-lifted operators seems nearly as good from my point of view.


I can't think of a programming language used for game development that doesn't have null.


In Rust, an `&Thing` (reference to thing), `Option<&Thing>` (optional reference to thing), `Box<Thing>` (thing on the heap), and `Option<Box<Thing>>` (optional thing on the heap) all have the same in-memory representation of a single pointer, but prohibit accidentally dereferencing a null pointer, and make non-nullable pointers the default.


That made my head hurt.


I'm not sure I understand exactly why this is specific to game development? (I see that the article uses a game-like example, but...)

Even so: I'm pretty sure Rust is suitable for game development being essentially zero-overhead, non-GC and Rust effectively disallows "null".

(That, and C++ if you allow only references like another poster mentioned.)


C++, unless pointers are explicitly used, which weren't in the OP example.


Still not solving all the problems that an accessor does.

Say you suddenly want to change the position to another unit, or change how it uses position (perhaps return a different position on some cases).

With direct access, you can't.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: