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

[audit]: x/gov/client/cli: PromptMetadata has non-idiomatic code pattern creates variables up top yet could perform an early return #17849

Closed
1 task done
odeke-em opened this issue Sep 24, 2023 · 0 comments · Fixed by #17859
Labels
Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.

Comments

@odeke-em
Copy link
Collaborator

Is there an existing issue for this?

  • I have searched the existing issues

What happened?

While auditing the v0.50.0.* code I noticed this pattern
Screenshot 2023-09-24 at 3 14 00 PM

func PromptMetadata(skip bool) (types.ProposalMetadata, error) {
var (
metadata types.ProposalMetadata
err error
)
if !skip {
metadata, err = Prompt(types.ProposalMetadata{}, "proposal")
if err != nil {
return metadata, fmt.Errorf("failed to set proposal metadata: %w", err)
}

that pattern is very non-Go and more C-like, notice that we have harder code to read just to avoid initialize 2 variables, and end up throwing err all around as if that variable needs to be captured and reused in both branches of the if statement.
Really that can be fixed by early returns https://cyber.orijtech.com/scsec/cosmos-go-coding-guide#reduce-code-nesting-with-early-returns-and-inversions

func PromptMetadata(skip bool) (types.ProposalMetadata, error) {
	if !skip {
		metadata, err := Prompt(types.ProposalMetadata{}, "proposal")
		if err != nil {
			return metadata, fmt.Errorf("failed to set proposal metadata: %w", err)
		}
		return metadata, nil
	}

	// prompt for title and summary
	titlePrompt := promptui.Prompt{
		Label:    "Enter proposal title",
		Validate: client.ValidatePromptNotEmpty,
	}

	title, err := titlePrompt.Run()
	if err != nil {
		return metadata, fmt.Errorf("failed to set proposal title: %w", err)
	}

	summaryPrompt := promptui.Prompt{
		Label:    "Enter proposal summary",
		Validate: client.ValidatePromptNotEmpty,
	}

	summary, err := summaryPrompt.Run()
	if err != nil {
		return metadata, fmt.Errorf("failed to set proposal summary: %w", err)
	}

	return types.ProposalMetadata{Title: title, Summary: summary}, nil
}

/cc @elias-orijtech

Cosmos SDK Version

v0.50.0-rc.0

How to reproduce?

No response

@odeke-em odeke-em added the Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity. label Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Code Hygiene General cleanup and restructuring of code to provide clarity, flexibility, and modularity.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant