My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Law of Demeter

Nate Sire's photo
Nate Sire
·May 3, 2021·

1 min read

Chaining methods in OOP is considered bad practice.

Blog.post.comment

Why? Because it creates a double dependency.

JS has optional checks with ?

Blog.post?.comment?

Which is syntactic sugar for the null object pattern.

So why is function chains normal practice in Clojure?

(Blog (Post (Comment )))

Because Post doesn't belong to Blog. And same for comment. The functions just need to know how to handle the input and type. At most.... You'd use type based polymorphism. Not class based.

Coding with functions is like using legos. And coding with classes is like spaghetti.