Should I put newlines before or after binary operators? [closed]

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật