There are many developer best practices that should be followed – one of these is Don’t Repeat Yourself (DRY). It’s quite simple, don’t do something twice if you don’t have to.

This post looks at how Android developers will be able to improve code quality by using duplication detection tools.

Following DRY is great for several reasons:
1. You save time by not writing the same code over again
2. When the software changes, you only have to update the relevant code in one location

To find out how well you are adhering to this principle, you need to be able to find out if you have any duplicate code. This is where Android developers run into a problem. There is currently no support in Android Studio for duplicate detection.

Android Studio is an Integrated Development Environment (IDE) that helps developers build Android applications. It is based on IntelliJ IDEA, which is an IDE developed by JetBrains. You may have heard of some of their other products such as PHPStorm, PyCharm and RubyMine.

The great news is that code duplication detection will be available in IntelliJ IDEA 15. On the fly. Yes, really.

On the fly duplication detection

On the fly duplication detection

However, static analysis tools like these still have problems. One of the problems that face static analysis tools is false-positives. False-positives are issues that are flagged by a tool, but aren’t really an issue (or at least not enough to worry about). Issues like these waste the developer’s time and can soon result in the tool not being used anymore. Most static analysis tools help alleviate this issue by allowing filtering and thresholds to be set.

IntelliJ’s solution to this common problem with static analysis tools is to provide a “cost” of the issue. It derives this cost from the number of lines that were repeated and how many times they were repeated. This can give you a good indication of how much of an impact this has and may help you prioritise issues. The tool will only show duplicates that exceed the cost that you set, resulting in much less false-positives being found.

An eliminate duplicates button is available in the duplicates tool window that allows you to extract the duplicated code into a method with a single click.

This feature is particularly effective for code reviews, and development in general as these issues can be detected earlier on in development. This is great because bugs cost more as the project progresses. Finding bugs earlier on in the lifecycle is very beneficial as these changes cost much less than fixing them after the software has been released.

 

 

Cost of bug fixes / change over time

Cost of bug fixes / change over time

These features should trickle down into the Android Studio builds soon and you can begin to reap the benefits.

Got any questions or interested to know more? Tweet us @Createful