• Croquette@sh.itjust.works
    link
    fedilink
    arrow-up
    100
    ·
    2 months ago

    I tend to add is to booleans toreally differentiate between a method name and a status.

    def open_file_dialog(self):
        self.dialog_file_is_open = True
        pass
    

    That way, it’s easier for my dumb brain to spot which is which at a glance.

        • sus@programming.dev
          link
          fedilink
          arrow-up
          4
          arrow-down
          2
          ·
          edit-2
          2 months ago

          that works for 2 word names eg is_open or is_file, but in this case is_dialog_file_open is structured like a question, while dialog_file_is_open is structured like a statement

          • Rinox@feddit.it
            link
            fedilink
            arrow-up
            8
            ·
            2 months ago

            Doesn’t matter, the point is that, if it starts with “is” then you automatically know it’s a boolean.

          • redfellow@sopuli.xyz
            link
            fedilink
            arrow-up
            3
            ·
            2 months ago

            It still works. is_this_thing_some_thingy. Is is just a prefix for if the suffix returns true/false.

    • 4wd@programming.dev
      link
      fedilink
      arrow-up
      20
      arrow-down
      2
      ·
      2 months ago

      In Elixir, we mark statuses by using a question mark at the end of the variable name. Something like this:

      authorized? = user |> get_something() |> ensure_authorized?()
      

      I like this better than the is_ prefix

        • Faresh@lemmy.ml
          link
          fedilink
          English
          arrow-up
          8
          ·
          2 months ago

          If it’s like Lisp, then ? is just part of the symbol and doesn’t have any special syntatic meaning. In different Lisps it’s also convention to end predicate names with a ? or with P (p for predicate)

        • FMT99@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          2 months ago

          We do this in Ruby all the time, we just prefer methods over variables, usually.

          def authorized?
            current_user&.authorized?
          end
          
          • cytokine0724@sh.itjust.works
            link
            fedilink
            arrow-up
            2
            ·
            2 months ago

            I’m a principal backend engineer routinely writing Ruby for my day job, so I’m familiar, lol. But you can’t do it for local variables and that just sucks. Definitely a +1 for Elixir.