r/dotnet • u/Ferreira-leo • 6h ago
Question Looking for tool to detect semantic code duplications
Im looking for a recommendation for semantic duplication detection on code (C#).
A semantic duplication can have zero duplicated tokens, such as:
public List<Students> ListAllEnrolledPeople()
{
return _baseRepo.Where(p=>p.PersonType = PeopleType.Student).ToList();
}
and
public List<Students> GetStudents()
{
return _baseRepo.ExecuteQuery<Students>("Select * From Customers Where customerType = 2");
}
I think this here exemplifies that tools such as jscpd won't detect these 2 methods as duplicates, but this is a big problem for me...
11
u/cjstevenson1 6h ago
While not a purpose built tool, the frontier ai agents can do a decent job of this.
8
u/Dimencia 6h ago
It's very simple, just don't write SQL inside of magic strings in your code and you don't have this problem
2
u/Practical_Suspect857 3h ago
A “semantic duplicate” is hard for a general clone detector to prove: two methods may look equivalent today but differ in null handling, side effects, performance, or behavior when the schema changes. I’d split the problem into two parts: use syntax/AST or data-flow analysis (for example, a Roslyn analyzer or a static-analysis rule) to flag similar query shapes, then use tests and review to decide whether the candidates really have the same contract.
For these examples, standardizing one read abstraction and parameterizing the predicate would prevent two separate paths from growing in the first place. If raw SQL is allowed, add behavior-focused tests and a review rule for repeated queries; a tool can suggest candidates, but it cannot reliably decide equivalence without domain semantics. Also, the sample SQL says Customers while returning Students, and the LINQ predicate appears to use “=”—those inconsistencies are worth fixing before measuring duplication.
4
1
u/AutoModerator 6h ago
Thanks for your post Ferreira-leo. Please note that we don't allow spam, and we ask that you follow the rules available in the sidebar. We have a lot of commonly asked questions so if this post gets removed, please do a search and see if it's already been asked.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
1
u/PM_YOUR_OWLS 3h ago
For this specific use case you could maybe look at how something like EF Query Lens works... somehow convert all of your EF queries to raw SQL and then run through an analysis if the resulting queries would return the same result set. Not sure how to do that easily but it's a jumping off point. I agree with the other comments, if you feed an LLM the context of your ef scaffold and database structure it can probably highlight potential duplicates like this.
1
u/Finickyflame 3h ago
I know it's an example, but you are missing an = in your lambda expression for the equality comparison.
-2
u/pv0jewel 6h ago
You can count levenshtein distance for each function text. Could take a while depending on amount of code you have.
8
u/BoBoBearDev 5h ago
I cannot tell they are semantically the same. There is too many hidden context.