Awesome Reviewers

Reviewer · Algorithms

Data structure design

When designing algorithms and data structures, favor the simplest structure that correctly and efficiently meets current needs, and make complexity and invariants explicit rather than implicit.

Instruction

When designing algorithms and data structures, favor the simplest structure that correctly and efficiently meets current needs, and make complexity and invariants explicit rather than implicit.

  • Avoid premature optimization: a plain map is fine for small/internal data sets; only switch to a denser structure (e.g. a bitset) when profiling or scale actually justifies it. Don’t “boil the ocean” for code that isn’t performance-critical.
  • Consolidate data that must stay in sync. If you find yourself maintaining two parallel slices/maps (or a map + a slice) where indexes or keys must line up, wrap them into a single struct/type instead so the relationship can’t drift out of sync. For example, instead of:
    root    []*[]digest.Digest
    content []*[]ocispec.Descriptor // must stay same length/order as root
    

    prefer:

    type sharedSizeData struct {
        chainIDs []digest.Digest
        content  []ocispec.Descriptor
    }
    

    Similarly, prefer a single map[string]int (id -> size, with a sentinel for “seen”) over a separate “seen” set plus a results slice.

  • Surface non-obvious complexity. If a function’s behavior silently becomes O(n*m) (e.g. by matching against every parent path), rename it or document it clearly (e.g. MatchesOrAnyParentMatches) so callers can decide whether a cheaper alternative is appropriate for their use case.
  • Double-check termination and boundary conditions in iterative/stateful algorithms (e.g. time or index “jumps”). Verify the exit condition can’t regress to a previous or equal state, which would cause an infinite loop — add a test case specifically for the boundary value.