• Ephera
    link
    fedilink
    English
    arrow-up
    2
    arrow-down
    5
    ·
    edit-2
    4 months ago

    2.is_even()

    (I don’t know, if this is possible in JS.)

    • 𝘋𝘪𝘳𝘬
      link
      fedilink
      English
      arrow-up
      4
      ·
      4 months ago

      Let’s call the number variable just x, you then have literal math (Euclidean division) if you ignore === instead of = for equals.

      x % 2 === 0
      

      This can’t get better or more native than “just math”. This is the whole code you need to detect if a number is even. I wouldn’t even call it “code”.

      If you remove whitespaces and ignore the type you end up with x%2==0 which is 6 characters long and a fully valid if clause. No magic involved, no abstraction, no weird function calls on integers …

      I see that in modern JS this type of coding is a trend, but you can’t tell me you want to replace 6 characters with an own module or a package. :)

      • Ephera
        link
        fedilink
        English
        arrow-up
        1
        arrow-down
        2
        ·
        4 months ago

        No, I want that in the std lib. Yes, it would just call x % 2 == 0 underneath. But the advantage is readability. I’m in principle aware that x % 2 == 0 is true when the number is even, but I need it seldomly enough that I do still need to think about it for a second before I know for sure. I don’t need to think about x.is_even(). And the readability is what I want natively, i.e. in the std lib.

        It being in the std lib would also sidestep your concerns about security or the function call having unknown side effects.