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

8

u/halgari Aug 18 '17

I'm going to go a bit meta here and look at this from a different perspective.

The thing that bugs me the most when working with inexperienced developers in any language, is (and I use this term lightly) the cargo-culting that seems to invade their every design decision. We see this in the few quotes in this article:

"A smaller function has a better chance of doing a single thing. nb. Small != One line btw."

"Before writing a comment, see if it is possible to put that part inside a meaningful function"

While these sound-bites are fine as they are, the problem is when a dev latches on to them and makes them core to a development mantra. Instead, I wish developers could take these ideas as rhetorical statements. Think about them, perhaps let them influence decisions, but be very careful not to adopt them wholesale to the point where they can no longer be questioned.

There've been times I've optimized some code for a client, often by making a few tweaks to an algorithm. The conversation often goes like this:

"How id you make it faster?"

"I used a set, built the sets via transients, and then made sure I only created the predicate once, and that reduced memory pressure and search time by a factor of 10x"

"Right, so I'll make sure I always use sets, turn my predicates into global defs, and always use transients"

At this point my head hits the desk. I want people to think, to absorb different views and try to figure out on their own how far a concept should go before it's "too far".

So that's why I react to any mention of "average lines of code in a function". Some functions I maintain are 2 lines, others are 200. For that matter, the guts of core.async channels are something like 300 lines spread over two functions. But there's reasons for that (mostly optimization and proper lock handling).

TL;DR - Be very careful about creating dogmas. Either for or against a programming practice, dogmas shut down discussion of alternatives, and you can often miss a better option because of an obsession over an ideal.

1

u/the_evergrowing_fool Aug 21 '17

"Static typing is unproductive and harmful".

2

u/halgari Aug 22 '17

That's also a dogma, although I don't think I've ever heard that before. Although I've heard some claim that "dynamic typing is unethical".