IntelliJ IDEA 15 EAP Adds On-the-Fly Code Duplicates Detection

It’s been a while since we added the Analyze → Locate Duplicates feature. If you’ve had a chance to use it, you know how helpful it is: by eliminating duplicates in your code, you make it more concise and stable. If you’ve never used it, you definitely should; it’s truly surprising how many duplicates an average codebase usually contains.

The fresh IntelliJ IDEA 15 EAP build released today takes the duplicates analysis to an entirely new level by allowing you to spot them on-the-fly. Now, if you stumble upon an existing duplicate, or somehow create one either by writing or pasting code, you will know it instantly:

v15_duplicates_1

The analysis comes with quick-fixes, so you can navigate to duplicates or have them shown in a tool window:

v15_duplicates_2

The new inspection is called Duplicated Code and is enabled by default. To toggle it, go to SettingsEditor → Inspections. And, of course, you can run it on the entire project (or a custom scope) any time via Run Inspection by Name.

Note that this feature is currently available only for Java. As the work is still in progress, rare false positives are possible.  Don’t forget to report them as well as any other bugs to our tracker.

You’re welcome to download IntelliJ IDEA 15 EAP right away, give this new feature a try, and share your feedback.

Among other enhancements, you’ll find fixes for:

  • Big and blurry fonts and icons in the Editor on Linux
  • Not working Terminal on Windows 10

Develop with Pleasure!

About Andrey Cheptsov

Part-time dreamer. JVM/JavaScript geek. IntelliJIDEA Marketing Manager at JetBrains.
This entry was posted in New Features and tagged , , . Bookmark the permalink.

24 Responses to IntelliJ IDEA 15 EAP Adds On-the-Fly Code Duplicates Detection

  1. Oleg says:

    Could you please add a way to configure this inspection similar to how you can configure “find duplicates” action from the menu? For example, I’d like to change “do not show duplicates smaller than” and “anonymize common subexpressions smaller than” parameters for it.

  2. Arteom Sedykh says:

    I’ve just installed new IDEA 15.0, and I cant find Analyze → Locate Duplicates neither Settings → Editor → Inspections. What should I do to access these features?

  3. Lenny Krueger says:

    just found the setting, it’s in File –>Settings –> Editor –> Inspections Once the Inspections menu opens, it’s under General –> Duplicated Code

  4. Gordon Child says:

    Is there a way to disable code duplicates detection for tests? In some cases, I’d prefer to not abstract code when writing tests and don’t mind duplicate code.

  5. Is there a way to configure the threshold (now defaulted to 5 lines) when two code blocks are classified as duplicate?

  6. David says:

    Just downloaded Intellij15 and try to test this out, but when I go to File->Settings->Editor->Inspections and search for Duplicated Code I can’t seem to find it.

    Was this options moved?

  7. ILya says:

    It would be convenient to have possibility to define exact methods, or method templates, for which duplicate detection will be disabled.

    For example hashCode, equals, toString methods are usually auto generated, and Idea often fires “duplication warning” in the places.

  8. Chris says:

    1. Shows duplicates warning where duplicates do not exist. Alt+Enter>show duplicates like this: only one usage found
    2. Balloon shows message “Found duplicated code in module/src/main/java/com…” even when I press ctrl+F1. Showing just the class name/es would be more helpful.

    • Maksim Sobolevskiy says:

      Hi, Chris! Could you provide more information about pint 1?
      Give us an example, please.

      • Chris says:

        Hi,

        Not sure what kind of information could provide, given that this depends on the rest of the codebase, not the class in question.

        There is a method in one of my classes where all lines of the body are marked as duplicate.

        When I Alt+Enter>show duplicates like this: I get “only one usage found” and the body of the method is highlighted.
        I did search in the hole project for parts of the method but didn’t find any other occurrences.

        It would be helpful if I was given the class name instead of the beginning of it’s path in the balloon.

  9. Brooke says:

    This is a great feature. Though I find it gets in the way sometimes and I’d like to change from showing a squiggly line under every line to just the first (or similar). For example I’ve copied code from another dev with the intention to refactor for my purposes though I want to read through it first to understand the changes I need to make. The squiggles everywhere is a bit like someone shouting at me! I don’t want to turn off the feature nor suppress it. I’d just like it to collapse temporarily (until file is closed or committed).

  10. Zsolti says:

    Can I suppress the duplicate code marking for just one particular occurrence?

  11. Cuero Qu says:

    Find an example of ‘Show duplicate warning but actually it’s not duplicate’. Here’s the code:
    method test and test2 are reported as duplicates code, while the parameters to inner class Test are different.

    public class TestMain {
    int anInt = 1;
    public void test(int a)
    {
    TestMain testMain = new TestMain();
    System.out.println(“haha”);
    System.out.println(anInt);
    System.out.println(“hahaa”);
    new Test(testMain::test);
    System.out.println(anInt);
    System.out.println(“hahaaa”);
    }

    public void test2(int a)
    {
    TestMain testMain = new TestMain();
    System.out.println(“haha”);
    System.out.println(anInt);
    System.out.println(“hahaa”);
    new Test(testMain::test2);
    System.out.println(anInt);
    System.out.println(“hahaaa”);
    }

    public class Test {
    Test(handler h) {

    }
    }

    public interface handler {
    void entitySelector(int a);
    }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *