tags:

views:

412

answers:

12
+9  A: 

In theory, I prefer #3 as it avoids having to assign a value to the variable twice. In the real world though I use any of the four above that would be more readable or would express more clearly my intention.

cruizer
A: 

switch statement also works. If it's simple and more than 2 or 3 options, that's what I use.

Jarrett Meyer
+1  A: 

I tend to use #1 alot myself. if condition reads easier than if !condition, especially if you acidentally miss the '!', atleast to my mind atleast.

Most coding I do is in C#, but I still tend to steer clear of the terniary operator, unless I'm working with (mostly) local variables. Lines tend to get long VERY quickly in a ternary operator if you're calling three layers deep into some structure, which quickly decreases the readability again.

Matthew Scharley
+4  A: 

I prefer method 3 because it is more concise and a logical unit. It sets the value only once, it can be moved around as a block, and it's not that error-prone (which happens, esp. in method 1 if setting-to-value1 and checking-and-optionally-setting-to-value2 are separated by other statements)

ΤΖΩΤΖΙΟΥ
A: 

In a situation where the condition might not happen. I would go with 1 or 2. Otherwise its just based on what i want the code to do. (ie. i agree with cruizer)

Ólafur Waage
A: 

I tend to use if not...return.

But that's if you are looking to return a variable. Getting disqualifiers out of the way first tends to make it more readable. It really depends on the context of the statement and also the language. A case statement might work better and be readable most of the time, but performance suffers under VB so a series of if/else statements makes more sense in that specific case.

Joe Chin
A: 

Method 1 or method 3 for me. Method 1 can avoid an extra scope entrance/exit, but method 3 avoids an extra assignment. I'd tend to avoid Method 2 as I try to keep condition logic as simple as possible (in this case, the ! is extraneous as it could be rewritten as method 1 without it) and the same reason applies for method 4.

workmad3
A: 

It depends on what the condition is I'm testing.

If it's an error flag condition then I'll use 1) setting the Error flag to catch the error and then if the condition is successfull clear the error flag. That way there's no chance of missing an error condition.

For everything else I'd use 3)

The NOT logic just adds to confusion when reading the code - well in my head, can't speak for eveyone else :-)

DaveF
A: 

If the variable has a natural default value I would go with #1. If either value is equally (in)appropriate for a default then I would go with #2.

Mark Cidade
+1  A: 

Note: The following examples may be simpler—and thus possibly more readable—but not all languages provide such syntax

This is no argument for not using them in languages that do provide such a syntax. Incidentally, that includes all current mainstream languages after my last count.

and they are not suitable for extending the variable assignment to include more than one statement in the future.

This is true. However, it's often certain that such an extension will absolutely never take place because the condition will always yield one of two possible cases.

In such situations I will always prefer the expression variant over the statement variant because it reduces syntactic clutter and improves expressiveness. In other situations I tend to go with the switch statement mentioned before – if the language allows this usage. If not, fall-back to generic if.

Konrad Rudolph
+2  A: 

3) is the clearest expression of what you want to happen. I think all the others require some extra thinking to determine which value is going to end up in the variable.

In practice, I would use the ternary operator (?:) if I was using a language that supported it. I prefer to write in functional or declarative style over imperative whenever I can.

Kristopher Johnson
A: 

It depends. I like the ternary operators, but sometimes it's clearer if you use an 'if' statement. Which of the four alternatives you choose depends on the context, but I tend to go for whichever makes the code's function clearer, and that varies from situation to situation.

Bart Read