DEV Community

Murphy Randle
Murphy Randle

Posted on

A Quick Productivity Tip For Scala Beginners

One of the first things I might want to learn when given a new language is how to quickly run an isolated chunk of code, so that I can easily try out small experiments, and make sure I understand the language semantics or its libraries. It's not always simple to figure out how to do this, though.

If we're using Node.js / Javascript, we can simply write a little script and execute it. Something like the following.

We write a little file called foo.js to see if we understand how strings and numbers interact in Javascript:

function foo() {
    var addingNumbersToStrings = 1 + "2"

Enter fullscreen mode Exit fullscreen mode

and we run it like this:

node foo.js
Enter fullscreen mode Exit fullscreen mode

It will run, and print out "12", and now we're sure we understand how JS works 😉.

But now let's say our job description changes a bit, and we're moved onto a team that's writing Scala. How can we quickly test our assumptions like we did in the example above?

We might reach out for the Scala REPL inside of sbt, which works to some degree, but is cumbersome to iterate with, and isn't great for keeping those little tests around, if we want to refer to them later.

But, if we're using IntelliJ with the Scala plugin as our editor (which I think most Scala developers are at this point), here's a handy little tip. Inside of any .scala file, we can add an object that extends App, and that object will become executable:

object UnderstandAddition extends App {
    println(1 + 2)
Enter fullscreen mode Exit fullscreen mode

IntelliJ will even put a little green triangle in the gutter next to that object, telling us that we can run just that piece, without having to run the whole massive server we're working on.

If we don't see that little green triangle, we can always always right click on the name of that object, and choose to run it from within the contextual menu.

In this case, we'll see the console print out the number "3".

But, let's level up a little bit and turn those experiments into ad-hoc tests:

object TestAddition extends App {
    assert(1 + 2 == 3, "Oops, math broke")
Enter fullscreen mode Exit fullscreen mode

Wow! we've just written a little test inside of our main codebase without having to configure test runners, load assertion libraries, or make new files! And any time we run that class, it will tell us if our assumptions are true or not.

Obviously, for a real test suite we'd want to go to the trouble of configuring a test runner, etc... and hooking up something to sbt test so that we can rest assured that all the test in our codebase have been run, and are passing. This little tip is just here to help us spot-test our logic, and iterate our way into becoming proficient in the ecosystem.

Top comments (1)

jhartley218 profile image

Really convenient, thanks. I personally prefer working inside Intellij vs. working in the Scala or Amonite REPLs. And this is faster than iterating on specs2 tests or worksheets. Thanks!