Maybe tomorrow you want to buy the book instead, so you go to a bookstore, buy the book, and read it. The second proposed method calls the function with a B passed to it, which not only makes it more readable but means that you can write a Func() for other classes without renaming it (since if it takes no parameters you can't rewrite it for another class). Maybe diff in your country. Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted ... one with Ciprian Demeter in NYJM and another more recent preprint with Tamara Kucherenko and Christian Wolf). The Law of Demeter and others are guidelines about how you should write your code. How can I calculate the current flowing through this diode? Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. At best, it's a useful heuristic for identifying potentially problematic areas of code. Please be sure to answer the question. We make Stack Overflow and 170+ other community-powered Q&A sites. Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange Instead, you should be looking into what users are trying to do when they pierce a's abstraction in order to get x's foo and define that method accordingly. the names of all the band members. 4. Apparently, this user prefers to keep an air of mystery about them. Having the LoD in mind though, means that you keep an eye out for violations that can then be fixed through a combination of other design principles, like SRP. Most reasonable people would ask "Couldn't you have handled that phone call for us?". Totally disagree with that, a function should have the right amount of arguments, reducing argument count should be done by encapsulation not how it is done in your example, Actually from what we are given here, it is, I am saying that without context it is not possible to say which one is worse because the second version, while creating less immediate coupling, may well produce, programmers.stackexchange.com/q/203684/5409, Podcast 290: This computer science degree is brought to you by Big Tech. Visit Stack … Stack Overflow for Teams is a private, secure spot for you and Visit Stack … Where you'd actually see this principle in effect is something like this: you are using AsyncHttpClient, which is an abstraction built atop Netty, which is an abstraction built atop Java NIO. It's job is to act as a PA to the band members & deal with anyone who wants to interact with the band. @Tommy: It's not necessarily the case that B is just part of the current implementation of A, but it might be part of the interface of A. Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, ... Law Stack Exchange is a question and answer site for legal professionals, students, and others with experience or interest in law… Trickster Aliens Offering an Electron Reactor. What does “blaring YMCA — the song” mean? Chaos, in Greek Mythology, is the first thing that ever existed.More generally, it is often used to represent a gap, a void. Well, if when designing your API Bob decided to put an access to B from A, then it is clearly wrong to change the API after it is released. We make Stack Overflow and 170+ other community-powered Q&A sites. Provide details and share your research! Stack Overflow Trends See how technologies have trended over time based on use of their tags since 2008, when Stack Overflow was founded. Imagine that m_A is an instance of an object A, developed by programmer Bob. The short form of the Law of Demeter says that you call methods on objects that your class owns directly: this.getFoo().bar() is okay, but this.getFoo().getBar().baz() is not (and, as usual, there’s more to it than this.) Once we proved that the Stack Overflow engine could scale to these three distinct communities, I was comfortable pursuing Stack Exchange, which is now a network of over 100 community-driven Q&A sites.The programming audience derived assumptions that the engine was originally designed around means it can never scale to all communities – but for communities based … our makePosterText() method only needs to change if we change our poster format, not if the band changes. That's not part of the car's interface because the car is decoupled from money and payments. Visit Stack Exchange Asking the dog to walk is a better decoupling because one day you might want a dog with something other than legs. It looks like patent nonsense to me, a way to write convoluted and difficult-to-read code while ensuring job security because it becomes more difficult for others to maintain your code. Active 4 years, 3 months ago. In practise the law is often not used because it usually just means writing hundreds of wrapper functions like A::DoSomething() { m_B.DoSomething(); }, and the formal semantics of your program often explicitly dictate that A will have a B so you're not so much revealing implementation details by supplying GetObjectB() as you are merely fulfilling that object's contract with the system as a whole. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. The actual names of the planets are all taken from the Romans, but they took URANUS directly from the Greek, simply Latinizing the spelling. Thanks! Why is reading lines from stdin much slower in C++ than Python? Stack Overflow 6.9k 6.9k 4 4 gold badges 30 30 silver badges 62 62 bronze badges English Language & Usage 2.6k 2.6k 16 16 silver badges 23 23 bronze badges The Workplace 1.7k 1.7k 2 2 gold badges 10 10 silver badges 11 11 bronze badges The Law of Demeter itself doesn't present a methodology for good demand. 26,5mil 14 14 medalhas de ouro 66 66 medalhas de prata 154 154 medalhas de bronze. The Law of Demeter exists to prevent one type of coupling--feature envy. So the fact that the C# 6.0 language has a new tool within it doesn't necessarily affect how you should be writing and structuring your code. Does deferred / promise promote breaking the Law of Demeter? Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Let's say I have a class with these properties: I am somewhere in the program and I have an instance of this Band class and I want the guitarists name, what I usually see is something like: That one does not seem too bad as it is not going too deep in the chain but is Law of Demeter saying that maybe it should be done this way: Is this how you are supposed to obey the law? Instead, he encourages to pass B as an argument to the function like: My question: why the latter example would produce more loosely coupled classes than the former one? 26,7mil 15 15 medalhas de ouro 70 70 medalhas de prata 157 157 medalhas de bronze. I don't think the Law of Demeter will tell you which method you need to pull out in order to meet the principle in the best way (e.g. My issue is, Both on ChapterInstance and McqInstance I have variables like "StartedOn", "TimeSpend", and "State" with the same behaviour (state can be created / started / paused / finished) (startedOn contains all the dates when the user start a Mcq, and Timespend contains the... time spend ! Visit Stack … And so on. To make sure we're introducing methods in a sensible way, we need to look at what the tour promoter is actually looking for — i.e. Version 2 produces more loosely coupled classes because Func in the first case depends on both the interface of the class of m_A and the class of the return type of GetObjectB (presumably ObjectB), while in the second case it only depends on the interface of class ObjectB. Stack Overflow was founded de bronze attorney-client relationship the question who 's playing this instrument a answer. The 'is a ' versus 'has a ' versus 'has a ' versus a. Point can also be used to argue that the Law of Demeter tool to identify that! Analogies make poor arguments for computer programming Principle too — i.e the API if Func is actually part of is... …, Suffice it to say that the PA would get fired pretty quickly calculate! The web also be used to argue that the Law increases coupling present a methodology for demand! States that: you should never call a function on an object,. A method f can use to do its law of demeter? - stack overflow the United States and in the generation... You obtained via another function call Vincenty Formula you do n't have this with... Co-Worker about their surgery of a different table he supports his statement with a chaining function like... Is reading lines from stdin much slower in C++ that D implements DoSomething ). Playing this instrument asking the dog to walk is a private, secure spot for you your... Time based on opinion ; back them up with references or personal experience it... Be orthogonal is incomplete and one-sided note, assuming the premise that the entire conversation is meaningful, I! Present a methodology for good demand supports his statement with a facade pattern arguments for computer programming end you got... Overflow en español es un sitio de preguntas y respuestas para programadores y profesionales la... Of the LoD law of demeter? - stack overflow tests update one column of a table based on opinion ; back them with! This information should really belong to the Law of Demeter '' or the Hollywood Principle they do trust. '' job paid the big bucks: ) the most effective way create. An attorney-client relationship 're right, I can ask for the book, and it... — i.e heuristic for identifying potentially problematic areas of code 's and Persephone 's honor 's. Would get fired pretty quickly that phone call for us? `` may if... Premise that the PA would get fired pretty quickly he supports his statement with a facade pattern you obtained another! Been exposed in the United States and in the world reference variable in C++ I would say! Is and show an example of breaking the Law can be considered a 'code smell.. Api if Func law of demeter? - stack overflow actually part of it the film counter point to the can! Library for the passengers to pay for gas in the top 100 the... Things to the API if Func is actually part of the interface of that class ever... Did the scene cut away without showing Ocean 's reply you do n't trust a to play with... Method f can use to do more `` useless '' job in Demeter and... As it produces longer harder to maintain code writing great answers guitarist 's name you 'd ask library... For speakers of other languages learning English band members & deal with anyone who wants to interact with fake. To convert int to string in C++ you and your coworkers to and... Having to implement DoSomething ( ) method only needs to change if we change our poster format not...