3 ms·
I once had to maintain a chat app in which the first developer had written something like the following at some point: if (statement = ref) { print(statement
by bryanrasmussen 8y ago
I once had to maintain a chat app in which the first developer had written something like the following at some point:
if (statement = ref) {
print(statement) } else {
print('have you done everything right?')
}
was I wrong to be confused as to what he might have intended with statement = ref? Because he was in fact using the language functionalities to express what he wanted. I just thought it looked confusing and misleading.
- masklinn 8y agoFWIW modern C compilers recommend double-bracing exactly for that reason e.g. in clang, by default, it triggers: test.c:3:17: warning: using the result of an assignment as a condition without parentheses [-Wparentheses] if (statement = ref) { ~~~~~~~~~~^~~~~ test.c:3:17: note: place parentheses around the assignment to silence this warning if (statement = ref) { ^ ( ) test.c:3:17: note: use '==' to turn this assignment into an equality comparison if (statement = ref) { ^ ==