Scala

How Scala changed the way I think about my Java Code

Some people advocate their preferred language as the only way to enlightenment and productivity boosts way in the two digit percentage range compared to another language in the same category. I don’t believe it. (It’s probably true when you compare things like Java and Assembler, but few do that).

There are others that tell you the language doesn’t matter at least not between languages like Scala and Java. I think they are wrong as well. The reason is: Although I don’t actually use Scala during my day job it does affect my Java Coding. Here are some of the things I noticed.

  • I appreciate immutability. I was aware for a long time of the benefits of immutability. But in Java using immutable data structures is so clumsy that I only used this approach in small areas, like rather simple value objects. But now I find my self more often tempted to use an immutable data structure even for relatively complex things.
  • Lots of small classes. Classes in Scala are unbelievable small. It’s easy to write a single page of code with half a dozen classes in it if you count the ‘class’, ‘object’ and ‘trait’ keywords and even more when you look at what the Scala compiler creates from closures and similar. Although creating a class is much more work in Java I find myself thinking much strict about the single responsibility principle, resulting in more smaller classes, which are easier to test.
  • Thinking more in DSLs. Scala is great for creating internal DSLs. Lots of libraries use that approach for designing their APIs and I do it in my little projects as well. Java is pretty useless for DSLs. But there are things you can do. Even if it is simple stuff like method chaining. I use that a lot lately, especially when testing is concerned, because here readability of code is even more important than with normal code.
  • I pretty much gave up on Java Generics. After seeing what is possible with a good type system, even when the compiled code runs on the same JVM as Java, made me also see the severe limitations of Java Generics. Before that I tended to think its just me who is to stupid. While this assumption is often true. Many things I tried to do in the past with Java Generics just can’t work, because Java is lacking the necessary power (e.g. covariance and contravariance). So I drop generics from my code as soon as more than one type parameter gets involved.

So go ahead and learn a new language. Even if you can’t use it in your normal job. You still will learn useful stuff.

But be warned. I might as well make you hate parts of your day job: I really, really hate semicolons, primitives, arrays and collection APIs without higher order functions. And switching on Strings is a joke.

Reference: How Scala changed the way I think about my Java Code from our JCG partner Jens at the Schauderhaft blog.

Related Articles :
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Debasish
11 years ago

Nice article, but giving up on generics altogether just because you don’t have variance is kinda over-reacting. Variance was mostly introduced because when the functions became values by themselves, you need to know what function type reference can hold what function types of objects, which is essentially a type hierarchy of functions. It does have some added advantage, but its not that java generics is completely useless.

Immutability does have a cost, the cost of creating lots and lots of objects that then need to be garbage collected. Garbage collection takes time just the moment you don’t want it to.

Stephen McConnell
Stephen McConnell
9 years ago

Early in my career as a developer, I was told by a very wise person to learn a new language a year. It changes how you develop in other languages. This is a great article explaining just that.

Back to top button