logoalt Hacker News

Show HN: CLI tool for detecting non-exact code duplication with embedding models

46 pointsby rkochanowskitoday at 2:19 PM20 commentsview on HN

Comments

vander_elsttoday at 6:09 PM

I implemented this for a large monorepo last year, it runs as an analysis during code review and it shows what are possible similar snippets wrt the code under review. It was a very nice project. It also allows to see across the repo what are the most common constructs for the different languages. This could also be helpful to see if some code has been copied e.g. from open source projects.

rkochanowskitoday at 2:19 PM

I built Slopo to solve one specific problem: finding similar code that is hardest to detect by other tools, coding AI agents, and humans.

It finds similar-looking code with embeddings. This detects more than just copy-paste clones or even clones with minor changes. Similar code is often not a clone to refactor, and this is a trade-off. Initial results need to be verified, but coding agents can do this quickly. Example prompts are available on https://slopo.dev

Additionally, similar code distant in the codebase is ranked higher to focus on less obvious duplication.

The results differ a lot depending on the codebase. I noticed that sometimes most of the detected duplicates are false positives, but the remaining ones are strong candidates to refactor or even bugs. Sometimes it reveals much more real duplication.

show 2 replies
rohanattoday at 6:06 PM

have you considered a deterministic tier before the embedding pass? I feel that approach can be more efficient.

show 1 reply
muratstoday at 3:28 PM

Nice idea. I can see this being useful before refactors, especially when the duplication is semantic rather than copy paste.

philajantoday at 4:40 PM

This is neat. Have you noticed any difference in duplicate detection between strongly typed and loosely typed languages / code bases?

show 1 reply
BrandiATMuhkuhtoday at 4:59 PM

What a simple and smart idea. Wonderful

forhadahmedtoday at 5:13 PM

self plug (for similar tool): https://github.com/forhadahmed/refactor

hdztoday at 4:36 PM

Very nice. I can imagine putting this into a pre push hook to keep things clean after an initial sweep.

NYCHMPAItoday at 3:26 PM

This is a great use case for embeddings. Code deduplication across distant modules is notoriously hard for traditional AST-based tools.

How do you handle chunking and parsing for different languages to make sure the embeddings capture semantic meaning effectively? For instance, do you chunk by functions/classes, or use a fixed token window? If a function is too long or too short, it can drastically skew the embedding similarity.

SpyCoder77today at 4:46 PM

I think that this is pretty cool, but is there any reason why we would want to remove similar/possible duplicate code?

show 3 replies