deft/notes/2020-06-12--09-32-26Z--why_no_defmethod.org
Yann Esposito (Yogsototh) d641cd04d9
moved files
2021-09-15 09:12:29 +02:00

1.3 KiB

Why no defmethod?

tags :: clojure functional programming source ::

The main reason is that it is harder to reason about in our environment. defmethod is exactly like a cond or case but potentially distributed between multiple files. So:

(ns foo.bar
  (:require [baz]))

...300 lines of code...

(foo :x) => "NICE X"

And after a few weeks someone add a require:

(ns foo.bar
  (:require [[baz]
             [pownd.core]]))

...300 lines of code...

(foo :x) => "PWND!!!!"

So defmethod is harder to reason about because the logic is distributed. And as a developer it is harder to reason statically about the code.

Still, this could totally be fine for clojure libraries doing "magic" stuff. There is a big difference between the code aimed for a production environment application written by a team and a codebase for a library.

So "never use defmethod" is a way of saying that it is only allowed if you really tried your best not to use it but this does not make sense, and you can show why to the rest of the team.