Now I am reading the book PURE code on a small chapter about the law of Demeter. Prior to that, he encountered in patterns from O'reilly (HeadFirst) and there was a little bit clear. After a clean code - in the head of the mess. There are a couple of questions:
1) Martin divides objects into objects and data structures. What is meant by structure? Struct (in c # for example) or in general a class object in which there is nothing more than open fields (or properties) (methods, for example)?
2) The first question implies a misunderstanding of what is meant by a hybrid (half object, half data structure), which is better not to write. Is it considered a hybrid, for example, an object of the “Machine” class, where you can look at its color, even change the wheels, and have a behavior too?
3) This question was raised for oral discussion by other developers (we are not junas, but not “cool”) and there was also a misunderstanding of the very essence of this law, why it should be observed, which is the main reason: either the basis for further light-change, or hiding the internal structure of the object, or even the easy prevention and handling of a NullReferenceException? Everyone understands differently.
4) Is the law violated when dividing the "bad" methods into many small ones?
It was
public class Class1 { public String DoSmth() { //Вроде как закон нарушен, потому что вызываем метод у вернувшегося значения return MethodClass1().DoSmth(); } public Class2 MethodClass1() { return new Class2(); } } public class Class2 { public String DoSmth() { String res2 = this.MethodClass2(); return res2; } public String MethodClass2() { return "test"; } } It became
public class Class1 { public String DoSmth() { //теперь тут вызываются только методы того же класса Class2 res1 = MethodClass1(); return this.MethodClass1_2(res1); } public Class2 MethodClass1() { return new Class2(); } public String MethodClass1_2(Class2 val) { return val.DoSmth(); } } Another small update: I personally have this attitude so far: this is some kind of rule, the need for explicit violation of which is an indicator that something is not so designed and you can find a solution that will be better both in business logic and in further escort.