←back to thread

504 points azhenley | 4 comments | | HN request time: 0.713s | source
Show context
zahlman ◴[] No.45773060[source]
> and it avoids problems where you move a block of code and it silently uses a version of the variable that wasn’t what it originally had.

I find that keeping functions short also helps a ton with that.

No, shorter than that. Short enough that the only meaningful place to "move a block of code" is into another function. Often, by itself.

replies(1): >>45773136 #
1. williamdclt ◴[] No.45773136[source]
It helps with that, but it has other trade-offs: indirection isn't free for readability.
replies(2): >>45773197 #>>45773451 #
2. throw10920 ◴[] No.45773197[source]
I'll go further than that and say that indirection significantly increases cognitive load and hurts readability.
replies(1): >>45774270 #
3. tengbretson ◴[] No.45773451[source]
> indirection isn't free for readability

Yes, but also no. If its a mostly side-effect free function with a good name and well defined input/output types its basically free.

4. zahlman ◴[] No.45774270[source]
I have consistently found the opposite to be the case across decades of programming experience, as regards the extraction of helper functions. This is not "indirection" in the same sense as with data structures. It is abstraction of currently-irrelevant detail.

Reading and understanding code is a process of answering "what are the immediate steps of this task?", without thinking about what those steps consist of or entail. It is not a process of answering "where is the variable representing ..., and the code that manipulates this?", especially since this makes assumptions that may prove incorrect.