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.

`if` statements – one line vs. one expression

See original GitHub issue

Until I started using ESLint with your configuration I saw nothing against the rules in this:

$ cat if.js
const [one, two] = [1, 2];

if (one !== two) throw new Error(
  'One does not equal two'
);

But ESLint does:

$ eslint if.js 

if.js
  3:0  error  Expected { after 'if' condition  curly

✖ 1 problem (1 error, 0 warnings)

Is this intended? In my opinion the pattern I’ve been using is explicit – and more readable than this:

if (one !== two) {
  throw new Error(
    'One does not equal two'
  );
}

The parens form a visual brace-like block much like in #438.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Aug 22, 2017

@englishextra your comment is both unhelpful and condescending; I’m going to delete it. Please be respectful in the future.

1reaction
tomek-he-himcommented, Jul 30, 2015

Yeah, both of your examples look good with a five-word error message. I use my variant because my error messages tend to get long and descriptive – much like in the #438 example.

So it’s a question of:

if (one !== two) throw new Error(
  'One does not equal two. This error is common when you try to determine if ' +
  'the number `1` has the same value as the number `2`. Please try again, or ' +
  '– if that fails – get in touch with your administrator.'
);

vs.

if (one !== two) throw new Error('One does not equal two. This error is common when you try to determine if the number `1` has the same value as the number `2`. Please try again, or – if that fails – get in touch with your administrator.');

vs.

if (one !== two) {
  throw new Error(
    'One does not equal two. This error is common when you try to determine ' +
    'if the number `1` has the same value as the number `2`. Please try ' +
    'again, or – if that fails – get in touch with your administrator.'
  );
}

The second is obviously invalid. And I do prefer the first to the last – for the sake of readability.

But feel free to close the issue if you don’t think my point makes sense – I’ll adapt.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The One Liner If Statement (Kinda): Ternary Operators ...
The difference between the two is If is a statement whereas a ternary operator is an expression; the latter returns a value while...
Read more >
Python If-Else Statement in One Line - Ternary Operator ...
A single-line if statement just means you're deleting the new line and indentation. You're still writing the same code, with the only twist ......
Read more >
Putting a simple if-then-else statement on one line [duplicate]
That's more specifically a ternary operator expression than an if-then, here's the python syntax value_when_true if condition else value_when_false.
Read more >
One Line If Statements - AutoHotkey Community
The , is part of the expression here therefore it cannot be used here since we dont know whether it's actually just part...
Read more >
One line if statement in Python (ternary conditional operator)
In the real world, there are specific classifications and conditions on every action that occurs around us. A twelve-year-old person is a ...
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