My Journey in Scala, Part 2: Tips for Using IntelliJ IDEA

After spending significant time in dynamic languages, learning Scala can be challenging, so having the right tools (like IntelliJ) at your fingertips can make all the difference.

After mentioning IntelliJ IDEA in my last blog post, I got a lot of feedback and great questions about how it’s used at Threat Stack. IDEs are very personal things to software developers, and our platform team generally uses IntelliJ — although we have everything from hardcore vim junkies all the way to die hard emacs users at Threat Stack spread amongst the different teams. We don’t require that developers use IntelliJ or any IDE in particular; a large majority of us do use it because of the great built-in functionality that enables more rapid development, easier day-to-day workflow, and an improved ability to interact with your code. Given the myriad features available in IntelliJ, it can be difficult to get exposed to all of them. To get you started, I’ll highlight some of our favorite and most commonly used features.

Scratch Files

Tools -> New Scratch File

Scratch files are an excellent way to dry run code without having to compile your full application. This is especially valuable if the codepath you’re working on is on the other end of a web request or has multiple conditions. Without spinning up the entire application, you can quickly iterate, test some code, or validate an assumption on how a particular bit of code performs. As you save the scratch file, it will display the results of your code in the right-hand pane. These files persist when you close the IDE but won’t display in your file tree or in your git repo, making them convenient to use. An example of the IntelliJ UI is shown below:

scratch.gif

Screen_Shot_2016-07-18_at_3.15.16_PM.png

Refactor

Refactor -> Rename

If you’re coming from JavaScript, as I was, refactoring is an incredible pain. Due to the dynamic nature of JavaScript, it’s difficult for the IDE to correctly identify all instances. It’s very likely that you will not hit all the desired cases, and without compile-time failures, you have to hit the code path to find the errors. However, refactoring in Scala with IntelliJ is an incredibly easy process. You just highlight the variable you want to refactor, and hit the command to change ALL instances of the variable across your entire project. This can also take care of class declarations, imports, and file names. IntelliJ also has excellent Declaration lookup, allowing you to see a method’s source code easily. Its JavaScript support is pretty good as well. I have more luck getting to the actual declaration with IntelliJ/WebStorm compared to other tools.

refactor2.gif

Extract Method

Refactor -> Extract -> Method

Extract Method is a wonderful, wonderful tool. Sometimes when you’re blazing through some code, you end up with a method that’s doing multiple things. Reducing method footprint is critical to having code that is easier to test, easier to read, and more reliable. At Threat Stack we have a high confidence in the code we ship by having small, easily testable units. Extract enables the IDE to help you out with that potentially complicated refactoring process by determining the parameter and return types automatically.

Screen_Shot_2016-07-16_at_1.13.19_PM.png

Screen_Shot_2016-07-16_at_1.13.49_PM.png

Reformat Code

Code -> Reformat Code

Occasionally you might have come across some code that is difficult to read. Perhaps even after editing your own and doing some refactoring, things are misaligned and aren’t looking quite right. IntelliJ has the ability to define code style, so you can set numerous options for formatting things such as how you want braces to appear, how to format if else statements, blank lines, and more. IntelliJ comes with a fairly reasonable default setup, so if your company hasn’t defined one, it’s better than nothing! By hitting the Code Reformat button, you can clean up code quickly and easily. Take a look at the before and after examples below:

Before

Screen_Shot_2016-07-16_at_1.25.37_PM.png

After

Screen_Shot_2016-07-18_at_11.07.31_AM.png

Database Tools

IntelliJ also has built-in database tools. After connecting IntelliJ to a database (if you run a microservice style architecture, you will want IDE-Wide access!), the schema is downloaded locally. This enables you to browse tables via the UI or write queries via the console. When you have the schema locally, you also gain the autocomplete in the console. See the examples below:

Screen_Shot_2016-07-18_at_11.10.10_AM.png

 

Screen_Shot_2016-07-18_at_11.10.24_AM.png

Note: Unfortunately, there is no type safety, so you could write this query with a String instead of an Integer without IntelliJ displaying an error. You will still get the response from the database as expected, however.

As part of the GUI, IntelliJ also has a very handly table creator. If you can’t remember all the different CREATE/ALTER SQL commands off the top of your head, you can use IntelliJ to help you out! An example of creating a new table with IntelliJ is shown below.

Screen_Shot_2016-07-18_at_11.12.11_AM.png

The Final Word — For Now

These are just a few of the ways I use IntelliJ on a regular basis. While I was writing in JavaScript, I wasn’t exposed to many of these features. Even IntelliJ has difficulty with some of the tasks in JavaScript. Thankfully, when I started my journey into Scala, the Threat Stack Engineering team showed me the way and helped increase my knowledge of many things, including IntelliJ.

If you have any tips or tricks of your own that you would like to include in the list of favorites I discussed in this post, send them along! We’re always looking to improve our workflow and to better use tools to achieve zen with our code.