• Ephera@lemmy.ml
    link
    fedilink
    arrow-up
    6
    arrow-down
    7
    ·
    3 hours ago

    Alright, spicy opinion time: I think, the ! operator is dumb.

    It’s yet another symbol with a meaning, which people have to learn.

    And it’s easy to overlook, especially in languages with parentheses in their if-conditions:

    if (!list.isEmpty()) { ... }
    

    I think that just a .not() method on Booleans is cooler:

    if (list.isEmpty().not()) { ... }
    

    You can do this in Rust, which is where I have that idea from: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=dedb71bd84243c78ee0afad0f30b80c3

    • DarkenLM@kbin.earth
      link
      fedilink
      arrow-up
      17
      ·
      3 hours ago

      Mathematics themselves are essentially tons of symbols that people have to learn, tbf.

      I personally never had any trouble looking at the negation operator, and find the .not() postfix cumbersome.

      • Deebster@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        1 minute ago

        My (ISO) keyboards do, under the Esc key. I guess you’re in North America (or Australia) and have an ANSI layout.

      • Ephera@lemmy.ml
        link
        fedilink
        arrow-up
        1
        arrow-down
        1
        ·
        1 hour ago

        Oh, I know where it came from. That’s also why we have all kinds of maths operators in the syntax of virtually any programming language. Because back when we didn’t yet know where this programming thing was going, we just threw in the conventions of maths and theoretical logic.

        What I’m saying is that we have our own conventions now, i.e. objects and methods, so I think, it’s worth reconsidering whether we still want to have these old conventions that are special cases in the syntax.

    • yetAnotherUser@discuss.tchncs.de
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      2 hours ago

      I dislike the .not() method a bit because it inverts the meaning of the previous code snippet you’ve read.

      if (list.isEmpty()...
      

      “Ah, so if the list is empty…”

      if (list.isEmpty().not())
      

      “Goddammit”

      • Ephera@lemmy.ml
        link
        fedilink
        arrow-up
        4
        ·
        1 hour ago

        For me, it’s pretty much the opposite. With the exclamation mark, I’ll see the inversion, then I’ll read the actual condition and try to make sense of it, and then I’ll remember that this thing was supposed to be inverted. Or I won’t remember, that’s unfortunately also a possibility.

        Might just be what we’re used to, though. I have been working in a codebase with mostly .not() for the past year.

    • rockSlayer@lemmy.world
      link
      fedilink
      arrow-up
      10
      ·
      3 hours ago

      It’s ok for symbols to have different meanings in different contexts. If someone is new to the context, they should research or ask about it. People that are familiar should provide the mutual understanding, provided they have the will and ability to educate.