law-of-demeter

How do I apply the Law of Demeter to this?

删除回忆录丶 提交于 2019-12-12 03:18:01
问题 I have an admittedly ugly query to do, to find a particular role related to the current role. This line produces the correct result: @person_event_role.event_role.event.event_roles. joins(:mission_role).where(:mission_roles => {:title => 'Boss'}). first.person_event_roles.first.person (You can infer the associations from the plurality of those calls) The only way to get this information requires a ton of knowledge of the structure of the database, but to remove the coupling... It would

IntelliJ Refactor to use LoD

萝らか妹 提交于 2019-12-05 06:07:50
问题 Say I have some class Foo class Foo { protected String x = "x"; public String getX() { return x; } } I have a program that uses Foo and violates LoD class Bar { protected Foo foo; public Bar() { this.foo = new Foo(); } public Foo getFoo() { return foo; } } public static void main(String [] args) { Bar bar = new Bar(); String x = bar.getFoo().getX(); } Refactoring to use LoD looks like this: class Bar { protected Foo foo; public Bar() { this.foo = new Foo() } public String getFooX { return foo

Simple Custom Refactoring in IntelliJ

孤街浪徒 提交于 2019-12-03 10:25:51
This question is a follow-up for this . Say I have some class Foo. class Foo { protected String x = "x"; public String getX() { return x; } } I have a program that uses Foo and violates LoD ( Law of Demeter ). class Bar { protected Foo foo; public Bar() { this.foo = new Foo(); } public Foo getFoo() { return foo; } } public static void main(String [] args) { Bar bar = new Bar(); String x = bar.getFoo().getX(); } I can refactor this code to use LoD in two steps. ⌥ ⌘ m bar.getFoo().getX() -> getFooX(bar) (extract to method, also find and replace occurrences) F6 getFooX(bar) -> bar.getFooX() (move

Law of Demeter confusion

感情迁移 提交于 2019-12-02 04:55:42
问题 I'm hoping someone can help explain the law of demeter to me. If I have a class which I'm assuming is an aggregate root and within that have a collection of child classes is it illegal to update the properties of those child classes by accessing them through the aggregate root? e.g. public class Company { // company has a number of employees public List<Employee> Employees {get; set;} } public class Employee { // each employee has a lastname public int Id {get; set;} public string LastName

Law of Demeter violation search tool? [closed]

感情迁移 提交于 2019-12-02 00:43:46
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . Does anybody know of a tool that I could use with a C# application to find possible Law of Demeter violations? I know that it would give a lot of false positives, but I think it could still be useful. Especially during the early design process. 回答1: If you're just looking for something.somethingelse.violation ,

Law of Demeter violation search tool? [closed]

烂漫一生 提交于 2019-12-01 21:41:43
Does anybody know of a tool that I could use with a C# application to find possible Law of Demeter violations? I know that it would give a lot of false positives, but I think it could still be useful. Especially during the early design process. If you're just looking for something.somethingelse.violation , then you can use Visual Studio. In the find dialog, check the box at the bottom to "Use" and select "Regular Expressions." Not very robust, but you can use <[:a_]+\.([:a_]+\.)+[:a_]+ to find the pattern above. A better tool would be grep or similar on the solution directory, so you can use

The Law of Demeter

╄→гoц情女王★ 提交于 2019-11-30 19:07:41
问题 I recently posted a question on stackoverflow where I did something to effect of @period_registration.period.event However, it was suggested that I do something like the following: def event period.event end @period_registration.event My general sense is that this seems a little heavy-handed. Looking at this previous posting How do I apply the Law of Demeter to this? shows how heavy handed this can become if you did this for every association. How common of a practice is this in rails? My

How to solve the violations of the Law of Demeter?

女生的网名这么多〃 提交于 2019-11-28 15:32:14
A colleague and I designed a system for our customer, and in our opinion we created a nice clean design. But I'm having problems with some coupling we've introduced. I could try to create an example design which includes the same problems as our design, but if you forgive me I'll create an extract of our design to support the question. We're developing a system for the registration of certain treatments for a patients. To avoid having a broken link to image I'll describe the conceptual UML class diagram as a c# style class definition. class Discipline {} class ProtocolKind { Discipline; }

Coupling, Cohesion and the Law of Demeter

删除回忆录丶 提交于 2019-11-27 10:25:52
The Law of Demeter indicates that you should only speak to objects that you know about directly. That is, do not perform method chaining to talk to other objects. When you do so, you are establishing improper linkages with the intermediary objects, inappropriately coupling your code to other code. That's bad. The solution would be for the class you do know about to essentially expose simple wrappers that delegate the responsibility to the object it has the relationship with. That's good. But, that seems to result in the class having low cohesion . No longer is it simply responsible for

Law of Demeter with data model objects

旧城冷巷雨未停 提交于 2019-11-27 04:25:59
问题 I came back to work from vacation yesterday, and in our daily standup, my teammates mentioned they were refactoring all of the model objects in our java code to remove all getters and setters and make the model fields all public objects instead, invoking the Law of Demeter as the reason for doing so because to facilitate the our adherence to Demeter's law: a module should not know about the innards of the 'objects' it manipulates. Since data structures contain no behavior, they naturally