Skip to content

rule candidate: streq — String.eq/cmp in a recursive search that only needs a bool #101

Description

@noah-emp

Category

suspicious

Shape

String.eq / String.cmp used only for a yes/no in a recursive search (find key, digit limit, …). Those helpers rebuild; a character walk can stop at the first mismatch.

A practical first cut: in a recursive def, String.eq(a, b) (or String.cmp) whose Bool/Cmp is only used to choose between returning a carried value and recursing on a sibling — suggest a local early-exit char walk (or a shared eq.go).

Bad

def find(cells: Json, +key: String) -> Json:
  match cells:
    case JPair{+k, v, r}:
      Lazy.stop(Json, String.eq(k, key), v, _u => find(r, key))
    case _:
      JNull{}

Good

def find.eq(a: String, b: String) -> Bool:
  match a b:
    case SNil{} SNil{}:
      True{}
    case SCon{+x, xs} SCon{+y, ys}:
      Bool.and(Char.is_eq(x, y), find.eq(xs, ys))
    case _ _:
      False{}

def find(cells: Json, +key: String) -> Json:
  match cells:
    case JPair{k, v, r}:
      Bool.pick(Json, find.eq(k, key), v, find(r, key))
    case _:
      JNull{}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestsuspiciousProbably a slip / perf smell

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions