Skip to content

Don't use data to drive behaviour, use behaviour. #38

@brackendawson

Description

@brackendawson

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions