• auf@lemmy.ml
    link
    fedilink
    arrow-up
    5
    ·
    9 months ago

    Thief way is actually the best among all of these imo, in terms of readability and efficiency.

            • el_abuelo@lemmy.ml
              link
              fedilink
              arrow-up
              1
              ·
              9 months ago

              Something has gone horribly wrong if you’re trying to do such optimisations when you’ve already chosen JavaScript…let alone Electron.

                • el_abuelo@lemmy.ml
                  link
                  fedilink
                  arrow-up
                  2
                  ·
                  9 months ago

                  Thankfully the only interaction I have with teams is when a supplier arranges the call. Once every two weeks. It grosses me out every time…and that’s the Web app.

                  Do you really think they have done such optimisation efforts as minimising function calls? I can’t imagine it’s required for what is actually a fairly simple frontend app. The complexity is the enabling stack on the backend.

        • Demonen@lemmy.ml
          link
          fedilink
          English
          arrow-up
          1
          ·
          9 months ago

          If you’re optimizing that hard you should probably sort the data first anyway, but yeah, sometimes it’s absolutely called for. Not that I’ve actually needed that in my professional career, but then again I’ve never worked close enough to metal for it to actually matter.

          That said, all of these are implemented as functions, so they’re already costing the function call anyway…

    • snowe@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      9 months ago

      They’re setting a variable to a function. Just use the original function. All thief does is obfuscate for literally no gain except character count.

  • davel@lemmy.ml
    link
    fedilink
    English
    arrow-up
    5
    ·
    9 months ago

    Thief. Writing code is for chumps, and the more code you right, the more of a chump you are.

    • tiredofsametab@kbin.social
      link
      fedilink
      arrow-up
      1
      ·
      9 months ago

      Writing code is for chumps, and the more code you right, the more of a chump you are.

      So you’re the one in there wronging up my code?

  • Leo Uino@lemmy.sdf.org
    link
    fedilink
    arrow-up
    5
    ·
    9 months ago

    TDD

    const max12 = (x, y) => {
        if (x === 1 && y === 2) {
            return 2;
        } else if (x === 7 && y === 4) {
            return 7;
        } else {
            return x;
        }
    };
    
  • kewko@lemdro.id
    link
    fedilink
    English
    arrow-up
    4
    ·
    9 months ago

    Mathematician 2 kinda blew my mind, kinda obvious, just can’t believe I was never taught or thought about it.

    • Artyom@lemm.ee
      link
      fedilink
      arrow-up
      1
      ·
      9 months ago

      I’ve been staring at it for 10 minutes and I’m still not convinced it works.

      • uberrice@feddit.de
        link
        fedilink
        arrow-up
        2
        ·
        9 months ago

        Simple, really. Abs(x-y) is the difference between the two numbers, absolute, so positive value. So, adding abs(x-y) to the smaller of the two numbers turns it into the bigger number. Plus the bigger number, now you have 2 times the bigger number

  • GTG3000@programming.dev
    link
    fedilink
    Русский
    arrow-up
    1
    ·
    9 months ago

    Why use const max = (x, y) => x > y ? x : y instead of function max(x, y) { return x > y ? x : y } ?

  • Eufalconimorph@discuss.tchncs.de
    link
    fedilink
    arrow-up
    1
    ·
    9 months ago
    #define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })
    

    GNU C. Also works with Clang. Avoids evaluating the arguments multiple times. The optimizer will convert the branch into a conditional move, if it doesn’t I’d replace the ternary with the “bit hacker 2” version.

  • bstix@feddit.dk
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    9 months ago

    And then your customer changes their mind. Instead of two numbers, they will now input three numbers. How easy will it be for you to change your code?

    And then the customer changes their mind. Instead of three numbers, they will now input any series of numbers. How easy will it be for you to change your code? And why didn’t you already do this is the previous step?

    And then the customer changes their mind. Instead of any set of numbers, they will now input numbers and text. How easy will it be to change your code?

    And then the customer changes their mind. They now have no idea of what they’re sending you or if they’re even sending you anything. Nevermind the code now, you already did that in the previous step, right? How easy will it be to explain what you’re invoicing them for?

    • neeeeDanke@feddit.de
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      9 months ago

      so 0.3 ~= 1-ln(2)=max(1-ln(2),1-ln(2)) = floor(ln(2*e^(1-ln(2)))) = floor(ln(2)+(1-ln(2))) = 1 ?

      That would bee engeneer 2, not Mathematician3 xD.

      Just out of curiostity, what was you Idea behind that?

      • driving_crooner@lemmy.eco.br
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        9 months ago

        Guess only work with integers, specially for the floor function that is going to give you an integer at the end everytime.

        Not my idea, learned it somewhere while doing college in an statistics class. The idea is that the exponential function grow really fast, so small difference on variables become extreme difference on the exponential, then the log function reverse the exponential, but because it grew more for the biggest variable it reverts to the max variable making the other variables the decimal part (this is why you need the floor function). I think is cool because works for any number of variables, unlike mathematician 2 who only work for 2 variables (maybe it can be generalized for more variables but I don’t think can be done).

        For a min fuction it can be use ceiling(-ln(e^-x + e^-y))

  • wethegreenpeople@sopuli.xyz
    link
    fedilink
    arrow-up
    0
    ·
    9 months ago

    Engineer I guess… Thief is the objectively better enterprise programmer option but I don’t know why I always forget about it and just write a ternary ¯⁠\⁠_⁠(⁠ツ⁠)⁠_⁠/⁠¯

    • mindbleach@sh.itjust.works
      link
      fedilink
      arrow-up
      1
      ·
      9 months ago

      JS instills distrust. Rely on built-in sort? It’s alphanumeric. Rely on built-in reverse? It modifies your array. Copy an array? No you didn’t.

      Yeah no kidding we bang out a kata in a dozen characters. I don’t even believe in order of operations at this point. I’ve been routinely betrayed.