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 :

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "How Scala changed the way I think about my Java Code"

  1. Debasish says:

    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.

Leave a Reply

two + 4 =

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: