I personally have no problem with the following code
if (Object foo != null && !String.IsNullOrEmpty(foo["bar"]))
{
// do something
}
Because I think the following is too verbose
if (Object foo != null)
{
if (!String.IsNullOrEmpty(foo["bar"]))
{
// do something
}
}
But I wouldn't go so far with this standpoint if say there were 5 predicates and I had to wrap the text in the editor to see them all at once, is there a logical "line" that you draw as to how many predicates you include in a single if statement in a similar sense to saying that methods should never require more than 7 parameters
I don't think rewriting if-statements in two is the way, especially if you consider that adding an else
clause to your examples gives different results. If I wanted to reduce the number of atomic propositions in the condition, I would factor some that make sense together to a function of their own, like:
if(won_jackpot(obj)) ...
I think it's a balance of the different types of operators and correct formatting. If all of the operators are the same (all "and" or all "or"), then you can probably concatenate together an indefinite number of expressions without losing comprehension:
if (something() &&
something_else() &&
so_on() &&
so_forth() &&
some_more_stuff() &&
yada() &&
yada() &&
yada() &&
newman() &&
soup_nazi() &&
etc())
...
Short term memory has a capacity of seven items, give or take two. This implies that an expression involving more than five dissimilar objects might require one to pause and think about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With