Cleaning up eval code idea #1552
Replies: 3 comments 5 replies
-
I have also noticed the large amounts of repetition, which increases the chances of typos when adding new options. I'm not so concerned with performance, since users will usually only set about 10-20 options, and this will only impact startup times. My only concern is the additional complexity involved (i.e. creating another method of setting options, which creates another layer of abstraction and can only handle about half of the options). I still think this is worth pursuing though, I wrote up my own implementation just now which looks like this: func applyBoolOpt(opt *bool, e *setExpr) error {
switch {
case strings.HasPrefix(e.opt, "no"):
if e.val != "" {
return fmt.Errorf("%s: unexpected value: %s", e.opt, e.val)
}
*opt = false
case strings.HasSuffix(e.opt, "!"):
if e.val != "" {
return fmt.Errorf("%s: unexpected value: %s", e.opt, e.val)
}
*opt = !*opt
default:
switch e.val {
case "", "true":
*opt = true
case "false":
*opt = false
default:
return fmt.Errorf("%s: value should be empty, 'true', or 'false'", e.opt)
}
}
return nil
}
func (e *setExpr) eval(app *app, args []string) {
switch e.opt {
case "anchorfind", "noanchorfind", "anchorfind!":
if err := applyBoolOpt(&gOpts.anchorfind, e); err != nil {
app.ui.echoerr(err.Error())
return
}
// do anything else if necessary
case "autoquit", "noautoquit", "autoquit!":
if err := applyBoolOpt(&gOpts.autoquit, e); err != nil {
app.ui.echoerr(err.Error())
return
}
// remaining options
}
} |
Beta Was this translation helpful? Give feedback.
-
On this only handling about half of the options: The map idea is intended to completely remove the switch statement in |
Beta Was this translation helpful? Give feedback.
-
Tagging @gokcehan, I suspect you will want to review this idea. Also @laktak might be interested after having to implement the |
Beta Was this translation helpful? Give feedback.
-
Hello
Currently the setExpr eval function is an over 900 line long switch statement
https://github.com/gokcehan/lf/blob/master/eval.go#L17
In the vast majority of cases the implementation is either exactly the same as or a lightly modified version of other options and this creates a lot of repetition. The string ": value should be empty, 'true', or 'false'" alone shows 30 results in this function. This seems really difficult to maintain and likely to result in errors/typos.
I believe it makes more sense to create a map of string: functions. I have a proof of concept for how I can see this working on my fork. Options that need special logic can have their own functions. I would just like to know what the team thinks of the idea before I put the effort in to fully remove the switch.
I know function calls are not free in Go ,but that at scale a map lookup is more performant than a switch, if this is a concern I can try to write some benchmarks.
Beta Was this translation helpful? Give feedback.
All reactions