Readability: splitting lines of code, should the operator be on the first or second line?

242 views Asked by At

Simple question about formatting code (in my case C++).

If I have a line of code, say:

SomeSortOfLongVariable = AnotherLongVariableThatTakesUpTonsOfHorizontalSpace + IDontActuallyUseVariablesThisLong

Should I split it like:

SomeSortOfLongVariable = AnotherLongVariableThatTakesUpTonsOfHorizontalSpace +
                         IDontActuallyUseVariablesThisLong

or:

SomeSortOfLongVariable = AnotherLongVariableThatTakesUpTonsOfHorizontalSpace 
                         + IDontActuallyUseVariablesThisLong

Another example:

foo = bar->baz;
// Should it be:
foo = bar->
      baz;
//Or:
foo = bar
      ->baz;

Do people have a preference when it comes to this? Is it on a case-by-case basis? They both seem equally clear (or unclear) to me, so I was wondering if there are any standard ways of doing it.

1

There are 1 answers

0
aschepler On BEST ANSWER

Yes, people have preferences about this.

No, there's no standard or consensus agreement. I'm not aware of any especially strong arguments for either side, either.

So like any coding style issue, you can stick with whatever other people who edit the same code have been doing, or just pick one you like.

(I wouldn't even complain about being inconsistent on this issue.)