Touring Plugins – GitIgnore

I always have a .gitignore file in my Dropbox folder that I copy over for new projects, knowing that any subsequent update to the copy isn’t always synced with the original. Of course, it’s not always necessary either, but it’s something that’s always bugged me.

Recently I ran across a plugin for IntelliJ IDEA (and family) that solves my issue with ignore files.

It’s called GitIgnore Support and as its name suggests it provides support for .gitignore files, but it goes way beyond what you’d imagine at first. Let’s take a look. Continue reading

Posted in Featured Plugins, Tips & Tricks | Tagged | Leave a comment

IntelliJ IDEA 14 EAP 139.1 available, from a release branch already

A fresh update to IntelliJ IDEA 14 EAP is released and you may note we’ve switched to a “release” branch (139) already, so the release is not far away already and we’re in a “stabilization phase” mostly.

We urge you to check the full release notes to compare this build to the previous one.

A number of not so big but important features for everyone was added recently to the editor, including “Go to Next Edit” command, which is the opposite to a very useful “Go to Last Edit”, an additional options to show white-spaces only in front and/or behind and a number of other changes.

We need your feedback now even more than before. It is very welcome in our discussion forum or directly in the issue tracker.

Develop with Pleasure!

Posted in Beta Releases | Tagged , | 13 Comments

The Inspection Connection – Issue #4, Duplication Detection

One of the core principles of computer programming is eliminating manual repetition. Not only is repeated code time-consuming to read and review, it is disproportionately complex to maintain and provides a ready source of bugs when one copy changes. Yet in spite of this, duplicating some code in a large project may be inevitable. IntelliJ IDEA offers a powerful inspection for detecting and deduplicating repetitive code. You can activate this inspection by navigating to the menu entry Analyze|Locate Duplicates, or by pressing Ctrl/⌘+Alt/⌥+Shift+I then typing, “Duplicated Code.”

As it turns out, “Don’t Repeat Yourself” has some notable exceptions, especially when writing unit tests, and occasionally in some rare cases when attempting to outsmart the compiler. IntelliJ IDEA gives you the opportunity to exclude test sources, since many prefer a little duplication in their tests to improve overall readability. Nevertheless, the choice is yours.

duplicates_scopeWhen analyzing a large set of files, you should carefully consider the scope of the inspection you are going to apply. There are a number of algorithms for detecting duplicate code – this one will scan for duplicates by comparing AST fragments. On an Intel i7-2640M with 8GB of RAM, scanning a codebase with 53,000 LOC will take about 3 minutes, or ~300 LOC/second. However your mileage may vary depending on the minimum duplicates length, line density and other factors. When searching for exact matches, the time complexity is effectively linear w.r.t. the number of nodes in your AST.

duplication_analysis_settingsWhen searching for duplicate code, we may want to discard duplicates shorter than some minimum threshold or add a similarity metric in order to detect fuzzy-matches. IntelliJ IDEA lets us define these values, but keep in mind they do not correspond to characters or lines, but rather arbitrary weights. If “Visible from outside of the current scope only” is selected, only subexpressions which are valid outside of their parent construct are anonymized. If you would like to learn more how all of this works, head over to the source.

duplicates_viewAfter scanning, the Duplicates Tool Window is a convenient area to browse through duplicate regions and view a side-by-side diff at each location. From here you can clearly see which sections are structurally similar and flag them for review or refactor them immediately. The highlighter will help you quickly identify which blocks are different.

duplicates_focus

When you have found a duplicate block of code, it can be easily simplified by pressing Ctrl/⌘+Alt/⌥+M to apply the Extract → Method refactoring. IntelliJ IDEA will transform all inbound variables inside the selected region with parameters and provide a helpful dialog to extract the method, which you can use to replace any other duplicates inside the analysis scope.

extract_method_windowThis inspection is also available in TeamCity, where you can assign a dedicated build runner to periodically scan your project for duplicate code. This may be an even better option if your organization needs regular assurance audits for clean-room engineering. Plagiarism detection is another important use case for the academic community, and if you are a student or teacher, IntelliJ IDEA is now completely free.

