IntelliJ IDEA. Tips for Writing Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this screencast, we're going to look at how the features and shortcuts of IntelliJ IDEA help us to stay in the flow while we're writing code.   The first and most important thing to remember is that we're going to avoid using the mouse as much as possible. So, let's say we use Cmd N on macOs, or Alt + Ins on Windows or Linux, to bring up the "new" menu, we could use the mouse to select something from this menu. Or we could use the arrow keys. Or, like any window in IntelliJ IDEA, we can start typing and IntelliJ will search for our feature.   Menus are context sensitive, showing us the relevant options for this action. Creating a new Java class lets us create one of several different types of classes.   Now we have a class file open in the editor. Notice that by default IntelliJ IDEA has a lot of navigation bars and tool buttons visible. We can use Find action, Cmd Shift A or Ctrl Shift A to turn off the editor breadcrumbs.  We can use find action, or search everywhere, which is shift shift, to turn off the editor tabs, the navigation bar, and the Tool Window buttons.  Finally, Hide All Windows will maximise the editor so we can completely focus on the code.   IntelliJ IDEA will automatically generate common code for you.  One way to do this is by using Live Templates. We can type a short sequence, like psvm, and have IntelliJ IDEA generate things like the public static void method. We can even create our own live templates for code snippets we commonly use.    We can run the current context, in this case the main method that our cursor is in, by using Ctrl Shift R, or Ctrl Shift F10 on Windows.  Because this is a Gradle project, IntelliJ IDEA uses Gradle to run the code and we can see the output in the run window.   Hello world is hardly a real world example. Let's look at how IntelliJ IDEA helps us to write real code.  When we're starting a piece of work, sketching out an idea or prototyping, sometimes we work from the outside in - we write code the way we want to use it, regardless of whether the class or method already exists.  IntelliJ IDEA assumes we want to work this way, and shows us clearly which bits of code need our attention. Using F2 takes us to the next error, and pressing Alt and Enter gives us suggestions for how to fix it.  for example here the IDE suggests creating a class for Order.  We can create this Order class, and IntelliJ IDEA creates the class and a constructor that matches the one we were trying to use. We can rename the int parameter to something meaningful.   We can use F2 again to find out what IntelliJ IDEA suggests for this ID parameter, and we can get the IDE to generate the field and assign it to the value of this constructor parameter.   We can use Generate again, Cmd N or Alt and Insert, to create standard methods on this class. Let's create a getter for the ID.  We can use Cmd Shift Down, or Ctrl Shift Down, to move this method beneath the constructor.  We can also generate toString, hashCode and equals methods for the class. When writing Java code in IntelliJ IDEA, there's no need to manually type out standard methods, and using code generation also ensures a standard approach to these methods in all our classes.   We can also generate test classes for our code. IntelliJ IDEA supports a variety of testing frameworks, let's use JUnit 5 for this case.  We can generate the test methods using Ctrl and Enter or Alt and Insert. If we want to we can change the shape of these generated methods in IntelliJ IDEA's settings. IntelliJ IDEA is designed to keep us in the flow of coding, so there are ways to keep moving forward when writing code. For example, if we declare a new variable value, we don't have to move the cursor to the start of the line to declare the type, we can use extract variable to create it. Or alternatively we can use postfix completion to keep typing forwards and not have to move the cursor to another place.    IntelliJ IDEA's code suggestions support camel case, so we don't have to type the whole of the method, or even the whole of the first part of a method name, to get relevant code suggestions.  And even though we removed the navigation bar, we don't need it to run tests. We can use Ctrl Shift R, or Ctrl Shift F10 on Windows, to run this test method.   A key part of writing code is committing frequently.  Now we have simple functionality and basic test that works, we can already commit our changes.  Cmd K, or Ctrl K on Windows and Linux, lets us commit our changes.  This is the new commit window from IntelliJ IDEA 2020.1, in older versions we'll see a commit dialog.  Note that we even get code completion inside the description.  We'll commit these changes and keep working   We'll probably need to make some changes to our working code that break compilation.  There's no need to manually fix problems with IntelliJ IDEA - press Alt and Enter on any error and the IDE suggests things that might make the code compile.  Here, we could create a new constructor with this shape, but what we actually want to do is update the current constructor to take a new parameter.  Once IntelliJ IDEA has made the change, the code is compiling again. There's no need to look for a list of problems to fix, the IDE has applied the defaults so it all works. If we go to the declaration of this constructor, using Cmd Alt B or Ctrl Alt B, we see it has a new description parameter, but that it's not being used for anything.  Let's not fix that right now, let's test drive it.   We can use back and forth navigation to go to recent files.  We'll extract our description value into a variable so we can check it in the test.  We can use smart code completion to get IntelliJ IDEA to show the methods on Order that match the type we need.  We actually don't have one that returns a description yet, so let's call it as if it existed…. And get IntelliJ IDEA to create the method with the signature that we expect.  If we choose "create read-only property", IntelliJ IDEA will create a getter method and a field, but no setter.    Running this test shows that the description is null, which is what we'd expect since we haven't initialised the order's description field with any value yet.  We can navigate to the problem code by going into getDescription, and then using F2 to find a suggestion that could fix the problem.  Let's assign the description parameter to the description field and rerun the test. This time it passes.   Let's move on to some other tips for writing code.  Maybe we need to calculate the price of this order by iterating over a list of the LineItems in this order.  To start sketching out this method, we might start with a local variable ArrayList of LineItems - this class doesn't exist yet so we just get IntelliJ IDEA to create the most basic thing that will keep the class compiling.   [insert use of var]   We'll probably want a price variable to store the calculation result, which we'll return at the end of the method. Again, we can use smart completion here to suggest the best way to complete this statement given the return type.   IntelliJ IDEA has a number of Live Templates that help us to iterate over something. In the case of an ArrayList like this, we probably want to use iter, which gives us the most readable way to iterate over a Collection like this.  We can then drive out the functionality we expect in LineItem by using the API and then creating the simplest method that works.    Now we have the code more or less looking the way we want, we can use IntelliJ IDEA's inspections to guide us to the next steps. We have one warning here which says we're using a collection, but we never actually put anything in it. We probably want to do something about that.  We could use the Introduce Field refactoring to turn this from a local variable into a field of this order class.  We can choose where to initialise the field, let's say we want to do it in the constructor.  Now, if we go to the declaration of lineItems we see it's a field in this class, and it's initialised in the constructor.  We can use navigate back to get back to where we just were.    Not all of IntelliJ IDEA's suggestions are visible as warnings. Intentions and some inspections which are not set to warn, will be accessible when we press Alt and Enter.  It's always a good idea to press Alt and Enter on any code that we think could be written a different way to see what IntelliJ IDEA suggests for us.  Here, it suggests we could use a sum() stream operation.  IntelliJ IDEA will automatically refactor this code so that it works exactly as before but uses the new syntax.    One final thing we can do to simplify this method is to inline the variable, since it no longer make sense in a one-line method.    This code uses the Streams API from Java 8. I like my streams calls to be on one line per operation to help me to read it.  We can use reformat code, Cmd Alt L or Ctrl Alt L, to see if IntelliJ IDEA will apply the formatting I want.  In this case it says the code is already formatted according to standards. I can highlight the code I want to reformat and press Alt and Enter, and select Adjust code style settings to see just the settings that apply to this code block.  In Wrapping and Braces, we can change the settings for Chained method calls.  When we make a change, it's previewed in the editor, so we can decide whether or not that's the formatting we want.    We can put the calls back on the first line by using Join Lines to bring the line below onto this line. This is particularly useful when working with Strings which span multiple lines.   Finally, once we've made all the changes we want, we could use Run Anything, which is Ctrl Ctrl, to run our test and make sure we haven’t broken anything.  With everything working, we'll commit all our changes. IntelliJ IDEA makes it easy to commit small sets of changes that we have high confidence will work.    That's a summary of a very small subset of functionality in IntelliJ IDEA that makes it easy for us to focus on writing code that does what we need. Thanks for watching!
Info
Channel: IntelliJ IDEA by JetBrains
Views: 72,683
Rating: undefined out of 5
Keywords: intellij idea tips, intellij shortcuts, intellij java, intellij idea tutorial, intellij idea, intellij, jetbrains, intellij idea java, java
Id: _Y1y8k-OTCQ
Channel Id: undefined
Length: 9min 43sec (583 seconds)
Published: Wed Apr 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.