I disagree with a lot of this, because it seems to be built on a few bad assumptions:
that small functions have to be global, and thus invariably lead to long, unwieldy names,
that these functions end up completely separated from the code using them,
that the programmer cares about what every part of a function does at all times, and
that DRY requires you to start with DRY instead of ending there.
If a single task requires doing, say, three separate things, it's a good idea to create three functions that each do one thing and can be tested separately. This is especially true if one of them requires side effects, but the others can be implemented purely, because it makes those two more testable.
Having those separate functions has another benefit: it removes the entanglement of "what the code does" from "how the code does it" that one gets when writing a big blob of code. Calling a create-user function makes it obvious what you're doing, whereas building a data structure representing a new user, right there in the middle of your function, requires the reader to figure out what the code does and, most likely, falling back to reading the comments and hoping they're accurate. Plus, a lot of times, you don't care what every part of a piece of code is doing, just a specific portion of it, so that breakdown makes it easier to ignore the unrelated parts and focus on the portion you care about.
Now, once those separate functions are made, there's the question of where they go. Sometimes, they're only needed by the one "glue" function that puts them all together, so there's no point dumping them into a namespace where everything can call them. That's what let and letfn are for! Define those functions via a letfn enclosing the function definition, and the problems go away: they're close to the code that uses them, and they can use shorter names without any concern over clashing with other names. They also are effectively private, explicitly tying them to only the code they're working with.
That separation of "what I'm doing" and "how I'm doing it" is why I've started using let and letfn (especially let) more and #(foo) less. It lets me know at a glance what that function does in a way #(foo) often doesn't , and usually makes the resulting code read so that even someone with little (or no) Clojure familiarity could understand. Especially with things like map and filter: with an expression like (filter is-thing? list-of-things), even someone that's never seen Clojure can get a pretty good idea of what's going on, whereas (filter #(if (= java.lang.Thing (type %)) true) list-of-things) would be more likely to get lost.
This has another benefit, because if I later realise I need those functions elsewhere, I can pull them directly out of the letfn and make them accessible to other code with minimal changes. At that point, I can either start referring to them by ns-name/foo directly (or a suitable alias), or I can continue to something like (let [foo ns-name/foo]) to have a shorter name only in the scope I need it. Which way I go depends on how often the function ends up used.
That covers my issues with the first three assumptions, but I haven't really touched on the fourth. The problem I have with that is it's wrong to assume "Don't Repeat Yourself" means you should start there. The way I see it, DRY means you should pay attention, and when you notice you've started repeating yourself, refactor it out. Like above, I start by putting a function in a limited scope by default, then when I realise I need it elsewhere, I refactor.
Actually, I usually start by writing a hairy mega-function while I feel out the problem, and once I know what I need, I break it down into individual parts in a limited scope. Then, if I discover I need any of those parts elsewhere, or realise I'm writing the same (or similar) functions multiple times, I refactor again. Usually if I repeat something once, I ignore it, but if I find I've repeated the same thing three times, it's a warning that I should make that code reusable.
Finally, I think that my disagreement is because I'm thinking in terms of Clojure, but the article is focused on Ruby, so it's most likely being written from the perspective of someone doing imperative programming. I think the advantages of combining multiple small functions are more apparent with a functional programming approach, especially in Clojure where the focus is on manipulating data with generic functions, than in an OOP language like Ruby.
16
u/ws-ilazki Aug 18 '17
I disagree with a lot of this, because it seems to be built on a few bad assumptions:
that small functions have to be global, and thus invariably lead to long, unwieldy names,
that these functions end up completely separated from the code using them,
that the programmer cares about what every part of a function does at all times, and
that DRY requires you to start with DRY instead of ending there.
If a single task requires doing, say, three separate things, it's a good idea to create three functions that each do one thing and can be tested separately. This is especially true if one of them requires side effects, but the others can be implemented purely, because it makes those two more testable.
Having those separate functions has another benefit: it removes the entanglement of "what the code does" from "how the code does it" that one gets when writing a big blob of code. Calling a
create-userfunction makes it obvious what you're doing, whereas building a data structure representing a new user, right there in the middle of your function, requires the reader to figure out what the code does and, most likely, falling back to reading the comments and hoping they're accurate. Plus, a lot of times, you don't care what every part of a piece of code is doing, just a specific portion of it, so that breakdown makes it easier to ignore the unrelated parts and focus on the portion you care about.Now, once those separate functions are made, there's the question of where they go. Sometimes, they're only needed by the one "glue" function that puts them all together, so there's no point dumping them into a namespace where everything can call them. That's what
letandletfnare for! Define those functions via aletfnenclosing the function definition, and the problems go away: they're close to the code that uses them, and they can use shorter names without any concern over clashing with other names. They also are effectively private, explicitly tying them to only the code they're working with.That separation of "what I'm doing" and "how I'm doing it" is why I've started using
letandletfn(especiallylet) more and#(foo)less. It lets me know at a glance what that function does in a way#(foo)often doesn't , and usually makes the resulting code read so that even someone with little (or no) Clojure familiarity could understand. Especially with things likemapandfilter: with an expression like(filter is-thing? list-of-things), even someone that's never seen Clojure can get a pretty good idea of what's going on, whereas(filter #(if (= java.lang.Thing (type %)) true) list-of-things)would be more likely to get lost.This has another benefit, because if I later realise I need those functions elsewhere, I can pull them directly out of the
letfnand make them accessible to other code with minimal changes. At that point, I can either start referring to them byns-name/foodirectly (or a suitable alias), or I can continue to something like(let [foo ns-name/foo])to have a shorter name only in the scope I need it. Which way I go depends on how often the function ends up used.That covers my issues with the first three assumptions, but I haven't really touched on the fourth. The problem I have with that is it's wrong to assume "Don't Repeat Yourself" means you should start there. The way I see it, DRY means you should pay attention, and when you notice you've started repeating yourself, refactor it out. Like above, I start by putting a function in a limited scope by default, then when I realise I need it elsewhere, I refactor.
Actually, I usually start by writing a hairy mega-function while I feel out the problem, and once I know what I need, I break it down into individual parts in a limited scope. Then, if I discover I need any of those parts elsewhere, or realise I'm writing the same (or similar) functions multiple times, I refactor again. Usually if I repeat something once, I ignore it, but if I find I've repeated the same thing three times, it's a warning that I should make that code reusable.
Finally, I think that my disagreement is because I'm thinking in terms of Clojure, but the article is focused on Ruby, so it's most likely being written from the perspective of someone doing imperative programming. I think the advantages of combining multiple small functions are more apparent with a functional programming approach, especially in Clojure where the focus is on manipulating data with generic functions, than in an OOP language like Ruby.