Caesar Caesar - 2 months ago 8
C++ Question

Why are multiple assignments in one line considered bad style?

In C++ you can do multiple assignment by doing this

x = y = z = 10;


Yet multiple people have told me that is a bad style and I shouldn't be using it without giving me a reason why.

Can someone please explain to me why this is considered a bad style?

Answer

It's not inherently bad style, but you can often make the code clearer by doing just one assignment per line and letting the compiler optimizer sort things out. If you use the multiple-assignment style then sometimes it might not be clear whether x = y = z = 10; was intentional or whether it was a typo for something like x = y = z + 10;. By always limiting yourself to one assignment per statement you make it obvious when typos occur.

Comments