32

I have a class which has a group of integers, say

foo()
{
  int a;
  int b;
  int c;
  int d;
  ....
  string s;
}

Now the question is for the best readbility, the init() function for foo(), should it look like

void init()
{
  a=b=c=d=1; //for some reason they are init to 1;
  s = "abc";
}

or

void init()
{
  a=1;
  b=1;
  c=1;
  d=1;
  s = "abc";
}

?

The reason for a string in class is a hint of other groups of same types might present and of course, the class might grow as requirement changes

EDIT: before this question goes too far, the intention of this question was simple: In Effective C++ item 12 (prefer initialization to assignment in constructors), Scott uses chain assignment instead of a=c; b=c; I am sure he knows when to use what, but I also remembered the books I read also recommended to use int a; int b; which in similar case of assignments. In my program I have a similar situation of a group of related individual build-in types needs to be initialized and I have found by making a chain assignment does makes it easier to read especially if the class have many other different types instance variables. It seems to contradict with books I read and my memory, hence the question.

7
  • Why not just do the assignment in the constructor? Commented Mar 21, 2011 at 2:14
  • 1
    Which language? You're not programming in all three. And perhaps a, b, c and d should be elements in an array.
    – GManNickG
    Commented Mar 21, 2011 at 2:14
  • @Kaleb , it is because for example in c++, initialize build-in types does not gain any performance and if the number of class members are big, it will become hard to read and error-pron. Or did you mean a init() is called in constructor?
    – Yuan
    Commented Mar 21, 2011 at 2:18
  • @GMan for some reason they are not preferred to be in an array, hence the question :) Maybe I chose tags too aggressively, but you got the idea :)
    – Yuan
    Commented Mar 21, 2011 at 2:19
  • @Yuan: Yes they are too aggressive, please fix them.
    – GManNickG
    Commented Mar 21, 2011 at 3:04

3 Answers 3

50

I happen to prefer the chained version, but it's completely a matter of preference.

Please note, however, that

a = b = c = 0;

is equivalent to:

c = 0;
b = c;
a = b;

and not

a = 0;
b = 0;
c = 0;

(not that it should matter to you which assignment happens first)

8
  • 3
    If you do such assertions on a multiple-language question, please specify for which language they are valid. I think in Java it would be c = 0; b = 0; a = 0; instead. (The differences would only be observable if using volatile variables, though.) Commented Mar 21, 2011 at 2:24
  • @Paulo: Good point. I'm specifically listing the behavior of C++, but I thought the other languages retain compatibility in this area. You're right that b=c vs b=0 only matters if some of the variables are volatile, the main point I was making was that c gets assigned before a, although again that should make a difference except for volatile, since the compiler can do reordering of writes to normal variables.
    – Ben Voigt
    Commented Mar 21, 2011 at 2:37
  • 2
    The fact that you have to point out that the assignment order is not obvious, should be a recommendation not to use it. Commented Mar 21, 2011 at 4:11
  • @Martin: Again, unless some of the variables are volatile, the compile can reorder the assignments. So which order is really "equivalent" is more a matter of pedanticism than anything.
    – Ben Voigt
    Commented Mar 21, 2011 at 4:56
  • 4
    strictly speaking, a=b=c=d; in C# is not the same as writing c=d;b=c;a=b;; this is apparent when c is a property (its get won't be invoked with a=b=c=d; but it will with c=d;b=c;a=b).
    – Mr. Smith
    Commented Feb 2, 2014 at 19:50
31

My personal preference is a=b=c=d for the following reasons:

  1. It is concise, saves lines
  2. It conveys the concept that (a/b/c/d) are initialized to the same thing, that they are related

However, caveat:

  1. Don't do that if a/b/c/d are not related (and just happens to be initialized to 1). You'll reduce the readability of your code. Example:

    a=c=1; // Foo-function related

    b=d=1; // Bar-function related

  2. Chaining assignments like this reduces the flexibility for you in the future to assign different initial values to the variables -- because then you'll have to break them up again.

Nevertheless, my personal recommendation is to chain assignments on variables that are related on concept/usage. In actual practice, the need to change an assignment usually doesn't come up often so caveat #2 should not typically pose a problem.

Edit: My recommendation may go against published guidelines. See the comments.

8
  • I do prefer a=b=c=d but I remembered when I declare variables, its good to say int a; int b; instead of int a,b; no?
    – Yuan
    Commented Mar 21, 2011 at 2:21
  • 1
    I usually declare variables one on each line, otherwise it is not clear at a glance what the type of each variable is. However, chain assignments conveys the message that the variables are related, and this may be worth something in terms of readability. Commented Mar 21, 2011 at 3:04
  • 7
    Why is saving lines a good thing? Clarity is king. One line per assignment. You are not paid to make it compact you are paid to make in understandable/robust and maintainable. Clarity is king. @Stephen Chung: who recommends chaining of assignments? Every coding guideline I have read recommends the exact opposite (and I have read a lot). Commented Mar 21, 2011 at 4:08
  • 1
    @Martin, I just advocate it myself. I didn't say it is recommended by any standard. I personally feel that chaining assignments on related variables provides more clarity/readability than one assignment on each line simply because it conveys the fact that those variables are related (or of similar usage). Commented Mar 21, 2011 at 4:24
  • 2
    @Stephen Chung : On the killing tress comment. That is just a silly confabulation. There are plenty of guidelines about making functions/methods short. Using multiple assignments on a single line is hardly going to make a difference in the larger context. Weighing this against the reduced clarity of the code an extra page of paper is going to actually cost the world more in resources by the extra electricity spent keeping a terminal on debugging problems that can be associated with it. See my comment on Ben Voigt question as to why it can cause problems. Commented Mar 21, 2011 at 4:46
9

I guess it is a matter of opinion which is most readable. (Clearly so ... otherwise you wouldn't be asking.)

However Oracle's "Code Conventions for the Java TM Programming Language" clearly says to use separate assignment statements:

10.4 Variable Assignments. "Avoid assigning several variables to the same value in a single statement. It is hard to read."

My opinion?

  1. Follow your project's prescribed / agreed style rules, even if you don't like them1.
  2. If your project doesn't (yet) have prescribed / agreed style rules:
    • Try to persuade the other members to adopt the most widely used applicable style rules.
    • If you can't persuade them / come to a consensus, then just do this informally for the major chunks of code that you write for the project1.

1 ... or get out.

1
  • 1
    +1 on following prescribed style rules or otherwise following the most widely used. Readability is always a matter of opinion and consistency is better than diversity. Ultimately the familiarity of consistency becomes more readable to people. Commented Mar 21, 2011 at 5:25

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