Posted in Tips & Tricks | Tagged , , | Leave a comment

The Inspection Connection – Issue #3, Annotation Validation

Android developers will be familiar with the concept of resource IDs, which are unique integers generated by the Android Asset Packaging Tool (aapt) for accessing resources within your application through the static class, R. Resource IDs are assigned to all resources defined inside your project’s /res directory.

In the past, it was common to inadvertently pass one resource type into a parameter that expected another, since they are both integers. IntelliJ IDEA now offers improved support for detecting mismatched resource types, thanks to some resourceful developers on the Android Tools Team. Tor Norbye explains.

Not only is it possible to detect mismatched resource types in Android’s APIs, now you too can leverage resource type checking in your own libraries and APIs on Android 19+, with resource type annotations. Like type annotations in Java 8+, these annotations will help validate parameter values at compile time:

This inspection will detect two forms of resource type mismatches. The first is when you pass one type of resource into a parameter that expects another type, like above. The second will notify you when you have passed a constant into a type that only accepts a specific set of values. Let us consider the following example:

IntelliJ IDEA will detect both of these cases with the inspection Constant and Resource Type Mismatches, enabled by default under Inspections|Android. In order to use support annotations on Android 19+, you can add the following dependency to the dependencies section of your build.gradle file.

In addition to Nullness Annotations available in Android, IntelliJ IDEA 14 now offers a brand new, fully automatic nullity inference inspection based on bytecode analysis, to help you identify sites that may cause NPEs. Together, these features will help you identify bugs much sooner and develop with pleasure!

Posted in Tips & Tricks | Tagged , , | Leave a comment

Touring Plugins – Floobits

Software has become an increasingly cooperative process, both for applications themselves and for those who build them. With today’s developer teams spanning classrooms and continents, good interprocess communication is critical, and Floobits can help.

Floobits is a real-time collaborative editor, a sort of “shared memory” for developers around the corner, or around the globe. We can imagine a few activities where this type of communication is especially useful: interviewing, reviewing and pair programming.

First you will need a Floobits account in order to get started, which is available through Floobits’ website or via your on-premises provider. Next, add a configuration file in your home directory, then install the Floobits’ plugin, as described below.

Once you have configured your development environment and shared a project with the Floobits server, go ahead and add a contributor via your Floobits account. Take some time to explore the browser features. When you’re ready for a real IDE, fire up IntelliJ IDEA.

Here at JetBrains, we have been happily using Floobits for a while and thought we would share an example of how you can use Floobits to collaborate on IntelliJ IDEA or any of the platforms which Floobits supports. Maarten and I take Floobits for a spin.

In SICP, Abelson and Sussman write, “Programs must be written for people to read, and only incidentally for machines to execute.” IntelliJ IDEA gives us the tools to help build and maintain code. Together, with best practices like pairing and regular reviews, we can achieve both.

Special thanks to Geoff Greer, Matt Kaniaris, and Bjorn Tipling for helping fix the bugs.

Posted in Demos, Tips & Tricks | Tagged | Leave a comment

Automatic @NotNull/@Nullable/@Contract inference in IntelliJ IDEA 14

Java annotations are awesome and helpful, but you need to add them by hand. Well, not anymore, because IntelliJ IDEA will do that for you in some quite important cases.

Jars in your dependencies are stuffed with methods and sometimes it may be difficult to tell whether you can pass null there without blowing things up, or whether you should be ready to get null as a method result.

If you’re lucky, the documentation will specify that, if not, you have to read the source code, or even the decompiled output, which is not always as easy as you’d like it to be. There are, of course, NotNull/Nullable annotations available in various frameworks, but not everybody uses them. JDK doesn’t, for example. IntelliJ IDEA supports external annotations, but they also don’t cover most of existing jars.

Well, this is what happens now. IntelliJ IDEA will look carefully at SDK and libraries bytecode and will infer these annotations automatically so that they can later be used to analyze source code to spot places where you overlooked null. You can see these inferred annotations in italic in the editor gutter.

