• ARGVMI~1.PIF@mastodon.sdf.org
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    23 hours 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

    • Eager Eagle@lemmy.world
      link
      fedilink
      English
      arrow-up
      4
      ·
      edit-2
      22 hours ago

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

      any readability gain will greatly outweight resources in most situations

        • Eager Eagle@lemmy.world
          link
          fedilink
          English
          arrow-up
          4
          ·
          21 hours ago

          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.

          • ARGVMI~1.PIF@mastodon.sdf.org
            link
            fedilink
            arrow-up
            0
            ·
            21 hours ago

            @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
              link
              fedilink
              English
              arrow-up
              4
              ·
              21 hours ago

              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.

      • ARGVMI~1.PIF@mastodon.sdf.org
        link
        fedilink
        arrow-up
        4
        ·
        22 hours 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