• VeganPizza69 Ⓥ@lemmy.world
    link
    fedilink
    arrow-up
    11
    arrow-down
    5
    ·
    4 months ago

    The high level setter function should be made to handle both string and numeric values.

    If it contains “%” it’s a percentage value.

    If it’s a string without a “%” it’s an absolute value and needs to be normalized.

    If it’s a numeric value, it’s an absolute value.

    If it’s a numeric 100, it’s 100%.

    If it’s a subunitary numeric value, it’s a percentage.

    • sandbox@lemmy.world
      link
      fedilink
      arrow-up
      28
      ·
      4 months ago

      yeah I’m gonna go ahead and reject your PR, please change this function to accept a decimal value between 0 and 1

      • ChickenLadyLovesLife@lemmy.world
        link
        fedilink
        English
        arrow-up
        12
        ·
        4 months ago

        Ironically, the worst thing I ever saw a coworker do was to change a function that accepted an Integer value between 0 and 32767 to one that accepted a Float between 0.0 and 1.0. Perfectly sensible change except that it resulted in a 120 mph knuckleball fired a foot above a 10 year old kid’s head, followed by a fist fight between the client and my boss.

    • Skates
      link
      fedilink
      arrow-up
      4
      ·
      4 months ago

      Oldman.setHealth(“dicktits”); //normalize pls

      Oldman.setHealth(“-100±1%”); //make percentage pls

      Oldman.setHealth(0.0); //it is subunitary, but undefined behavior - will it access the ‘numeric value’ overload, or the ‘subunitary numeric value’ overload?

      Don’t write your own code just yet.

      • VeganPizza69 Ⓥ@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        4 months ago

        Oldman.setHealth(“dicktits”); //normalize pls

        0

        Oldman.setHealth(“-100±1%”); //make percentage pls

        Reject operations.

        Use absolute number to remove the minus. Math.abs()

        Oldman.setHealth(0.0); //it is subunitary, but undefined behavior - will it access the ‘numeric value’ overload, or the ‘subunitary numeric value’ overload?

        Same result either way, so whatever if branch is first.

        Understand the purpose. If you want to kill the old man with 0, then there’s no point to leaving it as 0.9%, understand the non-linear characteristics of life and death.

        When you’re dealing with the low level functions, sure, you can keep it simple. When you’re reaching the surface of user input, you’re either going to waste time with validation and error reporting, or you’re going to waste time with interfaces that can handle more shit without complaining. There’s no fool proof either way, but good luck pissing users off with endless docs.

        Don’t write your own code just yet.

        If your goal in programming is just to be a traffic cop between the user input and the database, all you’re doing is building a virtual bureaucracy, the kind that people really hate and is easily generated with coding tools. Or you’re just deferring the “smoothing out” burden to the UI developers.