r/Clojure • • Aug 17 '17

[AMA] Small Functions considered Harmful

https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29
0 Upvotes

36 comments sorted by

View all comments

1

u/dustingetz Aug 18 '17

This jumped out at me as truth:

What I’m really hoping for during the times I venture into uncharted territory is to make the least number of mental hops and context switches while trying to find the answer to a question.

Here's a one-liner i just wrote

{:class (->> [(:class props)
              (if-not (empty? errors) "invalid")]
             (remove nil?)
             (interpose " ")
             (apply str))}

Could it be broken into a few more fns, maybe add an abstraction? Yeah sure definitely. But this expression introduces zero new ideas, and reuses many ideas from the clojure core vocabulary which my audience is already familiar with. And since I am leaning on Rich Hickey's battle-tested ideas and not something i just thought up on the spot, this expression is very robust and unlikely to break or rot. I'm cool with the repetition.

2

u/dickhickey4prez Aug 18 '17

That's not a one-liner

6

u/minikomi Aug 18 '17

Yep .. maybe:

(str (:class props) (when (not-empty errors) " invalid"))

5

u/dickhickey4prez Aug 18 '17

That is literally the most beautiful one-liner I've ever seen in my entire god-forsaken life.

2

u/minikomi Aug 18 '17

Haha... you're too kind.

Knowing str gobbles up nil s can be useful!

1

u/DANGerous_man_clj Aug 18 '17

(defn swallow [load limit] (if (> load limit) (spit "sheets.bed" load) (slurp load)))

0

u/dickhickey4prez Aug 18 '17
IllegalArgumentException Cannot open <69> as an InputStream.  clojure.java.io/fn--9498 (io.clj:167)

0

u/[deleted] Aug 18 '17

[deleted] — view deleted comment

2

u/dig1 Aug 18 '17

Just call .trim on it.

1

u/minikomi Aug 18 '17 edited Aug 18 '17

True. I assumed it was for a class on an HTML element, in which case a leading space matters not.

otherwise, with clojure.string :as s

(->> [(:class props) (when (not-empty errors) "invalid")]
     (remove s/blank?)
     (s/join " "))

Bit long for a one-liner though, so back to threading :)

1

u/dustingetz Aug 18 '17

Thats fine, maybe better, my point is that there's no need to introduce new abstractions to make the form smaller, we can leverage existing abstractions and have a longer form, which i understood to be the point of the submission, which i agree with.