`if` statements – one line vs. one expression
See original GitHub issueUntil 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:
- Created 8 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@englishextra your comment is both unhelpful and condescending; I’m going to delete it. Please be respectful in the future.
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:
vs.
vs.
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.