How Long Should a Function Be? (And Why It’s the Wrong Question to Ask) (adamtornhill.substack.com)
from codeinabox@programming.dev to programming@programming.dev on 29 Apr 14:35
https://programming.dev/post/49585213

#programming

threaded - newest

argv_minus_one@mastodon.sdf.org on 29 Apr 16:34 next collapse

@codeinabox

A function should be short enough that you can read and understand it.

Unless you're using a language in which each function declaration has a performance or memory-usage penalty. Not an issue if your language compiles to machine code or WebAssembly, but interpreted languages like JavaScript do have such a penalty. In these cases, you may need to make your functions longer to avoid that penalty.

#programming

HaraldKi@nrw.social on 29 Apr 17:00 next collapse

@argv_minus_one @codeinabox

This is utter nonsense, except you proof with serious performance measurements that
- the extra function is slower
- this actually matters

argv_minus_one@mastodon.sdf.org on 29 Apr 17:03 collapse

@HaraldKi

I am admittedly a bit…emotional about not wasting memory. Growing up on a 486 with 4MB of RAM does that to you, I guess.

The extra function will only be slower if the compiler/interpreter doesn't inline it, which most compilers/interpreters including JavaScript will, so it's mostly just a memory-usage issue. But I have used rather simple interpreters that *don't* inline functions, and one of them even came with a warning that function calls are slow!

@codeinabox

HaraldKi@nrw.social on 29 Apr 17:24 collapse

@argv_minus_one @codeinabox

Well, well, with JavaScript I think browser snd bloated web sites, where a good hand crafted extra function is the smallest if our problems.🤣

eager_eagle@lemmy.world on 29 Apr 17:06 collapse

this sounds like a pretty bad reason to justify ugly code today

any readability gain will greatly outweight resources in most situations

argv_minus_one@mastodon.sdf.org on 29 Apr 17:10 collapse

@eager_eagle

That might have been a reasonable statement 3 years ago, but today there is a global crisis caused by extremely high RAM prices. Optimize your blasted code.

eager_eagle@lemmy.world on 29 Apr 17:45 collapse

Agreed, optimize it. Where it matters. Reducing the number of functions to save space on the stack when the heap has 99% of the data is nonsense.

argv_minus_one@mastodon.sdf.org on 29 Apr 17:51 collapse

@eager_eagle

I'm talking about the *code* wasting memory. In JavaScript each function is a heap object and its source code is another heap object. Even if a JIT compiler inlines them, the original non-inlined functions keep sitting there wasting perfectly good bytes.

eager_eagle@lemmy.world on 29 Apr 17:55 collapse

and again, you end up sacrificing readability to address what, a fraction of a percent in memory use? If that matters in your program, maybe don’t use JS.

thingsiplay@lemmy.ml on 29 Apr 17:32 next collapse

42 or 69

Depends on the function. Scientific functions should be 42 long, and for a couple of functions it should be 69. I don’t make the rules here.

TehPers@beehaw.org on 29 Apr 23:59 collapse

I’ve heard the younger generation tends to prefer 21 or 67 long functions instead.

AnalogHole@lemmy.blahaj.zone on 30 Apr 02:36 collapse

Sufficiently DRY. Beyond that its pedantry.