duplicated-data
The same literal values are written out in more than one place.
- Group
- Code
- Runs
- On by default
- Level
- warning
- Reads
- Every file
- Fix
- None: which collection to keep, and how the others refer to it, is a design choice
Why
These collections, such as lists, map keys, or the labels a match dispatches on, hold the same values, though perhaps in another order or with a value missing. Or these functions write the same particular constant, such as a hash's prime, which usually means each carries its own copy of one algorithm. Copies must change together and drift apart when they do not. Define the values once and refer to them, or derive one collection from the other.
The fix, as Ur suggests it: define the values once and refer to them, or derive one collection from the other.
For agents
Before writing out a set of literal values or a particular constant, look for a definition that already holds them and refer to it instead.
Example
app/currencies.pySUPPORTED_CURRENCIES = ["USD", "EUR", "GBP", "JPY", "CHF"]
app/pricing.pydef decimals(currency):
if currency in ("USD", "EUR", "GBP", "CHF"):
return 2
raise ValueError(f"unsupported currency: {currency}")
$ ur lint --rule duplicated-data
warning[duplicated-data]: 2 collections hold the same values: "USD", "EUR", "GBP", "JPY", "CHF"
--> ./app/currencies.py:1:24 list `SUPPORTED_CURRENCIES`
--> ./app/pricing.py:2:20 list `decimals` lacking "JPY"
= hint: define the values once and refer to them, or derive one collection from the other
= see: Data Clone Detection and Visualization in Spreadsheets, Felienne Hermans, Ben Sedee, Martin Pinzger, and Arie van Deursen, ICSE 2013 (2013-05), https://doi.org/10.1109/ICSE.2013.6606575
0 clone classes and 1 set of duplicated data in 2 files
Source
Data Clone Detection and Visualization in Spreadsheets, Felienne Hermans, Ben Sedee, Martin Pinzger, and Arie van Deursen, ICSE 2013 (2013-05).