[ACCEPTED]-var1 = var2 = true; Advantages/Disadvantages?-c#

Accepted answer
Score: 11

It's a readability fail.
It really doesn't 1 cost anything more to say

var1 = true;
var2 = true;

or

var2 = true;
var1 = var2;
Score: 4

There aren't any compile or execution implications 4 but I personally wouldn't do this.

Using 3 two separate assignment statements is much 2 more readable/maintainable and will compile 1 to exactly the same IL as the single statement.

bool var1 = true;
bool var2 = true;

// versus

bool var2;
bool var1 = var2 = true;

// or

bool var1 = true, var2 = true;
Score: 4

I think I'm the only one that likes it that 14 way... maybe not for two variables, but 13 if you have like 5, and you're just initializing 12 them, why not? Makes it easier to change 11 them all at once. These suggestions about 10 var1=true; var2=var1; take a little more thinking to understand. To 9 figure out what var2 is actually initialized 8 to, you have to look at var1, and then see what 7 was assigned to that. I think having them 6 all on one line is quite clear.

If you're 5 doing anything other than initializing them 4 though (performing some logic that does 3 something meaningful) then it's better to 2 be very explicit about what you're doing 1 (have them on different lines).

Score: 2

It is easier to parse by a human if you 7 write it as:

bool var1 = true;
bool var2 = true;

but what is more important in 6 my view is that this is ambiguous against 5 the mistake of needing a comparison operator 4 instead of an assignment operator. Better 3 to be explicit about it. The following 2 2 blocks have opposite results for the value 1 of var1:

bool var2;
bool var1 = var2 = false;

versus

bool var2;
bool var1 = var2 == false;
Score: 1

I recently used the ability to chain assignments 15 like that in code golf, where the objective 14 is to have as few characters as possible.

Before 13 that I don't think that I have ever used 12 it in C#, as it makes the code less readable. To 11 understand the statement you have to read 10 it from right to left, and that is not how 9 you naturally read the code.

As Anders Rune 8 Jensen mentions, you might want to emphasise 7 that the values in the variables should 6 be the same, but then you can just as well 5 do that in two statements to make it clearer:

var1 = true;
var2 = var1;

If 4 the actual generated code differs anything 3 at all, the performance difference is none 2 at all or too small to measure in normal 1 circumstances.

Score: 1

I'd go for readability every time, but precisely 18 what is most readable is a matter of opinion 17 and depends on context. For two variables 16 as quoted I'd go with separate lines as 15 in

var1 = true;
var2 = true;

But on the other 14 hand for more variables I'd usually go with 13 a longer assignment to save screen space. For 12 example coding graphics it's really common 11 to have simple x,y,s and t variables for 10 coordinates. In which case I'd not hesitate 9 in prefering

x = y = s = t = 0;

as the loss 8 of 3 lines of screen space would impact 7 readability. Frankly although simplicity 6 is preferred if you can't grok the above 5 construct what are you doing coding a C 4 based syntax in the first place as the other 3 great space saver, the ternary construct, will 2 completely defeat you.

a = (x==y) ? value1 1 : value2;

Score: 1

In a similar vein, consider:

var var1, var2;
var1 = var2 = new int[3];
var1 = {1, 1, 1};
var2 = {2, 2, 2};

var1 and var2 5 now point to the same reference {2, 2, 2}.

When 4 var1 and var2 are reference types such as 3 an array, remember that even when the elements 2 of the array are values types, such as int, the 1 array is still a reference type.

Score: 0

It just fits stuff into one line instead 4 of multiple lines. And also makes it clear 3 that you actually intended var1 and var2 2 to have the same value.

var1 = true;
var2 = true;

Could look like a 1 c/p bug ;-)

Score: 0

This notation is very useful for "Code-golf" :)

// 34 symbols
bool var1 = true;
bool var2 = true;

// versus 27 symbols
bool var2;
bool var1=var2=1;

// vs 22 symbols
bool var1,var2=var1=1;

0

Score: 0

An interesting set of responses, thanks. I'd 11 thought I'd sum up (Is this allowed?)

I think 10 then that perhaps it is acceptable, though 9 not the preference of many, to use multiple 8 assignment when declaring initial values 7 of simple variables. But most agree that 6 readability is far more important; their 5 preference is separate statements. As is 4 mine.

I did a further Google on this just 3 now, and found that someone had tested "Performance and Multiple Assigment in C#" back 2 in .Net 1.1. It seems there is a very small 1 hit. Take a look.

More Related questions