Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Which of these styles do people prefer?

string mystring = "defaultvalue";
if (condition)
   mystring = "othervalue";

VS

string mystring = null;
if (condtion)
   mystring = "othervalue";
else
   mystring = "defaultvalue";

VS

mystring = condition? "othervalue" : "defaultvalue";
share|improve this question
    
Perhaps you could add the mystring = condition? othervalue : defaultvalue; for a third way of expressing the situation. –  BlackJack Aug 23 '11 at 12:16
3  
Seems like this question is not constructive and definitely subjective. –  DeadMG Aug 23 '11 at 12:29
1  
Hi Ian1971, subjective questions are allowed in both places, but it's not clear what value is gained by asking this: without any context to your actual problem you're facing, it's entirely a matter of personal preference. Can you revise your question to go into more about why you're asking this and what practical problem you hope to solve? –  user8 Aug 23 '11 at 12:43
2  
This is by no means a subjective question. There're various factors to be considered. The first and the third form are clearly superior to the second form for technical reasons (exception safety in standard scenarios like maintenance). –  Falcon Aug 23 '11 at 12:45
1  
@Ian1971 I'm afraid there isn't going to be a place on Stack Exchange where you can solicit opinions, especially without explaining the practical problem you're actually facing. For more information, check out Real Questions Have Answers. –  user8 Aug 23 '11 at 12:57
show 4 more comments

closed as not constructive by Walter, Mark Trapp Aug 23 '11 at 12:43

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

8 Answers

While people will have preferences, it doesn't really matter when compared to one particular style being followed consistently, all the time.

I would say, both of them need {} braces to prevent code being added incorrectly:

string mystring = "defaultvalue";
if (condition)
   mystring = "othervalue";
   string anotherVariable = "a value that someone could think is part of the conditional statement";

Some people may view this as overkill, but it seems preferable to me take a simple step that prevents easy defects.

share|improve this answer
6  
+1 for braces, always annoys me seeing code without them –  Nicholas Smith Aug 23 '11 at 12:17
2  
It's a valid point though I find I prefer the aesthetics of the code without them if there is only one line in the condition. –  Ian1971 Aug 23 '11 at 12:21
1  
@Ian1971 A lot of people will too, and people who do will likely not have many of these defects. I think there'll be a few +1 for the consistency point/-1 for the braces on this answer :) –  StuperUser Aug 23 '11 at 12:25
add comment

I'd prefer the first style just because it's shorter and still readable. Depending on the language, I think choice 3 is even better:

string mystring = condition ? "othervalue" : "defaultvalue"

or

string mystring = if (condition) "othervalue"
    else "defaultvalue"

because it allows you to make mystring an immutable variable, which has some nice benefits.

share|improve this answer
2  
I do like the conditional operator (?) version, but it can quickly get unreadable if there is more than just simple code in it. For instance if "othervalue" and "defaultvalue" are actually the result of some format string statement it can get quite long. –  Ian1971 Aug 23 '11 at 12:27
    
I also have readability concerns about the conditional operator, but being able to easily make it immutable sure is nice! –  Brian Knoblauch Aug 23 '11 at 12:29
    
In my opinion the conditional operator clearly states what is done, a conditional assignment, without elaborating such a conceptually simple statement on more than one line. –  Christian Rau Aug 23 '11 at 13:48
    
+1 for immutable variables. –  Caleb Sep 14 '11 at 5:27
add comment

The first style is 3 lines compared to 5 lines, and doesn't seem any less clear or readable; it's an easy preference from my point of view.

share|improve this answer
add comment

As several others, I personally also prefer the 1st version. One reason not mentioned so far: it makes clear what the default value (and execution path) is.

But overall, the difference is not dramatic, I can live with the 2nd version too (and I do, as there are lots of them in our legacy app).

share|improve this answer
add comment

mystring = condition? "othervalue" : "defaultvalue";

more intuitive for me:)

share|improve this answer
add comment

I prefer the first style when there is a known default value that will occur unless something else is true. If there's no default, then I will use the second style.

share|improve this answer
add comment

The first form is superior, as it offers a greater form of exception safety. If condition or some other code before the assignment throws, the string won't have the value that you assume it will.

share|improve this answer
    
Full ack. Exception-safety is the primary reason to use the first form. The second form is harder to read and more likely to cause errors during maintenance work. The third form is ok. –  Falcon Aug 23 '11 at 12:42
    
In C++, if condition throws, mystring will have its destructor called, and will no longer exist in any case (unless you put the initialization in its own try block). In another language where mystring stays around, it isn't clear to me that having it have the wrong value is any better than null. –  David Thornley Aug 23 '11 at 14:07
add comment

I don't much like mystring = condition? "othervalue" : "defaultvalue"; but that is just personal preference and perhaps not being used to seeing it (my language is Delphi).

The others are a toss up and I often let it depend on the distance (number of lines) between the variable declaration and the condition. Short: the start with the default value. The more lines there are between the declaration and the condition, the more I will lean towards having a null or empty value in the declaration and a complete if-else statement for assigning the value.

share|improve this answer
    
Why have any lines between variable definition and initialization (unless you're using a language like C89 where variables can't be declared just anywhere). –  David Thornley Aug 23 '11 at 14:09
    
@David: Oh, because I use Delphi (see first paragraph) and because I hate having variable declaration all over the place, and because my methods are usually short enough for that not to be a problem. –  Marjan Venema Aug 23 '11 at 14:18
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.