What is the most correct code
-
Hello,
Anyone know what is the most correct code for the Esp32 and Arduino, since they both workif ((gp_bit0) == 1 && (f_pat) == 0) {
or
if ((gp_bit1 == 1) && (f_pat == 0)) {Thanks
Regards -
@Sato Hi.
You can check out the operator precedence table, e.g.,:
https://en.cppreference.com/w/cpp/language/operator_precedenceYou 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 -