GUI-O Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    What is the most correct code

    Scheduled Pinned Locked Moved
    General Discussion
    2
    3
    198
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Sato
      last edited by

      Hello,
      Anyone know what is the most correct code for the Esp32 and Arduino, since they both work

      if ((gp_bit0) == 1 && (f_pat) == 0) {
      or
      if ((gp_bit1 == 1) && (f_pat == 0)) {

      Thanks
      Regards

      K 1 Reply Last reply Reply Quote 0
      • K
        kl3m3n @Sato
        last edited by kl3m3n

        @Sato Hi.

        You can check out the operator precedence table, e.g.,:
        https://en.cppreference.com/w/cpp/language/operator_precedence

        You can see that the equality operator has higher precedence than logical AND. So, you can omit the inner brackets...

        @Sato said in What is the most correct code:

        if ((gp_bit0) == 1 && (f_pat) == 0)

        This does not make sense and decreases readability (at least for me).

        @Sato said in What is the most correct code:

        if ((gp_bit1 == 1) && (f_pat == 0))

        I usually follow this logic as this exactly shows your intention. Although I would rather use:

        if (gp_bit1 && !f_pat)
        

        since you are basically using boolean values. If you are using C, you can use use defines for "true" and "false":

        typedef int bool;
        #define true 1
        #define false 0
        
        bool gp_bit1 = true, f_pat = false;
        
        if ((gp_bit1 == true) && (f_pat == false))
        

        C++ supports boolean values.

        Regards,
        kl3m3n

        S 1 Reply Last reply Reply Quote 0
        • S
          Sato @kl3m3n
          last edited by

          @kl3m3n,

          Thank you

          Best regards

          1 Reply Last reply Reply Quote 0
          • First post
            Last post