Skip to content

Add comparison helper methods to improve readability #56

Description

@thep2p

Problem

Current comparison checks are verbose and repetitive:

cmp := neighborID.Compare(&target)
if cmp.GetComparisonResult() == model.CompareGreater || cmp.GetComparisonResult() == model.CompareEqual {
    // ...
}

This pattern appears multiple times in the codebase (e.g., node/node.go, node/search_by_id_test.go) and reduces readability.

Proposed Solution

Add helper methods to the Comparison struct in core/model/identifier.go:

// IsGreaterOrEqual returns true if the comparison result is greater or equal.
func (c *Comparison) IsGreaterOrEqual() bool {
    result := c.GetComparisonResult()
    return result == CompareGreater || result == CompareEqual
}

// IsLessOrEqual returns true if the comparison result is less or equal.
func (c *Comparison) IsLessOrEqual() bool {
    result := c.GetComparisonResult()
    return result == CompareLess || result == CompareEqual
}

// IsGreater returns true if the comparison result is greater.
func (c *Comparison) IsGreater() bool {
    return c.GetComparisonResult() == CompareGreater
}

// IsLess returns true if the comparison result is less.
func (c *Comparison) IsLess() bool {
    return c.GetComparisonResult() == CompareLess
}

// IsEqual returns true if the comparison result is equal.
func (c *Comparison) IsEqual() bool {
    return c.GetComparisonResult() == CompareEqual
}

Benefits

  • Improved readability: cmp.IsGreaterOrEqual() vs cmp.GetComparisonResult() == model.CompareGreater || cmp.GetComparisonResult() == model.CompareEqual
  • Maintains debug information from the Comparison struct
  • More idiomatic Go code
  • Reduces potential for errors in comparison logic

Files to Update

  1. core/model/identifier.go - Add helper methods
  2. node/node.go - Update comparison checks
  3. node/search_by_id_test.go - Update comparison checks
  4. Any other files using similar patterns

Alternative Considered

Using bytes.Compare() directly is more performant for simple comparisons, but loses the debug information that the Comparison struct provides.

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