• Eufalconimorph@discuss.tchncs.de
    link
    fedilink
    arrow-up
    1
    ·
    1 year 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
    1 year 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?

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

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

  • Leo Uino@lemmy.sdf.org
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    TDD

    const max12 = (x, y) => {
        if (x === 1 && y === 2) {
            return 2;
        } else if (x === 7 && y === 4) {
            return 7;
        } else {
            return x;
        }
    };
    
  • Fungah@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    Reminded of how truly little I know about programming despite the time have spent doing it

    Ugh. I’ll never be any good.

    • TheHarpyEagle@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      Listen, in industry programming (and for personal projects if you want to get them done), the thief is the way to go. By all means, challenge yourself to understand each of these functions, but 99% of day to day development will not look like this.