Save Your Reputation - Eliminate Bugs

DZone 's Guide to

Save Your Reputation - Eliminate Bugs

· Performance Zone ·
Free Resource

Are you a professional developer? Now here's a question for you – who typically finds bugs in your code? Yourself, your pair-programming mate, your colleagues during a code review, the continuous integration system or your customer?

A teaser - how far would this code get in your code-chain before the bug gets detected?


final BigDecimal summary = BigDecimal.ZERO;

public void add(final BigDecimal value) {
And this one?
public class SharedLock {

private static long count = 0L;

public synchronized void increment() {

The bugs that make it into the production are obviously the most embarrassing and most expensive ones to fix. But even the bugs that get discovered earlier in the line still have a price and reputation tags attached to them.
Practices like unit testing, debugging or pair programming can eliminate a great deal of problems. Functional bugs and internationalization or code style issues can be quite reliably detected and localized using these techniques.
However, when it comes to verification of multithreaded correctness, security vulnerabilities, potential performance bottlenecks or bad practices, these tools and techniques quickly show their limits.

Of course, I have a solution up in my sleeve to show just now, when I've damaged your temper and you almost feel that bugs can be mostly detected only by accident. Before I do my magic trick, check out the following piece of code. Can you spot a bug in there?

* Fills given region with red color
private void paintRed(final Rectangle clip, final Graphics g) {
if (clip!=null) return;
g.fillRect(clip.x, clip.y, clip.height, clip.width);

Did you find the bug? Was it easy? Well done! And how about the second bug?  There are actually two bugs in the code. Could you try again?

Programmers' Nirvana

And here we come with the solution. Take a good look at the following couple of screenshots:

Do you recognize the examples?

Now it is much easier to spot problems, right? Isn't it cool to have the issues highlighted right in the code? Meet Static Code Analysis – a tool that carefully reads your code just to find suspicious pieces and throw them back at you. Either nicely highlighted in the editor

or consolidated into an interactive report, which offers several options for actions.

Behind the scenes you could see that IntelliJ IDEA parses your source code into an Abstract Syntax Tree (AST) and updates the AST each time you touch the keyboard to give you most relevant notification at any moment.

Abstract Syntax Tree

The background Static Code Analysis engine repeatedly compares the AST of your code with a database of about 700 (seven hundred!) patterns of known issues, bugs or styling problems and highlights each occurrence of any of the bugs in your code for you to see them.

Fingers ready, let's start fixing

Looking at a highlighted problem, press Control + F1 and you get a detailed description of what bad and ugly would happen if you didn't sort out the mess you or someone else has created.

Some issues can be automatically quick-fixed for you. Press Alt + Enter and choose the fix from the pop-up menu.

Pressing the Right key while in the menu will give you options to invoke the appropriate quick-fix on all bug occurrences, to suppress detection of the bug in a given scope or to disable the bug detection altogether. So you can quickly customize the list of bugs you want the engine to search for.

You can also jump right to the analysis configuration screen and edit the inspection profile settings directly.

Choose which bugs should be checked for in which part of your project. Very flexible. And powerful!

More to come

Static Code Analysis in IntelliJ IDEA is not limited to Java. There are other interesting technologies that can benefit from this technique – HTML, SQL, CSS, Hibernate, JPA, GWT to name a few.
And you can also teach IntelliJ IDEA your own bug patterns, specific to your concrete project.

But more on all that later. Subscribe to the JetBrains Zone rss feed and stay tuned!


I've shown you how to sweep bugs away from your code right at the beginning of the whole code chain - long before you save the file to the disk the first time. We've covered the principles and usages of Static Code Analysis and shown some of the options that are available in IntelliJ IDEA, honestly one of the sharpest bug-finding pair-programming mate you can get today.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}