Law of Demeter Ruby

Demeter is a very good principle, it is good food for thought, but you should not go all the way, especially if you are only reading data. I admit that I had a first negative reaction to the invocation of demeter – I saw the result of simplified demeter (Demeter method spam) and it`s not pretty. And at some point, I think an idea can blame its followers. Note, however, that you are referring to the “class formulation” (or “general formulation”) of the LoD (www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/general-formulation.html), which, according to the author (Karl Lieberherr), is not the “true LoD”. “Real” LoD is the most restrictive “object form” (www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/object-formulation.html). The general wording is simply easier to statically verify. The object form, in fact, excludes certain forms of concatenation that are allowed according to the general formulation. Now we know what Demeter`s law is. But why use it? Most blog posts don`t answer this specific question. What are the benefits or benefits of implementing the Demeter law in our Ruby software? [1] geek.susanpotter.net/2006/07/rubyisms-forwardables.html [2] www.ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html [3] railsmagazine.com/articles/4 Instead of reinserting it, I refer you to my answer to Susan above: avdi.org/devblog/2011/07/05/demeter-its-not-just-a-good-idea-its-the-law/#comment-247495349 you say the attempt is wrong because of Demeter. No, what`s wrong is the interface. The programmer violates Demeter, does not try. If AS didn`t have a delegate, you could easily implement department_name with try.

Even if AS provides a delegate, you might be less declarative, but you could and would be a common trial use case. That`s the problem with a simplified/robotic application of Demeter – you get a combinatorial explosion of strange methods satisfying Demeter in classes they don`t belong to. In this application, the experience is nothing special. You could write the same blog post about the phrase user.department.name. Although the blog post is obviously about Demeter`s law, its relationship to the sample code with try is only incidental. You can easily replace a method that has been “demetrized”. Just declare it before or after calling the Demeter method. At a code retreat I did last week, I found something similar: if you really write your unit tests in isolation and obsessively mock employees, then a violation of Demeter`s law becomes really easy to spot: in unit tests, you have nested stubs. Method chains are what make Ruby good! As an example, here is a method that takes a string and generates a “slug” to generate a friendly URL: In this context, a little mockery goes a long way. More thoughts. I`ve worked on many projects that had this seemingly fragile problem.

In any event, it necessarily pointed out design flaws. Not a large-scale design, but a low-level design of the kind I talk about in the article. If you have more than a few simple simulations for a test suite, your suite usually tells you something about how your objects will be factored (or not). If you have decided to change the street from the Location method to str, NoMethodError is thrown. The solution in this case is to create a new street or alias method in the localization model: the code now respects Demeter`s law: it is only coupled to the immediate interface of the user parameter. and delegate lookup:street, but retrieves data from the :str method. Demeter and stub awkwardness are ways to determine if your code can be simpler. You don`t have to write nested stubs, you need to see that you need nested stubs and refactor your code so you don`t have to. Less formal and specific to object-oriented languages that use this syntax, use only one dot in methods. My other comment is that I think the ternary part should have been in the country_stats method.

But aside from those two nitpicks, this was a fantastic article and will really help me in the future. I feel like I`ve covered it in this post before, but maybe I could expand on it in another. The point I tried above is that your heels and simulacra metastasize if you don`t listen to them and maintain good design discipline every step of the way. Simulations work great – they`re essential for true unit testing – but just as TDD only works if you`re disciplined on red-green refactoring, simulations only work if you use them in conjunction with rules like Demeter, as well as an acceptance or integration suite that tests integrations between units. I felt that using ActiveSupport`s delegated class method in your proposed solution was a bit out of principle, so I kept it simple and didn`t adopt the AS or ActiveRecord classes. I would like to see an experience where a complete application/problem is written strictly according to Demeter`s law, compared to where it is written in a more direct approach. Well, department.nil? ? Nil: department.name, how many code paths do you have there? In terms of types, I think it`s not really important, formally there is a type, but it`s a type that belongs to the desired output. You use try in situations where you get null or a string. Good parties: We do not explicitly cross the boundaries of two objects by associations (order.user…). We do this in a more abstract way by using wrappers for these attributes. The answer they found became known as “Demeter`s Law.” .