Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: add if/else -> switch suggesting checker #77

Closed
quasilyte opened this issue May 11, 2018 · 0 comments
Closed

lint: add if/else -> switch suggesting checker #77

quasilyte opened this issue May 11, 2018 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@quasilyte
Copy link
Member

quasilyte commented May 11, 2018

If there is a chain of if/else, suggest switch as alternative.

These two functions should trigger the warning:

func cond1(x int) string {
	if x == 0 {
		return "zero"
	} else if x < 0 {
		return "negative"
	} else {
		return "positive"
	}
}

func cond2(x int) string {
	if x == 0 {
		return "zero"
	} else if x < 0 {
		return "negative"
	}
	return "positive"
}

Suggested alternative is:

func condSuggested(x int) string {
	switch {
	case x == 0:
		return "zero"
	case x < 0:
		return "negative"
	default:
		return "positive"
	}
}

TODO: find out if if/else is ever more readable or wanted instead of switch.
Maybe init statements can complicate things.

@quasilyte quasilyte added the enhancement New feature or request label May 11, 2018
quasilyte added a commit that referenced this issue May 12, 2018
@quasilyte quasilyte self-assigned this May 14, 2018
quasilyte pushed a commit that referenced this issue May 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant