Disable the curly
rule
#219
Replies: 2 comments
-
Allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following: if (foo) foo++; Can be rewritten as: if (foo) {
foo++;
} Some problems reported by this rule are automatically fixable. For more details see the documentation |
Beta Was this translation helpful? Give feedback.
-
Let me get ahead that most comments against this aren't really relevant, since prettier already normalises a lot. A common mistake is code like this: if (foo)
console.log('a');
console.log('b');
somethingElse(); will only wrap the first log in the if, while the other is part of the normal code flow. However, prettier already rewrites it as: if (foo) console.log('a');
console.log('b');
somethingElse(); Or the people that say adding/removing braces when they want to add/remove a line: There are IDE quick-fixes for that (same as for arrow function braces). I also do agree that it's preferred for small checks/statements like this: if (conditionIsTrue) return;
if (isEnabled) callSomething() However, things get a bit more verbose/condensed when looking at: // nobody would write this, but it's allowed
if (a === true) if (b === true) alert(a);
// how do you parse the above clearly?
if (someFunc(passParam) > SOME_LIMIT) somethingElse(withAParam, 'a-setting'); In the last example, I find it difficult to parse when the condition ends, and the "then" part starts. And I'm afraid that a lot of code will start looking like that :( Especially when having if (someFunc(passParam) > SOME_LIMIT) somethingElse(withAParam, 'a-setting');
else if (someFunc(passParam, param2) > OTHER_LIMIT) somethingElse(withAParam);
else if (!otherFunc(passParam)) somethingElse(withAParam, 'a-setting');
else doCompletelyNothing('thequicbrownfox');
The issue (in all of these conversations) is that this differs from person to person. Where one lines to scan horizontally, others like to scan vertically. Where some like whitespace to clearly see separation in pieces of code, others like things more condensed to reduce scrolling. |
Beta Was this translation helpful? Give feedback.
-
There is a proposal to disable the
curly
rule.Beta Was this translation helpful? Give feedback.
All reactions