Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
codeinabox ( codeinabox@programming.dev )  to Programming@programming.devEnglish · 5 months ago

How Long Should a Function Be? (And Why It’s the Wrong Question to Ask)

adamtornhill.substack.com

external-link
message-square
7
link
fedilink
15
external-link

How Long Should a Function Be? (And Why It’s the Wrong Question to Ask)

adamtornhill.substack.com

codeinabox ( codeinabox@programming.dev )  to Programming@programming.devEnglish · 5 months ago
message-square
7
link
fedilink
Function length is the wrong focus. What matters is how clearly your code communicates intent.
alert-triangle
You must log in or # to comment.
  • thingsiplay ( thingsiplay@lemmy.ml ) 
    link
    fedilink
    arrow-up
    7
    ·
    5 months ago

    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 ( TehPers@beehaw.org ) 
      link
      fedilink
      English
      arrow-up
      5
      ·
      5 months ago

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

  • ARGVMI~1.PIF ( argv_minus_one@mastodon.sdf.org ) 
    link
    fedilink
    arrow-up
    5
    ·
    5 months ago

    @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

    • Harald ( HaraldKi@nrw.social ) 
      link
      fedilink
      arrow-up
      4
      ·
      5 months ago

      @argv_minus_one @codeinabox

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

      • ARGVMI~1.PIF ( argv_minus_one@mastodon.sdf.org ) 
        link
        fedilink
        arrow-up
        3
        ·
        5 months ago

        @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

        • Harald ( HaraldKi@nrw.social ) 
          link
          fedilink
          arrow-up
          2
          ·
          5 months ago

          @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.🤣

  • AnalogHole ( AnalogHole@lemmy.blahaj.zone ) 
    link
    fedilink
    arrow-up
    2
    ·
    5 months ago

    Sufficiently DRY. Beyond that its pedantry.

Programming@programming.dev

programming@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programming@programming.dev

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 78 users / day
  • 624 users / week
  • 1.75K users / month
  • 4.28K users / 6 months
  • 449 local subscribers
  • 28.6K subscribers
  • 2.91K Posts
  • 22.8K Comments
  • Modlog
  • mods:
  • snowe ( snowe@programming.dev ) 
  • Ategon ( Ategon@programming.dev ) 
  • UlrikHD ( UlrikHD@programming.dev ) 
  • bugsmith ( bugsmith@programming.dev ) 
  • Spyro ( Spyro@programming.dev ) 
  • BE: 0.19.15
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code