44 lines
1.2 KiB
Org Mode
44 lines
1.2 KiB
Org Mode
|
#+TITLE: Why no defmethod?
|
||
|
#+Author: Yann Esposito
|
||
|
#+Date: [2020-06-12]
|
||
|
|
||
|
tags :: [[file:2020-05-26--06-16-14Z--clojure.org][clojure]] [[file:2020-05-26--06-13-41Z--functional_programming.org][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:
|
||
|
|
||
|
#+begin_src clojure
|
||
|
(ns foo.bar
|
||
|
(:require [baz]))
|
||
|
|
||
|
...300 lines of code...
|
||
|
|
||
|
(foo :x) => "NICE X"
|
||
|
#+end_src
|
||
|
|
||
|
And after a few weeks someone add a require:
|
||
|
|
||
|
#+begin_src clojure
|
||
|
(ns foo.bar
|
||
|
(:require [[baz]
|
||
|
[pownd.core]]))
|
||
|
|
||
|
...300 lines of code...
|
||
|
|
||
|
(foo :x) => "PWND!!!!"
|
||
|
#+end_src
|
||
|
|
||
|
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.
|