When you’re in Python or Javascript, you should always put binary operators at the end of the previous line, in order to prevent newlines from terminating your code prematurely; it helps you catch errors.
But in C or C++, this isn’t an issue, so I’m wondering:
Is there any reason for me to prefer the second version to the first?
return lots_of_text
+ 1;
versus
return lots_of_text +
1;
(For example, does one of them help prevent other kinds of errors? Or is one of them considered more readable?)
11
As you can see from the answers, there is no consensus on this matter. Unless you work in a team, use what you are more comfortable with.
I prefer inserting a newline before operators.
Whenever I have to break lines, I usually put at most one term of the same “level” on a line:
Newton’s law of gravitation in Python:
force = (
gravitational_constant
* mass_1
* mass_2
/ (distance * distance)
)
Compare this to:
force = (
gravitational_constant *
mass_1 *
mass_2 /
(distance * distance)
)
I want to know, that I “divide by distance squared”, I don’t want to know, that “mass_2 gets divided”, because that’s not how I think of mathematical expressions.
Further, I usually want to know first, what I am doing (operator), before I care about what I do things with (operands).
Or consider this convoluted SQL statement:
WHERE
a = 1
AND b = 2
AND c = 3
AND ( -- or put the OR on one line together with the AND
d = 3
OR e = 1)
AND x = 5
This allows me to see how the individual conditions are connected very easily, just by skimming from top to bottom without having to read every line until the end to find the operator as opposed to:
WHERE
a = 1 AND
b = 2 AND
c = 3 AND
(
d = 3 OR
e = 1) AND
x = 5
I think about the former in terms of “X
is true”, then I amend that by saying: “AND this is also true” which feels more natural to me than the other way around. Further, I find the first much easier to parse visually.
Or a PHP example:
$text = "lorem ipsum"
. "dolor sit amet, "
. "consectetur adipisicing elit, "
. "sed do eiusmod tempor";
Again, I can just skim read vertically to see I’m simply concatenating text, because most of the time I feel that I do not actually care what is inside the strings/conditions.
Of course, I would not apply this style unconditionally. If putting the newline after an operator seems to make more sense to me, I would do so, but I can’t think of an example at the moment.
4
I almost always break lines before binary operators in order to make clear to readers of the code that this is the continuation of an expression and not the next statement. This is important if the next statement would normally be indented. For example, consider an if statement that has a complex conditional expression:
if (first_part_of_condition &&
second_part_of_condition) {
statement_within_then_part;
}
The second part of the conditional expression is easily confused with the first statement of the then-part. This can be quite confusing if the first part of the condition is long and the && ends up way off to the right. Compare this with the alternative:
if (first_part_of_condition
&& second_part_of_condition) {
statement_within_then_part;
}
This looks a bit odd but it makes it quite clear that the second part of the condition is not the beginning of a statement.
I do this in others contexts too but the if-statement is the most important, as the indentation is ambiguous. Of course, one could change the indentation or brace placement, but this looks odd too.
tl;dr the left end of the line is more significant to quick reading comprehension, so putting the operator on the left end is a more prominent marker that this is a continued expression, not a statement.
2
I’d always use the first. You want it to be clear what you are doing with that 1 there. If I saw that and there was a wall of text above that 1 I’d have no idea what was going on as far as that 1’s usage.
# The first
return lots_of_text
+ 1;
The ‘+’ being next to 1 makes it clear that at a minimum I am doing something with the 1 in question rather than it hanging out going “I’m all in your code dude…” for no apparent reason.
1
I feel that it helps reading comprehension tremendously when a line indicates that the statement continues in the next line by having the binary operator at the end, and thus rendering the statement incomplete. The missing semicolon isn’t enough for me to immediately understand that.
This may simply be a question of habit but unless you work exclusively in languages which require the semicolon (which I find pretty unrealistic given the pervasiveness of shell scripts, makefiles and languages such as Python) you probably won’t have that habit either.
EDIT: Giorgio makes an excellent point in the comments: this usage echoes common punctuation, both in mathematics and natural languages. You’d write
I sink,
therefore I swam
You would not write
I sink
, therefore I swam
10