structfield

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 4 Imported by: 0

README

GoDoc

Structfield

Find struct literals using non-labeled fields.

The structfield analysis reports the usage of struct literal using non-labeled fields more than defined limit (default: 2). The non-labeled field above the limit considered has higher cognitive load (harder to understand and rememeber).

Understanding Struct Literal

Given code, variable assigned using struct literal:

acc := Account{
    "john.smith@example.com",
    "John Smith",
    []Permission{
        Permission{"account", "read"},
        Permission{"account", "write"},
    },
    true,
    false,
}

Above code is harder to understand, hard to guess the field name since we have to remember exact order of the fields. The workaround is you have to always look the declaration of the Account type.

Suggestion is to refactor the code to:

acc := Account{
    Email: "john.smith@example.com",
    Name: "John Smith",
    Permission: []Permission{
        Permission{"account", "read"}, // Non-labeled here is still ok
        Permission{"account", "write"},
    },
    Verified: true,
    Deactivated: false,
}

The limit set to 2, which considered easy to understand and remember.

Benefits

By using the labeled fields you several benefits

  1. The fields doesn't have to be in order
  2. You don't have to declare the value if it's a default value

Example:

acc := Account{
    Email: "john.smith@example.com",
    Name: "John Smith",
    Permission: []Permission{
        Permission{"account", "read"}, // Non-labeled here is still ok
        Permission{"account", "write"},
    },
    Verified: true,
    Deactivated: false,
}

can be simplified into:

acc := Account{
    Name: "John Smith", // `Name` come first
    Email: "john.smith@example.com",
    Permission: []Permission{
        Permission{"account", "read"}, // Non-labeled here is still ok
        Permission{"account", "write"},
    },
    Verified: true,
    // Remove the `Deactivated: false` since it use default value
}

Installation

$ go install github.com/uudashr/structfield/cmd/structfield@latest

or

$ go get github.com/uudashr/structfield/cmd/structfield

Usage

$ structfield -limit 2 testdata/src/a/*.go
testdata/src/a/a.go:20:9: Found 4 non-labeled fields on struct literal (> 2)

Documentation

Overview

structfield provides the ability find the struct literal using non-labeled fields.

Index

Constants

View Source
const Doc = `` /* 284-byte string literal not displayed */

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "structfield",
	Doc:      Doc,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
	Run:      run,
}

Functions

func CountNonLabeledFields

func CountNonLabeledFields(lit *ast.CompositeLit) (ok bool, n int)

CountNonLabeledFields count usage of non-labeled fields in struct literal, returned as n. If the lit is not struct literal, then it will return ok as false.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL