5

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";
9
  • Perhaps you could add the mystring = condition? othervalue : defaultvalue; for a third way of expressing the situation.
    – BlackJack
    Aug 23, 2011 at 12:16
  • 3
    Seems like this question is not constructive and definitely subjective.
    – DeadMG
    Aug 23, 2011 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, 2011 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, 2011 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, 2011 at 12:57

8 Answers 8

9

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.

3
  • 6
    +1 for braces, always annoys me seeing code without them Aug 23, 2011 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, 2011 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, 2011 at 12:25
7

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.

3
  • 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, 2011 at 12:27
  • I also have readability concerns about the conditional operator, but being able to easily make it immutable sure is nice! Aug 23, 2011 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. Aug 23, 2011 at 13:48
5

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.

3

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).

3

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

more intuitive for me:)

1

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.

0
1

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.

2
  • 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, 2011 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. Aug 23, 2011 at 14:07
0

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.

2
  • 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). Aug 23, 2011 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. Aug 23, 2011 at 14:18

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