-
Notifications
You must be signed in to change notification settings - Fork 23
Open
Description
This might be more of a functional proverb than a Go proverb, but I frequently see variables which exist only to switch a branch.
This leads to a clear implementation but an unclear (to the reader) call.
func Modify(s string, upper, quote bool) string {
if upper {
s = strings.ToUpper(s)
}
if quote {
s = `"` + s + `"`
}
return s
}
Modify("this", true, true)When passing behaviour the intent of the call is obvious.
func Modify(s string, mod ...func(string) string) string {
for _, f := range mod {
s = f(s)
}
return s
}
func Quote(s string) string {
return `"` + s + `"`
}
Modify("this", Quote, strings.ToUpper)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels