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 :
Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

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


nine + = 10



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books