var1 = var2 = true; Advantages/Disadvantages?
Are there any advantages or disadvantages to using multiple assignment in a statement? In the simple example
var1 = var2 = true;
the assigment is right to left (as are all assignments in C#, I believe, and probably Java though I haven't che开发者_开发问答cked the latter). But are there any impacts (compiling, execution or otherwise) in coding this way?
Cheers.
It's a readability fail.
It really doesn't cost anything more to say
var1 = true;
var2 = true;
or
var2 = true;
var1 = var2;
I think I'm the only one that likes it that way... maybe not for two variables, but if you have like 5, and you're just initializing them, why not? Makes it easier to change them all at once. These suggestions about var1=true; var2=var1;
take a little more thinking to understand. To figure out what var2
is actually initialized to, you have to look at var1
, and then see what was assigned to that. I think having them all on one line is quite clear.
If you're doing anything other than initializing them though (performing some logic that does something meaningful) then it's better to be very explicit about what you're doing (have them on different lines).
There aren't any compile or execution implications but I personally wouldn't do this.
Using two separate assignment statements is much more readable/maintainable and will compile 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;
It is easier to parse by a human if you write it as:
bool var1 = true;
bool var2 = true;
but what is more important in my view is that this is ambiguous against the mistake of needing a comparison operator instead of an assignment operator. Better to be explicit about it. The following 2 blocks have opposite results for the value of var1:
bool var2;
bool var1 = var2 = false;
versus
bool var2;
bool var1 = var2 == false;
I recently used the ability to chain assignments like that in code golf, where the objective is to have as few characters as possible.
Before that I don't think that I have ever used it in C#, as it makes the code less readable. To understand the statement you have to read it from right to left, and that is not how you naturally read the code.
As Anders Rune Jensen mentions, you might want to emphasise that the values in the variables should be the same, but then you can just as well do that in two statements to make it clearer:
var1 = true;
var2 = var1;
If the actual generated code differs anything at all, the performance difference is none at all or too small to measure in normal circumstances.
I'd go for readability every time, but precisely what is most readable is a matter of opinion and depends on context. For two variables as quoted I'd go with separate lines as in
var1 = true;
var2 = true;
But on the other hand for more variables I'd usually go with a longer assignment to save screen space. For example coding graphics it's really common to have simple x,y,s and t variables for coordinates. In which case I'd not hesitate in prefering
x = y = s = t = 0;
as the loss of 3 lines of screen space would impact readability. Frankly although simplicity is preferred if you can't grok the above construct what are you doing coding a C based syntax in the first place as the other great space saver, the ternary construct, will completely defeat you.
a = (x==y) ? value1 : value2;
In a similar vein, consider:
var var1, var2;
var1 = var2 = new int[3];
var1 = {1, 1, 1};
var2 = {2, 2, 2};
var1 and var2 now point to the same reference {2, 2, 2}.
When var1 and var2 are reference types such as an array, remember that even when the elements of the array are values types, such as int, the array is still a reference type.
It just fits stuff into one line instead of multiple lines. And also makes it clear that you actually intended var1 and var2 to have the same value.
var1 = true;
var2 = true;
Could look like a c/p bug ;-)
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;
An interesting set of responses, thanks. I'd thought I'd sum up (Is this allowed?)
I think then that perhaps it is acceptable, though not the preference of many, to use multiple assignment when declaring initial values of simple variables. But most agree that readability is far more important; their preference is separate statements. As is mine.
I did a further Google on this just now, and found that someone had tested "Performance and Multiple Assigment in C#" back in .Net 1.1. It seems there is a very small hit. Take a look.
精彩评论