No. Using a getter doesn't guarantee that you're violating the Law of Demeter. AKA the Principle of Least Knowledge. As proof I'll finish your code example:
public class Conference {
public boolean isRegistered(Attendance attendance) {
PersonInfo personInfo = attendance.getPersonInfo();
return (personInfo != null);
}
}
Since this calls no methods on PersonInfo
it doesn't violate even the strictest interpretation of LoD. LoD is not a prohibition against accessing data or using getters.
So hooray! You can keep using collections! (They use getters). I'm no fan of over use of getters but LoD is a poor argument against them. Argue real encapsulation instead. And no, public fields aren't any better. In C# they aren't even different.
But seriously you should be aware that LoD is warning you against doing some things that can cause subtle non-obvious problems.
However, there is a structural way of looking at LoD that I do not promote. Lavi is arguing it well but this way of enforcing LoD is as brainless as a Linter. I draw your attention to the very next wikipedia paragraph.
In particular, an object should avoid invoking methods of an 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.m().n() breaks the law where a.m() does not. As an analogy, when one wants a dog to walk, one does not command the dog's legs to walk directly; instead one commands the dog which then commands its own legs.
Wikipdedia - Law of Demeter - In Object Oriented Programming
Sorry but, "use only one dot", is just wrong. That isn't what this is about. The Law of Demeter Is Not A Dot Counting Exercise. This is about where you end up after that dot. Sorry but Wikipedia got this wrong. As proof I offer Java 8 streams:
int sum = widgets
.stream()
.filter(b -> b.getColor() == RED)
.mapToInt(b -> b.getWeight())
.sum()
;
I mean, that's a lot of dots. What gives? These aren't strangers is what. Each dot is taking you to classes that were meant to work together, were deployed together, and will only change together. These are all friends. Friends are ok. Random friends of friends are not.
LoD is telling you that if you just randomly walk a codebase that (oh my gosh) uses getters, there's a good chance you're going to stich together things that never promised you that they would only change together. That's what's so subtle about this. Your Linter doesn't know what's likely to change. So you don't get any meaningful warnings or errors until something changes. If you've been doing a lot of stitching, that can be bad.
There simply isn't a structural analysis that will judge this for you. You need to understand this. You need to know where the tectonic plates are in your codebase and stay way from the fault lines. Because someday, this stuff will move.
if I don't write getter, I won't go hell at the of this so confusing existence
.personInfo
object, and why you want to return it fromisRegistered
, and can you makeattendance
do that work instead. Perhaps you'd have to preconfigure theattendance
object somehow in order to achieve that. Perhaps you'd have to reconceptualize and redesign things that interact with that class. That said, even in an OO system, there can be procedural bits. Sometimes an object can just be a data bag. 2/2