Screenshot from 2014-09-01 17:49:04

Contrary to the already available Infer Nullity feature, this one works with bytecode and is completely automatic: the results are just there in the editor, and you don’t have to do anything to make that happen.

Of course, the logic behind this feature is not as simple as “if a parameter is immediately dereferenced, then it’s @NotNull”. There’s science at work behind the curtains that does a lot more, for example, recursively processes delegating method calls.

Screenshot from 2014-09-02 14:25:29

For methods with a more complex behavior, as you probably know, we have the method contracts. These are now inferred, too. Why is this important? Well, suppose you’re using Apache Commons StringUtils and have such warnings in your code:

Screenshot from 2014-09-02 12:14:58

Earlier IntelliJ IDEA had no clue that code execution wouldn’t even reach the “startsWith” call if the url is null because it didn’t look inside StringUtils.isEmpty. You could of course remove those yellow warnings in extractPrefix by looking inside isEmpty yourself and adding a contract “null -> true”, but that’s so boring! That’s exactly a job for computer, not you, and now IntelliJ IDEA does it automatically, looking at byte and source code.

Screenshot from 2014-09-02 12:25:32

Get the latest preview build and see how this works for you. We are eager to hear your feedback, be it bugs, usability issues or suggestions about more annotations to infer.

Posted in New Features | Tagged , , , | 23 Comments

IntelliJ IDEA 13.1.5 Update is Available

Even if you’re already waiting for IntelliJ IDEA 14 with its all new awesome features, you still might be glad to know that we’ve just released a fresh update for IntellIJ IDEA 13.1 packed with lots of important fixes.

Screen Shot 2014-09-29 at 22.19.31

If you’re running IntelliJ IDEA 13.1, then click “Check for updates” and install a patch. Otherwise go to the official website and download the new version from there.

Develop with Pleasure!

Posted in Releases | Tagged , | 26 Comments

The Inspection Connection – Issue #2, Proposition Transformation

Boolean algebra can be difficult to reason about, which is why IntelliJ IDEA provides several inspections and intentions to simplify boolean expressions, improve readability and help make your code less prone to bugs. Here are just a few you may encounter while writing Java.

1. De Morgan’s Laws

De Morgan’s laws are (1) ¬a ∧ ¬b ≡ ¬(a ∨ b) and (2) ¬a ∨ ¬b ≡ ¬(a ∧ b). We can use these laws to transform boolean expressions programmatically, saving you the hassle of drawing a truth table to confirm your own good intuition.

By placing your cursor on line 2, then pressing Alt+Enter,w to select Replace ‘&&’ with ‘||’, IntelliJ IDEA will apply De Morgan’s first law, by first replacing conjunctions with disjunctions and negating the whole clause, leaving us:

As you can see, this may not always be the shortest or most natural form, so we can easily swap back and forth by between them by pressing Ctrl/Cmd+Z and Ctrl/Cmd+Shift+Z, or by repeating the inspection to apply De Morgan’s first law in reverse.

2. Rearranging control flow

IntelliJ IDEA provides two intentions for merging nested Ifs with conjunction and equivalent Ifs with disjunction, and two for separating them, via Split if and Extract if condition, respectively. For example, consider the following scenario:

By applying the first intention Merge nested ‘if’s, this can be reduced to:

Likewise, sequential ‘if’ conditions with identical bodies can be joined with disjunction.

By placing your cursor over the ‘i’ or ‘f’ on line #1 and applying the intention Merge sequential ‘if’s, this can be reduced to:

Now, if you want to break up a complex ‘if’ statement, you can place the cursor over a boolean expression, and apply the intention Extract if (…), where ‘…’ indicates the expression to be extracted.

Whichever disjunct your cursor is covering when the inspection is applied, that condition will be extracted into a new If statement and either nested inside the existing one or placed directly afterwards.

Keep in mind, if that if the condition contains a function call, this refactoring may alter the sequential structure of your program.

3. Flip Comparison

By reversing the traditional order of conditionals, Yoda notation claims two benefits: reducing null pointer errors and preventing accidental assignment. Many programmers follow this style in order to call attention to the boundary value:

