ELI5: Explain Like I'm 5

Law of Demeter

The law of demeter, also known as the "principle of least knowledge," is like a rule that tells you how to behave with strangers. Imagine that you're at the park playing with your friends, and you see a new kid who you've never met before. You want to talk to them and play with them, but you don't know them very well.

So, you follow the law of demeter by only talking to this new kid about things that are directly related to them, like what games they like to play or if they need help finding something. You don't ask personal questions like where they live or what their parents do for a living, because you don't know them well enough to ask those things.

The law of demeter works the same way with computer programs. When one part of a program needs to talk to another part, it only talks about things that are directly related to that other part. It doesn't ask about things that aren't important, like how the other part was made or what it does when it's not working with the first part.

Following the law of demeter helps to keep computer programs organized and easy to understand. Just like how talking only about important things makes it easier to make friends with someone new.