大佬们这段 golang 代码怎么优化,这么多 if 判断
... col := column.typeFn(column.ctype) //primaryKey if column.ColumnFn["primaryKey"] != nil { col = col + column.ColumnFn["primaryKey"](column) } //autoIncrement if column.ColumnFn["autoIncrement"] != nil { col = col + column.ColumnFn["autoIncrement"](column) } //default if column.ColumnFn["default"] != nil { col = col + column.ColumnFn["default"](column) } //notNull if column.ColumnFn["notNull"] != nil { col = col + column.ColumnFn["notNull"](column) } //comment if column.ColumnFn["comment"] != nil && !column.Add { col = col + column.ColumnFn["comment"](column) } ...
![]() | 1 jkm 2024-01-25 15:35:55 +08:00 sth like this? ``` col := column.typeFn(column.ctype) constraints := []string{"primaryKey", autoIncrement", "default", "notNull", } for _, cs := range(constraints) { if column.ColumnFn[cs != nil { col = col + column.ColumnFn[cs](column) } } ``` |
![]() | 2 yinxs2003 2024-01-25 15:41:18 +08:00 能用就不要动,重构是非常费力不讨好的事情 |
![]() | 3 yinxs2003 2024-01-25 15:44:41 +08:00 可以使用 switch 或者策略模式替换这段代码 https://www.javanorth.cn/2022/07/08/%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F-20220712/ |
![]() | 4 pdxjun OP 谢谢大佬们 |
5 falser101 2024-01-25 17:49:17 +08:00 GPT3.5 您可以通过将相似的逻辑整理成一个函数,并使用循环来遍历需要执行的函数,以减少代码的冗余。以下是一个优化后的示例: go Copy code // 定义一个函数类型,用于处理特定的 ColumnFn type ColumnFuncHandler func(column *Column) string // 处理 ColumnFn 的函数 func processColumnFn(column *Column, fnName string, fnHandler ColumnFuncHandler) string { if column.ColumnFn[fnName] != nil { return fnHandler(column) } return "" } // 处理所有 ColumnFn 的函数 func processAllColumnFns(column *Column) string { var result string // 定义需要处理的函数名列表 fnNames := []string{"primaryKey", "autoIncrement", "default", "notNull", "comment"} // 遍历函数名列表,执行相应的处理函数 for _, fnName := range fnNames { result += processColumnFn(column, fnName, column.ColumnFn[fName]) } return result } // 在你的代码中调用 processAllColumnFns col := column.typeFn(column.ctype) + processAllColumnFns(column) 通过这种方式,您可以将重复的逻辑整理到函数中,并通过循环来遍历需要执行的函数。这样可以使代码更加清晰、可维护,并减少冗余。同时,如果以后需要添加或删除某个 ColumnFn ,只需在 fnNames 中进行相应的修改,而不必在每个条件语句中进行更改。 |
![]() | 6 9136347 2024-01-25 18:03:36 +08:00 挺好的,清晰明了。别改了。 |
7 mydev6666666 2024-01-25 21:44:26 +08:00 col := column.typeFn(column.ctype) // 定义需要按顺序处理的列属性 columnAttributes := []string{"primaryKey", "autoIncrement", "default", "notNull", "comment"} for _, attr := range columnAttributes { if fn, ok := column.ColumnFn[attr]; ok && (attr != "comment" || !column.Add) { col += fn(column) } } |
8 mydev6666666 2024-01-25 21:46:10 +08:00 我是直接问 chatgpt 的。 |
9 yulon 2024-01-25 22:03:21 +08:00 我建议更名叫 v2ai |
![]() | 10 zjttfs 2024-01-25 22:35:06 +08:00 挺好的.一眼明了 见过这种代码吗 ``` a:=xxx //一些简单的代码 b:=a //一些简单的代码 c:=b //一些简单的代码 d:=c ``` |
![]() | 11 CEBBCAT 2024-01-25 22:39:07 +08:00 ![]() 清晰明了,我看写得挺好。楼里唯一能认同的理由是行数多,这个可以用表驱动来合并,特别注意的是,我建议一行一个策略,不然排序、合并代码冲突的时候要傻眼的 |
13 GeruzoniAnsasu 2024-01-25 23:15:56 +08:00 怎么没人提 validator 呢 struct tag 就是设计来简化这坨没必要的 if 判断的 |
![]() | 14 8rmEHZ8WhVHVOb0E 2024-01-25 23:19:11 +08:00 var col strings.Builder col.WriteString(column.typeFn(column.ctype)) // column functions columnFns := []string{"primaryKey", "autoIncrement", "default", "notNull", "comment"} for _, fn := range columnFns { if column.ColumnFn[fn] != nil { col.WriteString(column.ColumnFn[fn](column)) } } finalCol := col.String() |
![]() | 15 CEBBCAT 2024-01-26 01:05:27 +08:00 @GeruzoniAnsasu #13 这个感觉更像是 ORM 建表那里?不太像是参数检查。 |
![]() | 16 namek 2024-01-26 08:58:12 +08:00 看起来不够酷,确是最简洁高效的 |
17 CodeCodeStudy 2024-01-26 09:49:14 +08:00 简洁明了,如果是别人写的就不要改了。另外行数也挺多的,如果用行数来考核的话,也能取得不错的成绩。 |
![]() | 18 dhb233 2024-01-26 10:45:30 +08:00 从命名推测 ColumnFn 应该就是所有要调用的处理函数,如果是要添加一个新的方法,为了方便扩展,那直接这样就可以吧 for n, cf := range column.ColumnFn { col = col + cf(column) } 如果不是为了扩展,那还是别闲的没事改代码了。。。 |
![]() | 20 hoosin 2024-01-26 14:19:46 +08:00 ``` keys := []string{"primaryKey", "autoIncrement", "default", "notNull", "comment"} for _, key := range keys { if column.ColumnFn[key] != nil { if key != "comment" || !column.Add { col = col + column.ColumnFn[key](column) } } } ``` |
![]() | 21 fionasit007 2024-01-26 16:51:49 +08:00 代码最重要的可读性 |
![]() | 22 SSang 2024-01-29 15:18:53 +08:00 别改,这段代码我觉得写的非常好,没有优化的必要。 循环需要保证模式相同,前人写这个代码可能就是怕后期增加不同模式的判断。 |