Skip to content

Latest commit

 

History

History
146 lines (98 loc) · 10.4 KB

chapter6.md

File metadata and controls

146 lines (98 loc) · 10.4 KB

Chapter 6 - IntelliJ's Suggestions

Back to outline

We are but human. That means we tend to mess up from time to time. Knowing how to quickly fix mistakes you make is a tremendous boon to your programming productivity. Luckily, our IDE can help us fixing some of the smaller mistakes we often make.

IntelliJ's Auto-correct Mac + + ↵︎

Let's open Chapter6.java (Mac + o) , a class with many mistakes (and (a hell of (a lot of)) brackets).

Let's see what IntelliJ can do for us to fix the mistakes by pressing MacF2 to navigate quickly to the first error, and press Mac + + ↵︎.

Press MacF2 again and let's see if IntelliJ can auto-fill the missing semicolon.

Press MacF2 again and see if it can auto-correct an incomplete method.

Undo (Mac + z) your last auto-correct and type out public void poo( above the comment line.

It's basically the same thing we tried to auto-correct just a few seconds ago. The only difference is that there are now two "incomplete" error statements.

Try pressing Mac + + ↵︎ anyways and see what happens.

Autocomplete Mac + Space vs. Mac + Shift + Space

Autocomplete basics

Open Transformers.java, a class with a lot of action in it. To quickly navigate to the missing constructor argument, use F2.

To have IntelliJ suggest an autocomplete option, press Mac + Space. Note that even though the constructor expects an AutobotEnum, IntelliJ may suggest other options like new, null, equals(), etc.

To have IntelliJ provide more helpful context-specific code completion actions, try using the alternative hotkey combination Mac + Shift + Space.

Before selecting one of the AutobotEnum suggestions, try pressing Mac + Shift + Space again. This hotkey will show you that there is a static method Autobot.optimusEnum() that also returns an AutobotEnum. IntelliJ knows about this method as well.

Try it out again to construct Optimus' nemesis, Megatron.

Autocomplete selection confirmation

After using Mac + Shift + Space or + Space to bring up suggestions, there are multiple ways to confirm your selection:

  • Pressing MacEnter completes the code.
  • Pressing MacTab replaces code that was there before.
  • Pressing Mac. completes the code and adds a . so you can continue typing.
  • Pressing MacSpace does the same but adds a space instead of a . (this is different from regular suggestions after typing a .!)

As it stands, Optimus has a method called .catchphrase that takes a prefix and returns <prefix> Rollout!. We can rename that method to rollout.

To navigate to the .catchphrase method, use Mac + Shift + B. Then delete catchphrase and replace it with rollout (don't use MacShift + F6).

Next, go back to the Transformers class with Mac + [ and use autocomplete to replace the previous non-compiling catchPhrase method with the new rollout method. Try using MacEnter as your confirmation selection first, and then undo with Mac + Z and retry the autocompletion using Mac Tab as your selection confirmation.

Repeat the process with the other confirmation methods listed above.

QuickFix (:bulb:) Mac + enter

IntelliJ's QuickFix is a powerful tool that can save you a lot of time and effort. In fact, it is so powerful that it might as well be magic. Whether you're creating a new constructor, generating getters and setters for newly created fields, or extracting a method or variable, QuickFix has got you covered. And if your code isn't compiling, QuickFix can help you fix the problem in no time.

To use QuickFix, simply press Mac + enter. This shortcut will bring up a menu of possible actions you can take to fix the issue at hand. Use cases include, but are not limited to:

  • Creating a new constructor? Use Mac + enter.
  • Generating getters and setters for newly created fields? Why not try Mac + enter?
  • Extracting a method out of selected code? Make a selection, and press Mac + enter.
  • Extracting a variable? Press Mac + enter.
  • Code not compiling? Press Mac + enter.
  • Need to defeat agent Smith and save the matrix? Press Mac + enter.

QuickFix is so useful that it's probably the only IntelliJ shortcut you really need to remember, if you want to be lazy about it.

Creating stuff Mac + N

The "Generate" action in IntelliJ is a powerful feature that allows you to quickly create code constructs such as constructors, methods, and getters/setters. It saves you a lot of time and effort by automatically generating boilerplate code for you, based on the parameters you provide. You can access the Generate action by pressing Mac + N (Generate...) in the editor window. This action is particularly useful when you need to create a large amount of repetitive code, or when you need to add functionality to your code quickly.

Try out these actions using the Mac + N shortcut:

  • Create a new package from the 1: Project Tool Window to create a new class in the next step.
  • Create a new class from the 1: Project Tool Window.
  • Create a new constructor in that class from the editor.

Deleting stuff MacShift + Delete

Programming is not only about creating new concepts and abstractions but also about cleaning up and getting rid of old or unnecessary code. Fortunately, IntelliJ makes deleting code just as easy as creating it.

To delete code, you can simply select the lines you want to remove and press MacShift + Delete. You can use it to delete any code fragment in your project.

Your turn! Use this shortcut to:

  • Delete the constructor you just created
  • Delete the class you just created (the package will probably be deleted automatically because it was the only class in there)

TIP:
For more advanced scenarios, IntelliJ provides refactoring actions to help you delete code more safely and efficiently. For example, you can use the "inline and remove" refactoring action (Mac + + N) to remove a method and all its invocations at once without breaking your syntactical correctness.

First you empty the body of the method, next you inline it (Mac + + n).

This is sometimes called the "nuke it" option, and it can be very handy when you want to get rid of a method that is used in multiple places. However, be sure to use this option with caution, as it can have unintended consequences if you're not careful.

View JavaDoc MacF1

Open Transformers.java, and move your cursor to Autobot.

Press MacF1 to view the JavaDoc for the Autobot class. Press MacEsc to exit the window.

Press MacF1 twice to open a larger and separate window. Note that you cannot exit this window by pressing MacEsc, but you can close it by pressing F1 again and then pressing MacEsc.

You can also use MacF1 from inside the autocompletion suggestion box. Try calling .toString() after your new Autobot() statement using autocompletion (either by typing . or by pressing MacF1).

While still in the autocompletion suggestion box, navigate to the .toString() option and press MacF1 to read the associated JavaDoc.

View parameters Mac + P

If you are ever in doubt on which parameters to provide to a method, press Mac + P while your cursor is inside the brackets of a method invocation statement. IntelliJ will tell you what parameterization is needed, and even offer a list of alternative invocation options.

In the Transformers.java class, let's try to create the Decepticon, known as "StarScream".

First uncomment the // Decepticon.StarScream() line.

Then put your cursor in between the brackets and type Mac + P.

Notice how IntelliJ signals which parameter you are required to fill in by emboldening the parameter name.


Next Chapter

Back to outline