这段 if...else 有优雅的写法吗?

2024-04-30 17:22:51 +08:00
 waiaan
    function onSortChange({ order, prop }) {
      let sortType = 0
      if (order === 'descending') {
        if (prop === 'thisYearIncome') {
          sortType = 1
        }
        if (prop === 'lastYearIncome') {
          sortType = 3
        }
        if (prop === 'rate') {
          sortType = 5
        }
      } else {
        if (prop === 'thisYearIncome') {
          sortType = 2
        }
        if (prop === 'lastYearIncome') {
          sortType = 4
        }
        if (prop === 'rate') {
          sortType = 6
        }
      }
      this.fetchData(sortType)
    }

谢谢。

10409 次点击
所在节点    JavaScript
76 条回复
kinge
2024-05-01 15:58:09 +08:00
优雅的代码不易于阅读,这样就很好
ntedshen
2024-05-01 16:28:30 +08:00
map=new map([
['desc_tyi',1],
['desc_lyi',3],
['desc_r',5],
])

function onSortChange({ order, prop })
key=[order,pop].join('_')
type=map.get(key)??0
this.fetchData(sortType)
lDqe4OE6iOEUQNM7
2024-05-01 16:41:46 +08:00
function onSortChange({ order, prop }) {
const sortMap = {
descending: {
thisYearIncome: 1,
lastYearIncome: 3,
rate: 5
},
ascending: {
thisYearIncome: 2,
lastYearIncome: 4,
rate: 6
}
};

const sortType = sortMap[order][prop] || 0; // 如果没有匹配到,则默认为 0
this.fetchData(sortType);
}
incubus
2024-05-01 16:43:31 +08:00
function onSortChange({ order, prop }) {
const sortTypeMap = {
'thisYearIncome': { 'descending': 1, 'ascending': 2 },
'lastYearIncome': { 'descending': 3, 'ascending': 4 },
'rate': { 'descending': 5, 'ascending': 6 }
};

const sortType = sortTypeMap[prop][order];
this.fetchData(sortType);
}
FYFX
2024-05-01 19:27:30 +08:00
你是 order2 种值,prop 3 种值组合总共 6 种,我是觉得把 6 种可能枚举出来比较好,先判断 order 再判断 prop 感觉没那么清晰
mrytsr
2024-05-01 19:37:01 +08:00
没搞懂为什么要把 prop 和 order 转成 sorttype ,建议直接存储或传输 prop,order
pennai
2024-05-01 22:34:53 +08:00
1.可以配在 db 里,这样一行查 db 然后一行赋值 sort type 就行
2.策略模式实现
3.上规则引擎
nuk
2024-05-02 00:07:04 +08:00
let sortType = order === 'descending'?prop === 'thisYearIncome'?1:prop === 'lastYearIncome'? 3:prop === 'rate'?5:0:prop === 'thisYearIncome'?2:prop === 'lastYearIncome'?4:prop === 'rate'?6:0
shyangs
2024-05-02 00:33:45 +08:00
@z1829909

'''
> 举手提问, 这里为啥需要转成数字传入 fetchData, 为什么不在这一层直接转为查询条件传入 fetchData.
因为我觉得 sortType 在 fetchData 中又要被拆开解析, 看代码的人又要去记 sortType 的意思, 多一层开销
'''

z1829909 說的對,原本 order, prop 不是魔法數字 (magic number),經過 onSortChange 反而變成魔術數字了.

onSortChange 根本就是多餘的,fetchData 直接傳入 order, prop 即可. 呼叫者直接調用 fetchData 即可.
walkerliu
2024-05-02 09:20:11 +08:00
评论里的反向优化看醉了..........。OP 的代码逻辑清晰, 可读性强,也没性能问题。 评论里面又是匿名函数又是三目运算符的.. 这不妥妥的反向优化嘛
cpstar
2024-05-02 09:21:52 +08:00
全都是优雅 order 和 prop 的,窃以为问题的根源在 0123456
如果真的要优雅,应该多态到头,连带 fetchData 做到多态
amlee
2024-05-02 10:25:58 +08:00
我不懂为什么接口要单独定义一个 sortType ,直接 fetchData(order, prop)不就好了?后端也简单前端也简单。
zcm3579
2024-05-02 11:38:43 +08:00
你这边前端转成数字 , 后端接口拿到后是不是又转回字段和 order
liKeYunKeji
2024-05-02 12:22:46 +08:00
使用对象映射法:

```
function onSortChange({ order, prop }) {
const propMap = {
thisYearIncome: {
descending: 1,
ascending: 2
},
lastYearIncome: {
descending: 3,
ascending: 4
},
rate: {
descending: 5,
ascending: 6
}
};

const sortType = propMap[prop][order];
this.fetchData(sortType);
}
```
bv
2024-05-02 20:09:09 +08:00
下面这样,有点为了优化而优化,让人难以理解了:
function onSortChange(order, prop) {
let props = new Map([
["thisYearIncome", 1],
["lastYearIncome", 3],
["rate", 5],
]);

this.fetchData((order === "descending" ? 0 : 1) + props.get(prop))
}
netabare
2024-05-03 19:22:15 +08:00
表驱动。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1037129

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX