What was implied but not explicitly mentioned here is that && has higher precedence (at least in most well known languages - there may be exceptions) than ||. Thus a && b || c is equivalent to (a && b) || c but not to a && (b || c).
–
Péter TörökOct 5 '12 at 12:21

27

I think, that !exists || !isDirectory is more "understandable", because, isDirectory cannot be true if !exists. So as a human we will say "if it does not exist or it [exists and it] is not a directory".
–
durosOct 5 '12 at 12:39

6

I prefer !exists || !isDirectory over the last one.
–
Apoorv KhurasiaOct 5 '12 at 12:40

26

Anyone who relies on the relative precedence of '&&', '||', '==', '!=', etc and doesn't make their intention clear by using brackets deserves to be shot. In all languages, something like 'a && b || c' is equivalent to a comment saying that the author probably screwed the entire thing up in their rush to avoid typing a few extra characters.
–
BrendanOct 5 '12 at 16:26

@Christoffer Hammarström brought up an important point about the state of isDirectory being tied to the state of exists. Assuming that they refer to the same reference, and that it's not possible to have a state where the reference doesn't exist and is a directory, the truth table can be written as follows:

+1: this is how you teach someone to fish.
–
Steve EversOct 5 '12 at 22:24

4

The next step is to realise that isDirectory depends on exists. It can't both be a directory and not exist.
–
Christoffer HammarströmOct 6 '12 at 0:12

@ChristofferHammarstrom, Out of context I can't assume that the variables refer to the same thing, but that is a valid point. The result column should be filled with n/a in places where the state is impossible to achieve, and the equation reduced accordingly.
–
zzzzBovOct 6 '12 at 4:24

Well, if the variables refer to two different contexts, then they are too terse and need to be renamed.
–
Christoffer HammarströmOct 6 '12 at 13:22

But building a truth table and evaluating it is NP-complete!
–
Thomas EdingOct 17 '12 at 20:18

+1 for saying something about useful names. But somewhere you will have to reformat the conditional.
–
Apoorv KhurasiaOct 5 '12 at 12:41

4

A less extreme alternative, that still conveys intent, is just to name the condition used: boolean fileNameUnused = !exists || !isDirectory; if (fileNameUnused) { doSomething(); }
–
StevenOct 6 '12 at 20:00