Sign in
Log inSign up

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.