Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pattern or rule i can't remember

I read a long time ago about a rule that using an object we shouldn't point from one class to the other due to null possibility.

 class Foo
 {
   public Bar Bar1 {get; set;}

   public Foo()
   {

   }

 }

 class Bar
 {
     public string Name{ get; set;}
 }


 Foo foo = new Foo();
 foo.Bar1.Name;

what is the law or rule called after this pattern?

like image 442
guyl Avatar asked Jul 19 '26 21:07

guyl


2 Answers

It's the Law of Demeter

To quote some of it:

In particular, an object should avoid invoking methods of a member object returned by another method. For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as "use only one dot". That is, the code a.b().Method() breaks the law where a.Method() does not.

like image 173
BrokenGlass Avatar answered Jul 22 '26 12:07

BrokenGlass


I think your referring to the Law of Demeter

like image 23
Mikael Härsjö Avatar answered Jul 22 '26 12:07

Mikael Härsjö



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!