I have a condition
if(exists && !isDirectory || !exists)
{}
how can I modify it, so that it may be more understandable.
|
I have a condition
how can I modify it, so that it may be more understandable. |
||||
| show 2 more comments |
|
is equivalent. Now because exists is always true in the second part of the
Or you can go a step further and do:
|
|||||||||||||||||||||
|
|
As a process, I suggest building a truth table:
This matches the
If you don't remember all your logic gates, wikipedia has a nice reference with the truth tables to boot. @Christoffer Hammarström brought up an important point about the state of
The With this in mind, However, |
|||||||||||||||||
|
|
For better readability, I like to extract boolean conditions to methods:
Or with a better method name. If you can name this method properly, the reader of your code doesn´t need to figure out what the boolean condition means. |
|||||||
|
|
You could just try to nail the no-go case and bail out if that shows up.
or even
|
|||||||||
|
|
You can use a truth table as pointed out. Second step could be a KV-map for minimizing the number of terms. Using the laws of Boolean algebra is another approach: A = exists && = * [Edit] exists && !isDirectory || !exists exists && !isDirectory || !exists Or with double complement (!!x = x): A*B + !A |
|||||||
|
|
I don't like to use "!" when there is more than one condition in the expression. I'll add lines of code to make it more readable.
|
|||||
|
|
As previously indicated, the condition can be reduced to:
However, I'll bet that being a directory implies existence. If so, we can reduce the condition to:
|
|||
|
|
existsandisDirectoryare both true? – qegal Oct 6 '12 at 2:13