• monomon@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    18 hours ago

    Yes and no. I find short lambdas easier to read. In lisp you have flet, labels, which allows you to name the elements without introducing external functions.

    But of course there are cases where the proposed is useful, e.g. for reuse. I can imagine applyModifiers could be called in more than one place.

    Anyway, upvoted because it’s an interesting discussion.

  • folekaule@lemmy.world
    link
    fedilink
    arrow-up
    22
    ·
    3 days ago

    I once, many years ago, worked with a programmer who had a strict rule of not introducing any functions unless the block of code was used at least twice.

    Every code base they were the main developer on had source files thousands of lines long with if-statements nested to the 9 levels of hell.

    We worked mostly in Perl and Deplhi (Pascal). Reading old code was… interesting. It’s wild when your Perl code is more readable than your Pascal.

    I’m happy to say I don’t work with them anymore.

    So yeah, break things into functions and name your functions and variables sensibly. Your future self will thank you.

    • NotSteve_@lemmy.ca
      link
      fedilink
      arrow-up
      8
      ·
      2 days ago

      That sounds like the evil mirror of my one former professor who limited function length to 8 lines of code in assignments

    • theherk@lemmy.world
      link
      fedilink
      arrow-up
      12
      ·
      3 days ago

      Not making functions unnecessarily is a pretty good rule, but like all rules, it can be ignored for good reasons. Strict adherence to any rule (in programming) is fraught.

      • folekaule@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        3 days ago

        Yeah there is some overhead with a function call, but for our use case (low-traffic web and batch jobs) that wasn’t a big deal. And the rest of their code wasn’t exactly tuned for performance either.

        Yes, it was the dogmatic approach to it that was the most infuriating.

        • Feyd@programming.dev
          link
          fedilink
          arrow-up
          4
          ·
          3 days ago

          Yeah there is some overhead with a function call

          Not necessarily, since compilers can inline functions as an optimization

          • folekaule@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            3 days ago

            Yes, but Perl 5 did not, except for constant functions. Either way, the effect of that was outweighed by several orders of magnitude by the rest of the WTFs in that code.

            All that is a bit beside the point I was trying to make, which is write readable code.

            Use your own judgement to find a balance between readability and performance. Use profiling tools to find hotspots where performance is a concern and optimize those. Base your practices on evidence not dogma.

            • Feyd@programming.dev
              link
              fedilink
              arrow-up
              4
              ·
              3 days ago

              Yup just wanted any newbie types scrolling by to not take that as something that is always true, when writing in most compiled languages it is completely up to the compiler.

    • cosmicrose@lemmy.blahaj.zone
      link
      fedilink
      English
      arrow-up
      6
      ·
      3 days ago

      I can agree with this, mostly. More recently I’ve been trying to become aware of how much knowledge I’m encapsulating within functions, preferring to hide more stuff in them rather than less, and avoiding shallow functions that just, say, wrap a single line just passing along the arguments. I’ve realized over my career that I’d prefer a longer function that I can just read straight through rather than jump between a bunch of shallow functions trying to piece things together.

      • Womble@piefed.world
        link
        fedilink
        English
        arrow-up
        5
        ·
        2 days ago

        Honestly, as long as the functions you are creating are pure and reasonably named, I think its pretty much always a win to wrap any coherent operation up into one.

    • rockSlayer@lemmy.blahaj.zone
      link
      fedilink
      English
      arrow-up
      5
      ·
      3 days ago

      I mean, that’s a reasonable rule of thumb, but there are definitely limits. It seems that they chose to follow the rule regardless of practicality. Helper functions are nice and good. Almost as good as recursion. Recursive helper functions are nicer and gooder

      • esa@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        3 days ago

        Yeah, there are some examples out of “clean code” that are absolutely ludicrous on the “tons of tiny functions” end.

        I’d also say letting map/filter and other common operations show is entirely fine.

    • Ardyssian@sh.itjust.works
      link
      fedilink
      arrow-up
      2
      ·
      3 days ago

      I think the at least twice thing comes from Avoiding Hasty Abstractions (AHA) rule / convention, as a counter / caveat to DRY