How to Identify Similar Pieces of Code
Tags: haskell, javascript, python, staticcodeanalysis
Published: 2020-07-21 11:04:15

---
title: How to Identify Similar Pieces of Code
published: true
description: How to not repeat yourself. An article describing open source project in Haskell that identifies similar pieces of code.
tags: Haskell, JavaScript, Python, StaticCodeAnalysis
canonical_url: https://medium.com/@ch3rn0v/how-to-identify-similar-pieces-of-code-43d2a5cef5a6
cover_image: https://miro.medium.com/max/913/1*kattOlOGBha9cRweq38MEA.png
---

# The Goal

It’s best to achieve the desired result with as little a codebase as possible.
The fewer entities there are in a codebase, the easier and cheaper it is to familiarize oneself with it, support it and build upon.

However, as a project and its team grow, so does the risk of accidentally introducing identical or similar pieces of code. Even more so with iterations of automatic code generation.

[DRY](https://github.com/ch3rn0v/dry) is a project I built to identify such similar pieces (okay, fine, and to play with Haskell).

# Considered Approaches

It isn’t a trivial task to show deterministically that two (potentially different) pieces of code are identical, meaning they always produce the same output for the same input. In fact one has to make a proof for such a claim or its negation.

Another approach is based on heuristics. This is the path chosen for DRY. Such an approach has its downsides: one can easily create two functions that can fool any given set of metrics, and still prove the functions to be identical. Yet when building a project, the mentioned vulnerability can usually be safely ignored. Because of all the best practices, naming conventions, style guides, automatic syntactic tests and so forth it is likely that identical functions will have identical names, arity, statements, their count and depth. Thus I decided to give this approach a try.

# Metrics

A set of metrics is calculated for each unique pair of functions. Every metric is bound to the range of [0; 1]. Here are the metrics themselves.

1. [Levenshtein Distance](https://en.wikipedia.org/wiki/Levenshtein_distance) between the names of two functions. More precisely, `1 / (1 + LD)` where LD is Levenshtein Distance.

2. Functions’ Arity Difference: one if both have zero arity, otherwise a division of a lesser value over the greater one.

3. Functions’ Statements Difference: _the same_ statements are counted on each depth level and considered separately from the statements of different kinds, unique statements are also accounted for. The deeper the less important the difference is. The more unique statements any of the two functions has, the less similar the functions are (and vice versa).

4. Functions’ Statements Total Count Difference, counted in the fashion similar to that of arity: one if both have zero statements, otherwise a division of a lesser value over the greater one.

Finally, for every pair of functions the above scores are multiplied by a vector of weights. Then the average of the values is calculated, and that’s the similarity score of a given function pair.

One might wonder, why would we need the fourth metric (total statements count difference) if there already is the third one (accounting for the difference in statements count, grouped by statement type and depth level). If the functions’ bodies consist of similar statements, the fourth metric is perhaps excessive. However if they consist of different statements, there still might or might not be similarity in the amount of statements used to define them. The fourth metric accounts for that.

The final metric (as the four above) is bound to [0; 1] where 1 is the same and 0 is completely different.

# Results

I executed DRY against a number of repos, and created this visualization with Python:

![](https://miro.medium.com/max/913/1*7g68n_0GtMRlmEU-Rmarpg.png)
![](https://miro.medium.com/max/913/1*kattOlOGBha9cRweq38MEA.png)

Interestingly, most similar functions are the ones with similar total count of statements. This is merely the consequence of how the score is calculated ([see the weights vector](https://github.com/ch3rn0v/dry/blob/master/src/Analyser.hs#L148)). What’s also worth noting is that there are functions with hundreds of statements. For instance, here is [Vue’s `createPatchFunction`](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/core/vdom/patch.js#L70) with 3831 statements.

Vue is a project big enough to illustrate the whole spectrum of possibilities. Let’s take a look at exactly the same functions first, there is quite a number of them.

First here is `createOnceHandler` for [one platform](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/web/runtime/modules/events.js#L32) and for [another](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/weex/runtime/modules/events.js#L7).

Then `genData` for [class](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/web/compiler/modules/class.js#L34) and [style](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/web/compiler/modules/style.js#L37). This is a peculiar case. According to the metrics, these functions are absolutely the same (with a score of `1.0`). In practice however they are not: string templates differ a bit, so do object property names. It is technically possible to account for such kinds of differences. Whether it makes practical sense is another question.

There are other similar instances of `genData`, such as [this one](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/weex/compiler/modules/class.js#L38). In total six unique pairs of `genData`s were found. This gives us four distinct `genData` functions similar to each other (since the pairs are unique and order within a pair is insignificant).

One more example of a pair of similar functions is `generate`. I would assume [this one](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/server/optimizing-compiler/codegen.js#L39) has to do with server-side rendering, while [this one](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/compiler/codegen/index.js#L43) is for the client’s side.

Alright, how about _almost_ similar functions. The following pair of functions has the similarity score of `0.952695949955943` and statement counts are `105` and `100` respectively: [`transformNode`](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/weex/compiler/modules/style.js#L18) and [`transformNode`](https://github.com/vuejs/vue/blob/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7/src/platforms/weex/compiler/modules/class.js#L15). Notice how in the latter case it is tested whether `staticClass` evaluates to `true`, while `staticStyle` is not tested, even though the way they are retrieved is the same: using `getAndRemoveAttr`.

There is the total of 167,331 unique pairs for [the repo](https://github.com/vuejs/vue/tree/8ead9d2a0d4ca686eaf5e35526eff4af1b8c79a7), with the pair of the least similar functions having the score of `0.002267573696145`. I invite the reader to see for themselves what these functions are.

# Conclusion

As it is shown above, [DRY](https://github.com/ch3rn0v/dry) can be used not only to identify similar functions in order to abstract them away and thus alleviate any repetition in the codebase, but also to spot inconsistency which may help discover a bug. Hopefully you find the tool useful. Please feel free to try it, point out any issues and potential ways to improve it.

Thank you for reading!