by Eric Lippert via Fabulous Adventures In Coding on 2/1/2010 4:29:00 PM
Which is better style?
bool abc;if (Foo()) abc = Bar();else abc = false;
vs
bool abc = Foo() && Bar();
?
To me, this comes down to the question “is Bar useful solely for obtaining its value, or also for its side effects?” The stylistic choices should typically be driven by a desire to clearly communicate the semantics of the program fragment.
The metasyntatic names are therefore making this harder to answer, not easier. Suppose the choice were in fact between:
bool loginSuccessful;if (NetworkAvailable()) loginSuccessful= LogUserOn();else loginSuccessful= false;
and
bool loginSuccessful= NetworkAvailable() && LogUserOn();
I would always choose the former, because I want LogUserOn to be in a statement of its own. Statements emphasize “I am useful for my side effects”. Statements emphasize control flow and provide convenient places to put breakpoints.
If however the choice were between
bool canUseCloud;if (NetworkAvailable()) canUseCloud = UserHasFreeSpaceInCloud();else canUseCloud = false;
bool canUseCloud = NetworkAvailable() && UserHasFreeSpaceInCloud();
I would always choose the latter; here we’re using && idiomatically to compute a value safely.
Original Post: Style follows semantics
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.