V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
pdxjun
V2EX  ›  Go 编程语言

大佬们这段 golang 代码怎么优化,这么多 if 判断

  •  
  •   pdxjun · 93 天前 · 2728 次点击
    这是一个创建于 93 天前的主题,其中的信息可能已经有所发展或是发生改变。

    大佬们这段 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)
    	}
           ...
    
    jkm
        1
    jkm  
       93 天前
    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)
    }
    }
    ```
    yinxs2003
        2
    yinxs2003  
       93 天前
    能用就不要动,重构是非常费力不讨好的事情
    yinxs2003
        3
    yinxs2003  
       93 天前
    可以使用 switch 或者策略模式替换这段代码
    https://www.javanorth.cn/2022/07/08/%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F-20220712/
    pdxjun
        4
    pdxjun  
    OP
       93 天前
    谢谢大佬们
    falser101
        5
    falser101  
       93 天前
    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[fnName])
    }

    return result
    }

    // 在你的代码中调用 processAllColumnFns
    col := column.typeFn(column.ctype) + processAllColumnFns(column)
    通过这种方式,您可以将重复的逻辑整理到函数中,并通过循环来遍历需要执行的函数。这样可以使代码更加清晰、可维护,并减少冗余。同时,如果以后需要添加或删除某个 ColumnFn ,只需在 fnNames 中进行相应的修改,而不必在每个条件语句中进行更改。
    9136347
        6
    9136347  
       93 天前
    挺好的,清晰明了。别改了。
    mydev6666666
        7
    mydev6666666  
       92 天前
    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)
    }
    }
    mydev6666666
        8
    mydev6666666  
       92 天前
    我是直接问 chatgpt 的。
    yulon
        9
    yulon  
       92 天前
    我建议更名叫 v2ai 😎
    zjttfs
        10
    zjttfs  
       92 天前
    挺好的.一眼明了
    见过这种代码吗
    ```
    a:=xxx
    //一些简单的代码
    b:=a
    //一些简单的代码
    c:=b
    //一些简单的代码
    d:=c
    ```
    CEBBCAT
        11
    CEBBCAT  
       92 天前   ❤️ 1
    清晰明了,我看写得挺好。楼里唯一能认同的理由是行数多,这个可以用表驱动来合并,特别注意的是,我建议一行一个策略,不然排序、合并代码冲突的时候要傻眼的
    cmsyh29
        12
    cmsyh29  
       92 天前
    @falser101 确实写的比我好
    GeruzoniAnsasu
        13
    GeruzoniAnsasu  
       92 天前
    怎么没人提 validator 呢

    struct tag 就是设计来简化这坨没必要的 if 判断的
    xiaomada
        14
    xiaomada  
       92 天前
    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()
    CEBBCAT
        15
    CEBBCAT  
       92 天前
    @GeruzoniAnsasu #13 这个感觉更像是 ORM 建表那里?不太像是参数检查。
    namek
        16
    namek  
       92 天前
    看起来不够酷,确是最简洁高效的
    CodeCodeStudy
        17
    CodeCodeStudy  
       92 天前
    简洁明了,如果是别人写的就不要改了。另外行数也挺多的,如果用行数来考核的话,也能取得不错的成绩。
    dhb233
        18
    dhb233  
       92 天前
    从命名推测 ColumnFn 应该就是所有要调用的处理函数,如果是要添加一个新的方法,为了方便扩展,那直接这样就可以吧
    for n, cf := range column.ColumnFn {
    col = col + cf(column)
    }

    如果不是为了扩展,那还是别闲的没事改代码了。。。
    dhb233
        19
    dhb233  
       92 天前
    @dhb233 这个错了,看起来是在拼字符串?这样就乱序了。。。
    hoosin
        20
    hoosin  
       92 天前
    ```
    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)
    }
    }
    }
    ```
    fionasit007
        21
    fionasit007  
       92 天前
    代码最重要的可读性
    SSang
        22
    SSang  
       89 天前
    别改,这段代码我觉得写的非常好,没有优化的必要。

    循环需要保证模式相同,前人写这个代码可能就是怕后期增加不同模式的判断。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3009 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 13:43 · PVG 21:43 · LAX 06:43 · JFK 09:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.