However we can see that Yoda notation hides a null pointer that may potentially reappear later (in this case on line #4). The second motivation, that Yoda notation prevents ‘=’ where ‘==’ is intended, is demonstrated below.

In Java, there are very few cases where accidental assignment is also syntactically valid, and IntelliJ IDEA will detect those cases where assignments are used inside a condition. So we can safely swap Yoda conditions with the more traditional variation.

No matter which convention your code adopts, is important to use one consistently across your project. IntelliJ IDEA supports annotations for nullity inference, so the rationale behind Yoda notation in Java is less clear, but the choice is yours.

4. Avoiding negative conditionals

In Clean Code, Robert Martin writes, “Negatives are just a bit harder to understand than positives. So, when possible, conditionals should be expressed as positives.” (Martin, [G29]). IntelliJ IDEA has three inspections to help you stay positive.

When conditionals are negative, this is a good indication that control flow can be refactored. When refactoring your code, you should apply the intention Invert If Condition. Notice how De Morgan’s law is implicitly used to refactor this clause.

IntelliJ IDEA can detect when a boolean method is always inverted, giving you the opportunity to rename these functions. In order to trigger a global inspection like this one, you will need to explicitly run it. You can find this inspection under Analyze|Inspect Code.

This warning can be resolved by clicking “Invert method” on the right hand side of the Inspection Results Window, under “Problem resolution”. IntelliJ IDEA will prompt us to rename the method and then remove all negated invocations across the scope of the inspection.

isAlwaysInverted

Similarly, the inspection boolean variable is always inverted will help discover negated variables within a class, and IntelliJ IDEA 13+ offers an inspection for detecting negatively named boolean variables, like disabled, hidden, or isNotChanged. You can enable this inspection under Inspections|Data flow issues.

Posted in Tips & Tricks | Tagged | 5 Comments

IntelliJ IDEA 14 Preview is Available!

Many of you, together with the IntelliJ IDEA team, have been waiting for this moment for some time: today we can finally reveal what we’ve been working on during the year to make your Java development even more of a pleasure. We’re extremely happy to announce that IntelliJ IDEA 14 is now available as a preview.

Everyone is welcome to download it and give the new features a try right away!

idea_logo

As always, IntellIJ IDEA 14 is better in every way: the UI, the editor, built-in developer tools, and coding assistance across all the supported frameworks. The major changes include: Continue reading

Posted in Beta Releases | Tagged | 9 Comments

Touring Plugins – Presentation Assist

When doing screencasts on IntelliJ IDEA or some of our other IDE’s, or giving live presentations, I usually use a very useful plugin which is called Presentation Assist.

What it does, once installed, is display on screen the keyboard combination that was just pressed.

highlight

The added benefit is that it provides keyboard combinations for one or two layouts. This can be really useful for those that are using different keyboard mappings. What’s even better is that the layouts and text displayed for alternative key presses can be configured, or even turned off

Configuration for Presentation Assist

Also, if doing presentations, note that there is actually a Presentation Mode in IntelliJ IDEA (View menu) which goes full screen and larger fonts.

Use while Pairing

You might wonder of course, what use is this for me if I’m not presenting or doing screencasts? Actually, this plugin is even more helpful when pairing with someone. Often times when I’m working with a colleague, I’m asked “Oh, what keyboard combination did you press just there?” or “What did you do to get that window?”. Having key presses show up on the screen answers most of these questions and helps us pick up shortcuts and more efficient ways of working with IntelliJ IDEA (or sister IDE’s).

One other tip in regard to pairing, don’t forget the Quick Switch Scheme (Ctrl+`) shortcut option, which allows us to quickly change keyboard mappings (and other things), something that is again very convenient when doing pair-programming or having to use someone else’s computer for a while.

keymap

Last but not least, and talking about presentations, if you’re at JavaOne 2014, don’t forget that we’ll be giving two talks, one on IntelliJ Tips and Tricks and another on building your own IDE. Hope to see you there!

Posted in Tips & Tricks | Tagged | 2 Comments