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

[OTHER] refactor the instance resource (happy path left) #331

Open
fernando-villalba opened this issue Aug 20, 2024 · 0 comments
Open

[OTHER] refactor the instance resource (happy path left) #331

fernando-villalba opened this issue Aug 20, 2024 · 0 comments

Comments

@fernando-villalba
Copy link
Contributor

Description

The instance resource code could do with some improvement to make it more readable. Do not do this until the test issue #330 has been implemented to ensure we don't introduce additional bugs as part of the refactoring process.

This ticket focuses on keeping the happy path left and avoiding nesting.

Most of the time else statements are not necessary. case is sometimes better, but even it is sometimes overused, so think carefully about the logic and avoid nesting as much as possible.

Also we should be careful not to next too many if statements on top of the other:

For example, this block starting here is very hard to read as it goes four levels in with if statements and I am pretty sure we can rewrite it for clarity.

Then there are simpler examples like this:

if resp.PublicIP != "" {
	d.Set("public_ip_required", "create")
} else {
	d.Set("public_ip_required", "none")
}

Which can probably be rewritten like this without any adverse effects (verify this statement) and achieve the same result more elegantly:

d.Set("public_ip_required", "none")
if resp.PublicIP != "" {
	d.Set("public_ip_required", "create")
}

This is not about NEVER using else, but I think in most cases you can totally do away with it.

For more info, please read this:

https://dave.cheney.net/2020/02/23/the-zen-of-go#:~:text=Return%20early%20rather%20than%20nesting%20deeply

Acceptance Criteria

There is much that can be refactored, but to complete this ticket, I'd suggest we focus on refactoring to keep the happy path left, that is:

  • Get rid of most, if not all if statements.
  • Don't have multiple levels of nesting, it's very hard to wrap your head around when you are three levels into an if statement. Try and avoid this.
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

No branches or pull requests

1 participant