Alex Rivera | Logout

"Double" assignment - should it be avoided?

Asked 2010-11-13T11:14:05.110
20

Consider you have some expression like

i = j = 0

supposing this is well-defined in your language of choice. Would it generally be better to split this up into two expressions like

i = 0
j = 0

I see this sometimes in library code. It doesn't seem buy you much in terms of brevity and shouldn't perform any better than the two statements (though that may be compiler dependant). So, is there a reason to use one over the other? Or is it just personal preference? I know this sounds like a silly question but it's bugging me for a long time now :-).

Edit
Report

1 Answer

1

The second way is more readable and clear, I prefer it.

However I try to avoid "double" declaration:

int i, j;

instead of

int i;
int j;

if they're going consecutively. Especially in case of MyVeryLong.AndComplexType

answered 2010-11-13T11:15:59.320

Your Answer