Over a million developers have joined DZone.

How Scala changed the way I think about my Java Code

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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 then 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 then 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.


From http://blog.schauderhaft.de/2011/08/14/how-scala-changed-the-way-i-think-about-my-java-code/

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}