Hacker News new | ask | show | jobs
by Zaheer 3750 days ago
My biggest problem with Scala is that it's too powerful. Reading code I've written weeks or even days later is painful. Conciseness is not always good.
4 comments

In what ways does Scala you've written end up being unreadable just days later? I honestly find that Scala code is some of the simplest and easiest to reason about of any language. Writing Scala has honestly been such a joy, it's easy to refactor, easy to read and write, and I almost never miss something -- if it compiles, Scala code is usually correct I find.
I think that, if you're not super aware of most of the features of Scala, that it can be confusing to understand what certain syntax means (implicits are a big one!)

Just the infix operator syntax plus implicits can lead to stack trace confusion all around

And try understanding some of the type signature funkiness! It can be difficult

I definitely think that Scala is a harder language than most to grasp, if only for the feature support not seen since C++

Scala's infix operator syntax is very mechanical. Much easier than e.g. Python where you have to remember whether a * b desugars to a.__times__(b) or a.__star__(b) or something else. The IDE support for those and for implicits is very good these days.

The type signatures can be complex but only when you're doing complex things with them - if you stick to doing the kind of things you'd do in another language you'll get similarly simple types.

You're absolutely right that the native stack traces are poor, but you can use tools that understand them better, e.g. Takipi.

Well written Scala code is fantastic to work with, but it does take some practice and judgement to write well structured readable code. If you're having trouble reading your own code days later perhaps consider the way you're using the language. I'd strongly recommend reading http://www.lihaoyi.com/post/StrategicScalaStylePrincipleofLe... if you're have trouble reading your own code.
I second that. Well written Scala code is simply wonderful to read. You learn so much just by seeing how people solve the problem much better than you. Its probably because Scala is so terse, you get to see the logic behind the code much easier than a language with lots of boiler plate.
I find that I consciously need to go over any code and rewrite it with a reader in mind. The first version is usually crap, but at the same time, it's very easy to fall into the trap of making it Just Right which makes the second version "super elegant" but completely opaque at the same time.

I often end up making a "super elegant" version and, almost invariably, end up scrapping it for a (hopefully...) more bread-and-butter version, which may or may not incorporate some lessons learned from the concise version. Even if not, it's not all loss, because coming up with the elegant version is intellectually rewarding and super fun.

Add lots of in-line (not just scaladoc) comments helping the reader along. Extract vals for intermediary steps. Variable naming is always important, but even more so when you don't explicitly declare the type.

Conciseness is optional. Sometimes a new Scala programmer will learn functional combinators and try to cram a lot of logic into one line. Breaking things up into semantically related steps and assigning each step to a val with a descriptive name will make the code more readable to others (and the author's future self)