Skill

Enforce Algorithmic Correctness in Code

lemmaly forces stating Big-O complexity, data structure, and algorithm family before writing any loop, query, or recursion, catching O(n^2) and N+1 defaults.


90
Spark score
out of 100
Updated 20 days ago
Version 14.1.0

Add to Favorites

Why it matters

Ensure code adheres to strict algorithmic complexity and data structure rules before it's written or reviewed. This skill acts as a gatekeeper, preventing common performance anti-patterns and ensuring efficient solutions.

Outcomes

What it gets done

01

Audit code for Big-O violations and anti-patterns like N+1 queries.

02

Enforce the use of appropriate data structures and algorithm families.

03

Prevent common performance pitfalls such as await-in-loop or nested loops.

04

Guide developers to state complexity, data structures, and algorithm families before coding.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-lemmaly | bash

Overview

lemmaly - Algorithm-First Proof

lemmaly is the gateway skill in an algorithm-discipline suite that forces stating time/space complexity, data structure, and algorithm family before writing any non-trivial loop, query, or recursion, catching O(n^2) and N+1 defaults before they ship. Use it when writing or reviewing code with loops, joins, or recursion, especially AI-generated code. Do not auto-clone and run the upstream scanner's default branch - pin to a reviewed release commit and use a throwaway directory if running it at all.

What it does

lemmaly enforces an "algorithm-first" discipline: before writing any non-trivial loop, query, join, or recursion, it requires stating time/space complexity, the chosen data structure (with a one-phrase reason), and the algorithm family (e.g. linear scan, two-pointer, sliding window, hash join, BFS/DFS, DP, greedy) - in that order - before code is written. It is the gateway skill in a four-skill algorithm-discipline suite: lemmaly (prevention - think before coding), complexity-cuts (correction - a refactoring playbook for code that's already slow, OOMing, or timing out), invariant-guard (verification - for algorithms where the obvious version is subtly wrong, like binary search variants or in-place dedup, forcing a function contract and loop invariant), and mathguard (acceleration - for n >= 10^6, similarity search, or streaming analytics where classical algorithms hit their asymptotic floor and approximate/math-heavy techniques like Bloom filters or HyperLogLog are needed).

The "Iron Law": no non-trivial code without stated complexity, data structure, and algorithm family. A seven-step pre-write protocol requires stating the problem shape, input dimensions (realistic n), target complexity, data structures, algorithm family, and relevant edge cases (empty, singleton, all-equal, n=1, overflow, duplicates) before the code itself. It presumes repeated work inside loops is wrong until justified: I/O inside a loop, recomputing the same value, re-sorting inside a loop, linear scans (.find/.indexOf/.includes) inside a loop, and materializing intermediate collections just to iterate again are all flagged as needing either a fix or an explicit justifying comment.

// Without the protocol - ships O(n*m)
const active = users.filter((u) => !bannedIds.includes(u.id));

// With the protocol - O(n + m)
// time = O(n+m), space = O(m); structure: Set<string> for O(1) membership; family: linear scan with hashed lookup
const banned = new Set(bannedIds);
const active = users.filter((u) => !banned.has(u.id));

The upstream project also ships an optional deterministic CLI scanner covering 59 rules across 11 languages (JS/TS, Python, SQL, Java, C#, C++, Go, Rust, PHP, Ruby, Shell), each with a documented rationale and incorrect/correct example pair, flagging patterns like js-await-in-for-loop, py-mutable-default-arg, sql-update-no-where, and go-loop-var-capture at critical severity.

When to use - and when NOT to

Use it when writing, editing, or reviewing any code involving loops, collections, lookups, joins, recursion, or graphs - and especially when auditing AI-generated code that "looks idiomatic" but might hide O(n^2) or N+1 behavior. Escalate to complexity-cuts instead when refactoring already-shipped slow code, to invariant-guard when the correctness risk is in a subtle algorithmic trap rather than raw complexity, and to mathguard when n is very large and classical algorithms have hit their lower bound. The skill explicitly warns against automatically cloning and running the upstream scanner's default branch, since that executes whatever code is currently on a third-party repository - if the scanner is wanted, pin to a reviewed release tag or commit, run it in a throwaway directory, and show the resolved commit hash before executing.

Inputs and outputs

Input is a coding task involving iteration, lookup, or recursion over a collection. Output is a stated complexity/structure/algorithm-family declaration followed by code that matches that declaration, or a scanner report flagging specific anti-pattern rule violations by file and line if the optional CLI tool is run.

Integrations

Works as a discipline layer applicable to any language; its optional companion CLI scanner (lemmaly.js scan/rules) supports JavaScript/TypeScript, Python, SQL, Java, C#, C++, Go, Rust, PHP, Ruby, and Shell/Bash.

Who it's for

Developers writing or reviewing code with loops, queries, or recursion - especially those reviewing AI-generated code - who want a forcing function against O(n^2)/N+1 defaults before code ships, rather than catching them after the fact.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.