MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/22lj4a/theo_de_raadt_openssl_has_exploit_mitigation/cgoloae/?context=3
r/programming • u/[deleted]
[deleted] — view deleted post
661 comments sorted by
View all comments
Show parent comment
1
I don't code professionally so perhaps it's just never personally running into a case where it's useful... Why would anybody ever want to perform an assignment inside an if block?
Is there still a flag to trigger a warning for your "okay" case?
4 u/[deleted] Apr 10 '14 You can toggle a switch and act on it at the same time. I actually like this one (*hides*).if ((toggle = !toggle)) { ... } else { ... } 1 u/wescotte Apr 10 '14 I kinda like that too actually but I'd probably find it more useful inwhile ( (toggle = !toggle) ) { ... }since your else clause can never actually execute. 3 u/[deleted] Apr 10 '14 The else clause is called when toggle is originally true. :)To make it more clear here is a real world example.main() { bool isOn = false; while(1) { if ((isOn = !isOn)) { // Turn the light on. } else { // Turn the light off. } sleep(1); } }This is the hello world program for embedded things with LEDs. 2 u/knome Apr 10 '14 since your else clause can never actually execute.uhm...#include <stdio.h> int main( int argc, char ** argv ){ unsigned int toggle = 1 ; if( (toggle = !toggle) ){ printf( "does not print\n" ); } else { printf( "does\n" ); } return 0; }...$ gcc toggle.c $ ./a.out does 1 u/wescotte Apr 10 '14 Oh duh.. reduces to if (toggle) which evaluates to false. For some reason I was thinking leftHandSide = rightHandSide always reduces to true. 2 u/[deleted] Apr 10 '14 I use it to avoid null reference exceptions but still be able to test the result for a different definition of empty:object ack = GetWhatever();string foo;if (ack == null || (foo = ParseString(ack)) == "") { /*handle multiple definitions of empty e.g. for user input validation */ } 1 u/BonzaiThePenguin Apr 10 '14 Being able to do it is just a side effect of assignments also returning the value of the assignment, which allows code such as this:a = b = c = d;Outside of that, I've only ever seen it used within other constructs as convenient shorthand; never as something that had to be done that way.
4
You can toggle a switch and act on it at the same time. I actually like this one (*hides*).
if ((toggle = !toggle)) { ... } else { ... }
1 u/wescotte Apr 10 '14 I kinda like that too actually but I'd probably find it more useful inwhile ( (toggle = !toggle) ) { ... }since your else clause can never actually execute. 3 u/[deleted] Apr 10 '14 The else clause is called when toggle is originally true. :)To make it more clear here is a real world example.main() { bool isOn = false; while(1) { if ((isOn = !isOn)) { // Turn the light on. } else { // Turn the light off. } sleep(1); } }This is the hello world program for embedded things with LEDs. 2 u/knome Apr 10 '14 since your else clause can never actually execute.uhm...#include <stdio.h> int main( int argc, char ** argv ){ unsigned int toggle = 1 ; if( (toggle = !toggle) ){ printf( "does not print\n" ); } else { printf( "does\n" ); } return 0; }...$ gcc toggle.c $ ./a.out does 1 u/wescotte Apr 10 '14 Oh duh.. reduces to if (toggle) which evaluates to false. For some reason I was thinking leftHandSide = rightHandSide always reduces to true.
I kinda like that too actually but I'd probably find it more useful in
while ( (toggle = !toggle) ) { ... }
since your else clause can never actually execute.
3 u/[deleted] Apr 10 '14 The else clause is called when toggle is originally true. :)To make it more clear here is a real world example.main() { bool isOn = false; while(1) { if ((isOn = !isOn)) { // Turn the light on. } else { // Turn the light off. } sleep(1); } }This is the hello world program for embedded things with LEDs. 2 u/knome Apr 10 '14 since your else clause can never actually execute.uhm...#include <stdio.h> int main( int argc, char ** argv ){ unsigned int toggle = 1 ; if( (toggle = !toggle) ){ printf( "does not print\n" ); } else { printf( "does\n" ); } return 0; }...$ gcc toggle.c $ ./a.out does 1 u/wescotte Apr 10 '14 Oh duh.. reduces to if (toggle) which evaluates to false. For some reason I was thinking leftHandSide = rightHandSide always reduces to true.
3
The else clause is called when toggle is originally true. :)
toggle
To make it more clear here is a real world example.
main() { bool isOn = false; while(1) { if ((isOn = !isOn)) { // Turn the light on. } else { // Turn the light off. } sleep(1); } }
This is the hello world program for embedded things with LEDs.
2
uhm...
#include <stdio.h> int main( int argc, char ** argv ){ unsigned int toggle = 1 ; if( (toggle = !toggle) ){ printf( "does not print\n" ); } else { printf( "does\n" ); } return 0; }
...
$ gcc toggle.c $ ./a.out does
1 u/wescotte Apr 10 '14 Oh duh.. reduces to if (toggle) which evaluates to false. For some reason I was thinking leftHandSide = rightHandSide always reduces to true.
Oh duh.. reduces to if (toggle) which evaluates to false. For some reason I was thinking leftHandSide = rightHandSide always reduces to true.
I use it to avoid null reference exceptions but still be able to test the result for a different definition of empty:
object ack = GetWhatever();
string foo;
if (ack == null || (foo = ParseString(ack)) == "") { /*handle multiple definitions of empty e.g. for user input validation */ }
Being able to do it is just a side effect of assignments also returning the value of the assignment, which allows code such as this:
a = b = c = d;
Outside of that, I've only ever seen it used within other constructs as convenient shorthand; never as something that had to be done that way.
View all comments
Show parent comment
1
u/wescotte Apr 10 '14
I don't code professionally so perhaps it's just never personally running into a case where it's useful... Why would anybody ever want to perform an assignment inside an if block?
Is there still a flag to trigger a warning for your "okay" case?