IntelliJ hacks using Scala

Reading Time: 3 minutes

Introduction

An IDE or integrated development environment is a software application that combines, in one place, all the tools needed for a software development project. On a more basic level, IDEs provide interfaces for users to write code, organize text groups, and automate programming redundancies.

Here we will talk about IntelliJ IDEA, one of the popular IDE in the market which has advantages over others when we are considering the Scala language.

Let’s see one by one which are the tips and tricks we can use while working with the IntelliJ idea & Scala.

Scala Plugin for IntelliJ IDEA

The plugin adds support for the Scala language :

  • Coding assistance (highlighting, completion, formatting, refactorings, etc.)
  • Navigation, search, information about types, and implicit.
  • Integration with sbt and other build tools.
  • Testing frameworks support. (ScalaTest, Specs2, uTest)
  • Scala debugger, worksheets, and Ammonite scripts.

Investigation performance issues

There is a “Scala plugin profiler” tool window to track invocations of methods with @Cached* or @Measure annotations (from org.jetbrains.plugins.scala.macroAnnotations package) in real time. The tool window is available in internal mode or if -Dinternal.profiler.tracing=true is passed to IDEA using custom VM options.

Themes option

Dracula PRO is a color scheme and UI theme tailored for programming. Made for terminal emulators, code editors, and syntax highlighters. Designed to be aesthetically pleasing while keeping you focused. Also when switched to a dark theme, the Scala syntax highlighting kept certain parts of the code in dark colors.

Type Inference

This feature works when we put the cursor on a variable definition hit Alt+Enter and IntelliJ will offer you to add a type annotation to the declaration. For example, if you have this :

val map = service.giveMeMyMap().mapValues(transformsTheValues)

Then this operation will yield something like :

val map: Map[String, ValueType] = service.giveMeMyMap().mapValues(transformsTheValues)

In this way, the editor participates more in the mechanics of coding & minimizes typing errors.

Code Inspection

You can ask the editor to inspect your code through some set of predefined rules. For example, The original code was something like this :

books.filter(_.isInteresting).headOption // returns the first interesting book if such exists

The code inspector’s suggestion was to replace it with :

books.find(_.isInteresting)

which is equivalent, probably a little more efficient as there’s no intermediate data structure, but most importantly – it is more readable.

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 code path 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.

Refactor

In the case of refactoring, 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.

Extract Method

Refactor -> Extract -> Method

Sometimes when we are blazing through some code, we 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. Extract enables the IDE to help you out with that potentially complicated refactoring process by determining the parameter and return types automatically.

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. By hitting the Code Reformat button, you can clean up code quickly and easily.

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 we have the schema locally, we also gain the autocomplete in the console.

Written by 

Written By Shashank Khutwad Shashank is a Software consultant at Knoldus Inc. with more than 2 years of experience in Java, MySQL, Spring Framework, and Scala. His primary skill areas are spring boot, hibernate, and collection framework. Shashank is a focused and result-oriented, self-motivated,team-oriented, and effective team player.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading