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

refactor: removes useless else to get more idiomatic GO code #1432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chavacava
Copy link

This PR removes useless else statements and replaces code blocks to get more idiomatic Go code and less deep nesting.

Refs: https://youtu.be/5DVV36uqQ4E?t=660

@AnyCPU
Copy link

AnyCPU commented Jan 16, 2025

some notes:

  1. actually there are a lot of good code style guides from Google, Uber etc, but yes - some else is redundant;
  2. this pr is huge, maybe it is ok to decouple something;
  3. some if-chains could be written as switches, for instance:

Origin:

if strings.HasSuffix(bnm, "s") {
	return "List of " + bnm
} else if strings.Contains(bnm, "Function of") {
	return strings.ReplaceAll(bnm, "Function of", "Functions of") + "s"
} else {
	return bnm + "s"
}

Result:

switch {
    case strings.HasSuffix(bnm, "s"):
        return "List of " + bnm

    case strings.Contains(bnm, "Function of"):
        return strings.ReplaceAll(bnm, "Function of", "Functions of") + "s"

    default:
        return bnm + "s"
}

or maybe someone would like to introduce a new string var and use single return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants