30

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.

Yuan
  • 2,690
  • 4
  • 26
  • 38
  • Why not just do the assignment in the constructor? – Kaleb Brasee Mar 21 '11 at 02:14
  • Which language? You're not programming in all three. And perhaps `a`, `b`, `c` and `d` should be elements in an array. – GManNickG Mar 21 '11 at 02: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 Mar 21 '11 at 02: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 Mar 21 '11 at 02:19
  • @Yuan: Yes they are too aggressive, please fix them. – GManNickG Mar 21 '11 at 03:04
  • I have an opinion (as expressed below in comments to others subjective comments). But in reality the actual answer is totally subjective. It is up-to the user to decide based on context. As such this question should be closed as subjective. – Martin York Mar 21 '11 at 04:13
  • You could always do a 'one-liner' as follows: `a=1; b=1; c=1; d=1`. It may be a bit unorthodox, and it's _slightly_ longer, but it's clear enough for me. – Mike Smith Jul 01 '20 at 05:33

3 Answers3

48

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)

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 2
    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.) – Paŭlo Ebermann Mar 21 '11 at 02: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 Mar 21 '11 at 02:37
  • 1
    The fact that you have to point out that the assignment order is not obvious, should be a recommendation not to use it. – Martin York Mar 21 '11 at 04: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 Mar 21 '11 at 04:56
  • @Ben Voigt : With the assumption that the variables are POD. If not then the compiler can re-order the assignments. So unless you want to have different style one for POD and another for class types you leave yourself open to the confusion of the syntax. – Martin York Mar 21 '11 at 06:59
  • @Martin: POD-ness has not much to do with it. A type can be POD and still have a user-defined `operator=`. Anyway, if the `operator=` is user-defined and not `inline`, the compiler can't reorder, but in the common case reordering is possible. – Ben Voigt Mar 21 '11 at 14:33
  • 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 Feb 02 '14 at 19:50
  • @Mr.Smith: That is an interesting (and correct) observation, but `c` is not a property so it doesn't apply here. – Ben Voigt Feb 02 '14 at 19:51
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.

Stephen Chung
  • 14,497
  • 1
  • 35
  • 48
  • 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 Mar 21 '11 at 02: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. – Stephen Chung Mar 21 '11 at 03:04
  • 6
    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). – Martin York Mar 21 '11 at 04: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). – Stephen Chung Mar 21 '11 at 04:24
  • @Margin, also saving lines has some benefits as well. It makes it easier to read more code in one screenful (and not having to scroll). For people who still kill trees by printing out source code, it can be the difference of multiple spanning pages or a few pages. – Stephen Chung Mar 21 '11 at 04:26
  • @Stephen Chung: If its your opinion then make that explicitly clear in the answer. As it currently stands you are implying this is a general recommendation of C++ community (which it is not. The C++ community is probably split 60/40 on the issue and thus you can not say there is particular correct answer). – Martin York Mar 21 '11 at 04:42
  • @Martin, ok. Will amend answer. – Stephen Chung Mar 21 '11 at 04:44
  • 1
    @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. – Martin York Mar 21 '11 at 04:46
8

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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. – WhiteFang34 Mar 21 '11 at 05:25