regural if / else VS shorthand if / else and type of functions
Hello forum ! i have a question !
Which one do you recommend as "better" ? or it doesn't hurt ?
Code:
private void yes() {
MessageBox.Show("Hah");
}
private void no() {
MessageBox.Show("Hah");
}
if ( x > y ) { yes(); } else { no(); }
OR
Code:
private bool yes() {
MessageBox.Show("Hah");
return true;
}
private bool no() {
MessageBox.Show("Hah");
return true;
}
bool answer = ( x > y ) ? yes() : no();
Thanks !
Re: regural if / else VS shorthand if / else and type of functions
Using the ternary operator (? :) only makes sense if you need to assign a value.
It wouldn't be recommended return fake data from methods just to use the ternary operator.
Re: regural if / else VS shorthand if / else and type of functions