question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multi-line if statements - breaks and indentation

See original GitHub issue

I think it would be nice to add an additional section to the guide for multi-line if statements, especially where to set breaks and how the indentation should look like? I have added some examples below and would like to know what your preferred way of formatting this is.

Example:

if (a === 123 && b === 'abc') {
  doSomething();
}

Case A: Breaks are applied after logical operators.

if (a === 123 &&
  b === 'abc') {
  doSomething();
}

Case B: Breaks are applied before logical operators.

if (a === 123
  && b === 'abc') {
  doSomething();
}

Indentation: Regardless of where the breaks are set, indent the next line with 4 instead of 2 spaces.

// Case A
if (a === 123 &&
    b === 'abc') {
  doSomething();
}

// Case B
if (a === 123
    && b === 'abc') {
  doSomething();
}

I personally prefer Case B because you can immediately see what’s going on, but I’m not sure about the indentation - would still prefer 2 over 4 though as putting the operators in front of it help a little bit to distinguish the statement from the block.

At this point I also want to say that this guide is super useful, so thanks a lot for sharing this!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

62reactions
ljharbcommented, Apr 21, 2017

Neither.

Only one of these two is acceptable:

if (
  a === 123 &&
  b === 'abc'
) {
if (
  a === 123
  && b === 'abc'
) {

We’re not decided yet, nor do we enforce, whether the operator should end lines or begin them.

A PR to the guide that adds a section on this would be appreciated.

25reactions
aliofyecommented, Oct 21, 2019

I vote for this:

if (
  a === 123
  && b === 'abc'
) {

Simply because you can easily comment out a condition if you are debugging like so:

if (
  a === 123
  // && b === 'abc'
) {
Read more comments on GitHub >

github_iconTop Results From Across the Web

Styling multi-line conditions in 'if' statements? [closed]
In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For...
Read more >
Styling multiline if statements in Python | bobbyhadz
The recommended style for multiline if statements in Python is to use parentheses to break up the if statement. The PEP8 style guide...
Read more >
Style for Python Multiline If-Statements - Naftali Harris
Well, for one, I like how the code for the condition, True, and False branches all line up. It looks visually pleasant, (at...
Read more >
Use conditional statements to handle multi-line conditions
Sometimes I break long conditions in ifs onto several lines. The most obvious way to do this is: if (cond1 == 'val1' and...
Read more >
Manual :: Split long if statements onto several lines - PHP
The conditions have to be positioned onto the following line, and indented 4 characters. The logical operators ( && , || , etc.)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found