Live data from Hacker News

Law of Demeter and immutability

enterprisecraftsmanship.com

31–38 of 38 posts

Re: Law of Demeter and immutability

#31
post #11

Earlier quoted context omitted.

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 ` (optional reference to thing), `Box ` (thing on the heap), and `Option >` (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.

Re: Law of Demeter and immutability

#32
End goal is to make code that is easier to maintain and understand.

The intermediate goals should be to reduce coupling, increase encapsulation, etc. These are tools to achieve the end goal.

Multiple dots is just a code smell, indicating that you're probably not meeting your intermediate goals or the end goal. If you see multiple dots, you should probably examine that code to see if it "wants" to be somewhere else, in service of the Primary goal.

But if you elevate "multiple dots" to the level of a "Law" then you're looking at the problem from the wrong end, and are in real danger of losing sight of the end goal. You can follow this law right down a hole that works AGAINST your real goal.

Re: Law of Demeter and immutability

#33

Earlier quoted context omitted.

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

Re: Law of Demeter and immutability

#34

Earlier quoted context omitted.

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.

Re: Law of Demeter and immutability

#35

Earlier quoted context omitted.

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.

Re: Law of Demeter and immutability

#36

Disagree with the premise. The purpose of Demeter isn't to prevent one class from mucking up the internal state of another, that's a side-effect. Its purpose is to prevent one class from being coupled to the internal structure of another so that if you change one, you don't have to change the other. It stops code updates, not state updates, from rippling out. This is why the original formulation of the LoD didn't tal…

Getting most object-oriented programmers to think about the static structure of their programs (e.g., classes rather than objects, variables rather than runtime values) is going to be an uphill battle.

Re: Law of Demeter and immutability

#37
post #26

This "law" sounds like a reasonable idea, at first, but if you think about it for a few seconds, you'll realize that it makes little, if any, sense (at least, when taken out of some specific context). As the designer of the class, often neither you can possibly anticipate all the ways in which objects of the classes involved will be used, nor would you want to re-expose all (or some of) the APIs provided by the "subo…

It's a law as in "law of nature" not "law of the land". Things which follow LoD can be observed to have certain properties.

Re: Law of Demeter and immutability

#38
post #5

The author claims that Dementer's law is primarily there in order to prevent state corruption. I don't know if I agree with this premise. There are also 2 other reasons to follow Dementer's law: 1. Make it easier for your users to achieve specific goals through a single method call, instead of having to know & go through a chain of method calls. Ie, instead of having to know that the Dog object has Leg sub-objects, a…

Coupling is an important concept but I think this concern can be mitigated by following the DRY principle. That is, if you have only one function that knows about the object’s internal structure (“couples” to it, so to speak), it doesn’t really matter where exactly that function is located – in the class itself or in some other place. Also, your example isn't entirely valid as you mutate the objects' state.

It seems like you're trying to redefine what the LoD is because you think there's a more important principle it should refer to instead, or because you disagree with its original framing. That's not going to be a fruitful approach.
Post reply on HN