Skip to content

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

@fernando-villalba

Description

@fernando-villalba

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.

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