Basili et al.[3] published experimental results in 1996 suggesting that a lower response for a class (RFC, the number of methods that can be called in response to a call to a method of that class) can reduce the likelihood of software errors. Following Demeter`s law can result in a lower RFC. However, the results also suggest that increasing class-weighted methods[4] (WMC, the number of methods defined in each class) may increase the likelihood of software errors. Following the Demeter law can also lead to a higher WMC; see Disadvantages. You can call methods from objects that are:1. Pass as arguments2. Created locally3. Instance variables4. Globals After delegating the calculation of costs, the business class is greatly simplified. The same should be done at the ministerial level.
Version 2 produces looser coupled classes because in the first case Func depends on both the m_A class interface and the return type class of GetObjectB (presumably ObjectB), while in the second case it depends only on the interface of the ObjectB class. Rigorous implementation of the law ensures that only the fields defined in the current class can be used. On the other hand, the weak Demeter law allows you to use field values from the current class and also inherited class values. Both solutions offer some advantages and disadvantages. A strict law encourages the encapsulation and localization of information. The advantage is that the subclasses are immune to structural changes in the superclass. The weak implementation of the law makes it easier for us to access data and limits the number of methods to write for new functions. Usually, each layer of an architecture has a number of interfaces with which it is possible to communicate, and if we think about the layers that hide their implementation, we avoid problems like those we have seen before. With regard to the second point, I think we are getting lost in the debate `is one` versus `has one`. „To one“ is a very natural way of expressing certain object relationships, and dogmatically hiding that behind a façade of „I have the keys to the locker, so if you want your locker open, come and ask me, and I`ll unlock it“ – as conversations only obscure the task at hand. As a general rule, if you see that you need to access the structure of an object that contains logic, there is certainly a more elegant way to do it. EDIT: Since some people want more exposure, let me try this: Small steps when refactoring might be easier.
Note that Demeter`s law says which method should know, but not what the class itself knows. We can use it to get around the rules a bit to start refactoring. The starting point can be a self-call to an internal method that encapsulates nested calls for us. Object a can request a service (a method) from an instance of object b, but object a must not „pass“ through object b to access another object, c, to request its services. This would mean that object a implicitly requires greater knowledge of the internal structure of object b. An easy way to recognize when this law is being violated is that you end up with a lot of chained calls, but that`s not a 100% reliable reason (read on to the end and you`ll see why), and it`s not the only reason either. And it`s a clear symptom that the code isn`t done right. So far so good.
The problem occurs when one of the classes has to use some of the objects a few levels lower. We rolled the blanket over our heads and started creating getters as if there was no tomorrow. This can be dangerous. Well, I think it should be obvious why function concatenation is bad, because it produces code that is harder to maintain for longer. In the example above, Func() is an ugly function as it seems to happen simply like this: As always, this article was inspired by Uncle Bob and his book Clean Code. This is the most obvious option and the one I recommend the least. Instead of having multiple calls, let each object run its corresponding subcalls. You might have something like this: A quote in one of the remnants of the project seems to clarify the origin of the name: concatenation methods aren`t the only thing that violates Demeter`s law. Remember that the law allows you to only talk to the object you know directly, so it is a violation to ask any other object what it is. In my opinion, you don`t need to be extremely strict with these rules, but you have to see when we actually use the system to load the rules.
He supports his statement with a concatenation function like Like everything in this world, it is interesting to understand the concept behind this law, but not to consider it as something we cannot ignore or ignore. In any case, we need to examine whether it makes sense and why. Well, in the whole project, if we chain objects, it could lead to a lot of places where things need to change. How to avoid chaining? If this structure changes tomorrow in one of the classes involved, this code will be affected. Is it bad? It depends, but usually yes, because this code is very likely to change: a single refactoring can result in a change. Similar to shameless assignment, in this scenario you also reuse the value returned by a method. The only difference is that the result of the methods is not stored in another variable, but is returned directly.