Over a million developers have joined DZone.

Save Your Reputation - Eliminate Bugs

· Performance Zone

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

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) {
summary.add(value);
}
 
And this one?
public class SharedLock {

private static long count = 0L;

public synchronized void increment() {
count++;
}
}

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) {
g.setColor(Color.red);
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!

Summary

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.

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}