Maintainability of Boolean logic - Is nesting if statements needed?
Posted
by
Vaccano
on Programmers
See other posts from Programmers
or by Vaccano
Published on 2010-10-19T20:03:48Z
Indexed on
2011/01/04
1:59 UTC
Read the original article
Hit count: 308
programming-practices
|software-engineering
|coding-standards
|Maintenance
|legibility
Which of these is better for maintainability?
if (byteArrayVariable != null)
if (byteArrayVariable .Length != 0)
//Do something with byteArrayVariable
OR
if ((byteArrayVariable != null) && (byteArrayVariable.Length != 0))
//Do something with byteArrayVariable
I prefer reading and writing the second, but I recall reading in code complete that doing things like that is bad for maintainability.
This is because you are relying on the language to not evaluate the second part of the if
if the first part is false and not all languages do that. (The second part will throw an exception if evaluated with a null byteArrayVariable
.)
I don't know if that is really something to worry about or not, and I would like general feedback on the question.
Thanks.
© Programmers or respective owner