Comments (4)
这条可能考虑禁止,将代码扁平化。
from eslint-config-alloy.
禁用这个规则有以下几个原因:
1. 我觉得下面两种写法中,前者阅读起来更清晰:
// 阅读起来结构清晰,层次分明,在同一个缩进层次下
function foo() {
if (bar) {
return doSomething();
} else {
return doOther();
}
}
// 没有前者结构清晰,特别是代码行数较多的时候
function foo() {
if (bar) {
return doSomething();
}
return doOther();
}
2. early return 写多了之后,容易舍本逐末的想给每个 if
都加个 return
,即使它根本不需要返回值
// 正常写法,不需要返回值
function foo() {
if (bar) {
doSomething();
} else {
doOther();
}
}
// 为了 early return,而使 foo 有了返回值
function foo() {
if (bar) {
return doSomething();
}
doOther();
}
from eslint-config-alloy.
@xcatliu 这确实是一个有争议性的话题,强制开启可以让代码更加扁平化,依靠层次结构来表现清晰带来的好处可能抵不过else return所带来的可能层级嵌套很深带来的副作用。
前端的一个很重要的话题就是消除回调地狱,这条规则可以很有效的避免层级过深的情况,如果大家都遵循规范,我们将习惯于阅读扁平化的代码,清晰度不会比简单的缩进带来的清晰度差很多。
一般情况下early return用于处理一些异常情况,见过有人在if return后在else里面又进行异步调用,异步调用中再来个if else,这个时候如果能开启该规则,可以避免陷入回调地狱,我看到这样的代码会感到十分不舒服。
from eslint-config-alloy.
这条规则只有在 if 里面有 return 语句的时候才会生效,所以并不能有效的避免回调地狱。
避免回调地狱的方式,eslint 已经提供了:max-nested-callbacks
from eslint-config-alloy.
Related Issues (20)
- 为什么在vue.js中eqeqeq是off二不是error? HOT 10
- Typo: no-unused-vars HOT 1
- [feature] eslint-config-alloy@3 中建议将parser由babel-eslint升级为@babel/eslint-parser HOT 2
- TypeScript should disable "no-undef" HOT 1
- 请问一下有没有对 typescript 5 的支持计划 HOT 1
- 安装[email protected]版本报错 HOT 5
- 为什么要启用这个 prefer-object-has-own 规则,有兼容性问题 HOT 1
- consistent type export/import HOT 6
- [feature] eslint rules conflict with mocha timeout HOT 1
- @typescript-eslint/parser 更新到 6.5.0 以上版本后报错 Error while loading rule '@typescript-eslint/consistent-type-assertions'
- vue/cli 5.0.8版本使用ts+vue的例子时,出现ERROR HOT 1
- 是否可以加入import排序配置 HOT 1
- 什么时候会适配新的eslint.config.js HOT 2
- 是否开个新坑呢?关于biomejs HOT 2
- [feature] 考虑一个alloy版本兼容不同语言,比如vue2/vue3/typescript4/typescript5 HOT 4
- 提供的 example 无法使用 HOT 4
- no-undef 规则默认关闭的原因是什么? HOT 1
- 请问 @babel/core @babel/eslint-parser 是否还有必要安装
- 当前提供的库和vue命令行安装的库并不相同,是否同步为官方推荐的库? HOT 2
- Support eslint 9 flat config
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eslint-config-alloy.