JetBrains IntelliJ IDEA: Explore Your Program with Static Analysis
JetBrains IntelliJ IDEA
Explore your program with Statistic Analysis
When writing code, we constantly receive assistance from static analysis. Invisible mechanisms work behind the scenes to provide us with all sorts of suggestions and warnings. It would not be an exaggeration to say that IntelliJ IDEA knows more about our code than we do.
Now what if we could access the static analysis data, either to examine how the program works or to debug complex issues? Actually, this feature already exists and has in fact been around for quite a while.
It is called Dataflow to/from here, and it allows you to identify which values are possible at a given stage of data processing. This knowledge helps us detect potential runtime errors before they happen and optimize our code by removing redundant checks, conditions, and so on.
Consider the following example:
In the method
printList, we have a null check to account for cases when
null is passed as an argument. This protects us from a
NullPointerException in the event that we try to print out the contents of the list when it is
Let’s see how this feature can help us optimize this code.
Right-click the parameter, select Analyze | Data Flow to Here and specify the scope of the analysis. In the Analyze Dataflow tool window, click Group by Leaf Expression Nullness and open the respective tab.
The results of the analysis clearly show that no upstream code ever passes
null as a parameter when calling this method. We see that because all calls fall under the NotNull Values node.
Thus we are safe to eliminate the redundant null check and annotate the parameter as
@Notnull. Whenever you want to optimize your code but need more information to guide your decision about how to do so, data flow analysis is a tool that may save you a lot of time and help you avoid errors.
Using the same procedure, you can examine the entire range of input values. With this information available, you can prevent errors like unexpected ArrayIndexOutOfBoundsExceptions.
If an exception is thrown at runtime and you have a stack trace, identifying the cause becomes even easier. There is no need to look for the method that threw it and scrutinize the call tree. Instead, just click the frame where the exception was thrown.
Contact us to know more information on JetBrains IntelliJ IDEA and its other solutions.
LOGON is a pan-asian company operating in China, Hong Kong (HK), India, Singapore, Malaysia, Indonesia, Vietnam, Philippines and Thailand. LOGON has local dedicated trained product specialists in Hong Kong, Guangzhou, Kuala Lumpur, Mumbai and Bangalore. LOGON acts both as value added reseller and sole distributor for award winning software solutions. Customers can buy new licenses, purchase upgrades and renewals from any of our local offices. Contact us for first line support during evaluations, PoCs. We offer best practices consulting services and classroom & online training. Check our site for latest offers, special discounts, bundle deals, etc..