2

Why do some developers in C for example chain together assignments of variables like this:

i = j = k = 0;

or sometimes formatted like

i =
j =
k = 0;

Is there some underlying complier optimization here? Do hardware registers overflow into the next loaded variable or something? Or is this syntactic sugar?

  • 6
    I have never seen the second style, and most automatic code formatters would remove that layout. How you format your code does not affect any optimization whatsoever. So always choose the layout that is clearest to human readers. – amon Mar 13 '18 at 17:50
5

Not going to make any difference in generated code for any competently written compiler.

At least when I've done it, I did it because it seemed like a relatively minor activity, and I didn't want it to distract from the rest of the code.

Some mind find one bit of trivia interesting though. In PL/I you could also write code like:

A = B = 0;

Some might find the result of this a bit surprising though. PL/I didn't require use of (for example) = for assignment and == for comparison. Instead, it sort of...guessed what you meant by any particular use of =. So, if you had something like if a = b, it treated that as a comparison, but if you had a = b; it treated that as an assignment.

In the case of A = B = 0; things got just a little strange though: it treated the first = as an assignment, but the second as a comparison. So, it would compare B to 0, and (like C) produce 0 for false, and 1 for true. Then it would assign that integer-representing-Boolean to A. As a result, after executing A = B = 0; we could be certain of one thing: A would not equal B. If B was 0, then A would be 1, and if B was not equal to 0, then A would be 0.

You could, however, override its guess, so A := B := 0; assigned 0 to both A and B.

5

One of the motivation of writing

i = j = k = 0;

for me is, it is more convenient at the case that may need to change the assign value of all variables frequently (e.g.:for testing):

i = j = k = 1;
3

In every language I know, there is no difference between i = j = k = 0; and i = 0; j = 0; k = 0;, so shortest code wins:

  • You type less code.
  • It takes less space.

It seems more readable as well. I would be tempted to read:

i = j = k = 0;

as: “Assign a value zero to variables i, j and k,” while:

i = 0; j = 0; k = 0;

would rather be: “Assign a value zero to variable i, then assign a value zero to variable j, and then assign a value zero to variable k.”


As already noted in comments, I have never seen the second style, and it looks weird. If I see this one, I would probably refactor it into the first form.

  • 4
    The second style would be much more reasonable, and IMO readable, if the variable names were long. A single line somelongName = anEvenLongerName_WithanUnderscore = supercalifragilistic = 0 would be much better in style #2. Also, "typing less code" is unimportant in deciding how to write clean programs. – user949300 Mar 13 '18 at 22:19
  • @user949300: interesting point. However, I believe that if the names of variables are long, I would rather switch to the full syntax with three assignments. As for the non-importance of typing less code, it would be relevant if i = j = k = 0 was less readable than the i = 0; j = 0... form, which is not the case here. So, in this particular case, shortest code wins. – Arseni Mourzenko Mar 13 '18 at 22:51
1

The primary benefit is for readability by humans. As a general rule of thumb, unless you are working on code that has to scale massively, it's usually worth it to trade minor execution inefficiency for simpler, readable, maintainable, or reusable code. Processor time is cheaper than programmer time, usually. In the chain assignment example there are no efficiency differences, but even if there were I would still probably use the chain assignment.

Putting it all in one line makes it more visually manageable and can hint to future programmers that the variables are related to one another.

On a big picture level, you want for someone to open your code and be able to say, "There are three loops that are executed in this routine and at the end we have this task completed." Anything you can do to allow a future programmer to look at your code and get there quicker is something you should work towards.

  • Since this isn't flagged as embedded-systems or real-time, I agree that more maintainable code trumps minor inefficiency. However, add either of those flags to the question and I believe your comment would change. – NetJohn Mar 28 '18 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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