174

Is it possible to disable duplicate code detection in IntelliJ?

I haven't found this feature to be useful and it continues to distract me.

informatik01
  • 16,038
  • 10
  • 74
  • 104
zudduz
  • 2,457
  • 5
  • 24
  • 29

5 Answers5

239

Only available in IntelliJ Ultimate:

To disable duplicate code detection, go to

File → Settings → Editor → Inspections → General → Duplicated code fragment

and uncheck box "Duplicate code fragment". intellij code duplication

Community
  • 1
  • 1
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
  • 6
    This option is not available in IDEA community edition. – Saikat Mar 18 '17 at 16:35
  • 6
    @takias duplicate code detection isn't a feature in the Community edition at all. It's only present in the Ultimate edition. – AER Oct 09 '17 at 22:19
  • 4
    Actually it works after you close the file you work on and reopen it or restart the IDE after you apply these settings – ACV Mar 05 '19 at 09:32
  • 1
    The suggested actions should offer to take us to this screen so we can disable these code checks easier. – Shadoninja Oct 15 '19 at 22:10
  • The same recipe works for PyCharm Professional – akpp Jan 25 '22 at 23:53
168

Add a hint to your code so that others will know your intent:

@SuppressWarnings("Duplicates")
Vik David
  • 3,640
  • 4
  • 21
  • 29
Max Barrass
  • 2,776
  • 1
  • 19
  • 10
15

Yes, it's possible, but I would strongly advise against it!

Duplicate code is a form of technical debt. Any duplicated code that contains a bug means you now have a duplicated bug - you then run the risk that when you fix it, you'll only fix it in one place and the duplicate will remain...

If duplicate code warnings are distracting you, then the best strategy for getting rid of them is to remove the code duplication... Your codebase and future maintainers will thank you for it

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
  • 53
    OK, but sometimes I have the old version and the new version of the same class. There duplication is normal, I don't need the warning. It is just very intrusive and hides many other useful warnings. The question is legitimate. – Florian F Nov 14 '16 at 11:45
  • 2
    Agreed it is a smell. I however see that IntelliJ freezes for a few seconds while it detects the duplicate code. This for me is quite annoying. – codematix Jun 24 '17 at 13:07
  • 5
    I find it fires erroneously for unit tests with similar setups. Sometimes I can't get it to go away even if I do change the tests fairly significantly. – Julian Jocque Mar 12 '18 at 15:57
  • 1
    If you have a similar unit test setup, you might put the setup in a separate method? – vatbub Mar 28 '18 at 00:25
  • 9
    Its not always as simples as get rid of all duplicates! For example in web controllers there is a lot of similarity which flags as duplicates they are not exact duplicates. The benefits of keeping your controllers logically separated is useful in many ways and that is just one example. Pragmatic not purist gets projects delivered in my experience. – Shawn Vader Jan 25 '19 at 08:56
  • 6
    You claim it is possible and yet you don't describe how. There are plenty of situations where it makes no sense to try and abstract. Please at least say "Yes, here is how, BUT ...." – Dilapidus Jan 31 '19 at 19:58
  • 4
    this answer is not related to the question at all. – ACV Mar 05 '19 at 08:26
  • 1
    I am getting duplicaties warnings because some lines, within different methods, are the same. For example: user.getName is occurring in multiple methods. That's annoying and wrong. – aki Apr 01 '19 at 19:00
  • 2
    It is ok to have duplication in unit tests (for the sake of readability), so I would disagree here. – silentser Feb 13 '20 at 08:21
  • It's also not true. 'Duplicate code' as defined by Intellij leads to premature abstraction and scattering crap all over the place. The only thing worse than duplicated code is a developer who physically can't duplicate any code and creates a crappy abstraction instead. – Josh Alexy Feb 05 '21 at 18:29
  • I mean that's true if it were working properly, I have duplicated code warnings for comments that sound similar but are different meanings, it's so annoying – Olli Jun 08 '21 at 16:08
  • How can I hide the duplicates display temporarily to complete the task and return to de-duplication later? It's VERY annoying to see the purple duplicates highlight while trying to concentrate. – Jari Turkia Aug 05 '21 at 16:12
  • What is the point to highlight duplications in Imports? I have to use the same method in other places, so it highlights duplication for import something, doesn't make any sense in this case. – Roger Oliveira Feb 16 '22 at 03:37
9

This answer may be little irrelevant, but I found this helpful, From this answer if you want to disable it for a specific code block, not the entire method or class or ide, then just add the following line just before that code block

//noinspection Duplicates

Note: You can not put any other comment after this line.

Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
0

Could not get any of these answers to work in Webstorm.

To work around this without turning this feature off, I added this line of code:

if (Math.random() === 1) console.info('suppress duplicate code warning')

This makes the warning disappear if you add it in 1 of the 2 dupe files since they are not dupe anymore.

Another alternative if you are using lodash is:

noop('suppress duplicate code warning')
danday74
  • 52,471
  • 49
  • 232
  • 283