Back to all reviewers

Consolidate related code

hashicorp/terraform
Based on 5 comments
Go

Organize code to keep related functionality together. When functions serve similar purposes or operate on the same data, combine them rather than creating separate implementations. Position validation and condition checks before execution to allow early returns and avoid unnecessary processing. This improves code readability, maintainability, and reduces...

Code Style Go

Reviewer Prompt

Organize code to keep related functionality together. When functions serve similar purposes or operate on the same data, combine them rather than creating separate implementations. Position validation and condition checks before execution to allow early returns and avoid unnecessary processing. This improves code readability, maintainability, and reduces duplication.

For example, instead of having separate validation functions:

func (n *NodeAbstractResourceInstance) validateIdentity(newIdentity cty.Value) (diags tfdiags.Diagnostics) {
    // Validate marks
    // ...
    return diags
}

func (n *NodeAbstractResourceInstance) validateIdentityMatchesSchema(newIdentity cty.Value, identitySchema *configschema.Object) (diags tfdiags.Diagnostics) {
    // Validate schema
    // ...
    return diags
}

Prefer consolidating them into a single function:

func (n *NodeAbstractResourceInstance) validateIdentity(newIdentity cty.Value, identitySchema *configschema.Object) (diags tfdiags.Diagnostics) {
    // Validate marks
    // ...
    
    // Validate schema
    // ...
    
    return diags
}

Similarly, perform condition checks early in functions to avoid unnecessary work, as seen in discussion 9 where condition checks were moved before execution calls.

5
Comments Analyzed
Go
Primary Language
Code Style
Category

Source Discussions