Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:09:11+00:00 2026-06-16T05:09:11+00:00

Law of demeter says that an object can’t invoke a method M from an

  • 0

Law of demeter says that an object can’t invoke a method M from an object B from an object A.
But is it aplied to properties too? Example?

public class B{
     public bool IsValid();
}

public class A{
     public B B{get;set;}
}

Can I do something like that?

var isValid = new A().B.IsValid()

or should I do this:

public class B{
     public bool IsValid();
}

public class A{
     private B B{get;set;}

     public bool IsValid(){
          return B.IsValid();
     }
}

var result = new A().IsValid();

Is there a problem(according to law) if I access a B’s method from A?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:09:12+00:00Added an answer on June 16, 2026 at 5:09 am

    Yes, it applies to properties as well, since the client of this code:

    var isValid = new A().B.IsValid();
    

    is coupled to A and also to B.

    When fixing the law of Demeter violations, you have to balance the need for decoupling and the need to keep responsibilities clearly separated. Sometimes you can create Demeter transmogrifiers: classes that have too many unrelated methods just to comply with the law of Demeter.

    Update: An example of a Demeter transmogrifier can be found in this post:

    Consider, for example, someone who’s trying to kiss up to his boss: sendFlowers(john.getManager().getSpouse()). Applying Hide Delegate here would yield a getManagersSpouse() method in Employee. Yuck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The wikipedia article about Law of Demeter says: The law can be stated simply
The Law of Demeter (really should be the suggestion of Demeter) says that you
According to the Law of Demeter , can you call methods on returned objects?
Law of Demeter expects to make the loosest coupling between classes. This implies that
There is a law of parallel programming but I can't find a link to
The Law of Demeter indicates that you should only speak to objects that you
I would like to obey Law of Demeter. But I would also like to
How can I randomly assign weights from a power-law distribution to a network with
The following code breaks the Law of Demeter : public class Student extends Person
I've been reading up on this Law of Demeter thing, and it (and pure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.