News for hackers. Stuff that matters.
Re:Law of Demeter and immutability by debacle on Friday September 30, 2016 @11:17AM (#12612958)
> 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.
Re:Law of Demeter and immutability by lomnakkus on Friday September 30, 2016 @11:39AM (#12613101)
... or you could just use a language which doesn't allow "null".
Re:Law of Demeter and immutability by recursive on Friday September 30, 2016 @11:52AM (#12613191)
Or use a null safe member access operator like the one in c#.

    player?.position.x
Re:Law of Demeter and immutability by douche on Friday September 30, 2016 @12:37PM (#12613525)
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.
Re:Law of Demeter and immutability by lomnakkus on Friday September 30, 2016 @12:58PM (#12613646)
That doesn't really solve the problem. What if "x" is a nullable type (i.e. not a primitive type)?
Re:Law of Demeter and immutability by recursive on Friday September 30, 2016 @01:36PM (#12613888)
The nulls get coalesced. In what way is it not solved?
Re:Law of Demeter and immutability by lomnakkus on Friday September 30, 2016 @01:53PM (#12614040)
"x" could be of a nullable type.
Re:Law of Demeter and immutability by recursive on Friday September 30, 2016 @03:22PM (#12614647)
And the result of the entire expression is still nullable. I'm not seeing any problem.
Re:Law of Demeter and immutability by lomnakkus on Friday September 30, 2016 @04:01PM (#12614903)
Hm. What do you think "42 + null" is?
Re:Law of Demeter and immutability by recursive on Friday September 30, 2016 @05:41PM (#12615430)
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

Re:Law of Demeter and immutability by lomnakkus on Saturday October 01, 2016 @08:30AM (#12617585)
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.

Re:Law of Demeter and immutability by recursive on Saturday October 01, 2016 @10:20AM (#12617947)
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.

Re:Law of Demeter and immutability by debacle on Friday September 30, 2016 @11:53AM (#12613202)
I can't think of a programming language used for game development that doesn't have null.
Re:Law of Demeter and immutability by kazagistar on Friday September 30, 2016 @02:50PM (#12614431)
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.
Re:Law of Demeter and immutability by coldtea on Friday September 30, 2016 @04:10PM (#12614981)
That made my head hurt.
Re:Law of Demeter and immutability by lomnakkus on Friday September 30, 2016 @01:01PM (#12613661)
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.)

Re:Law of Demeter and immutability by gpderetta on Friday September 30, 2016 @12:03PM (#12613273)
C++, unless pointers are explicitly used, which weren't in the OP example.
Re:Law of Demeter and immutability by coldtea on Friday September 30, 2016 @04:09PM (#12614972)
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.