Jens Schauder is software developer since 1997. He loves software development for the constant challenges and constantly changing environment. A great chance to learn and teach. He is also blogger, author of various articles and speaker at conferences. Jens is a DZone MVB and is not an employee of DZone and has posted 86 posts at DZone. You can read more from them at their website. View Full User Profile

How Scala changed the way I think about my Java Code

08.15.2011
| 5797 views |
  • submit to reddit

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/

Published at DZone with permission of Jens Schauder, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Loren Kratzke replied on Mon, 2011/08/15 - 12:07pm

Curious about your last comment about switching on Strings. Are you saying that the act of switching on Strings is a joke, or the String switching implementation in Java 7 is a joke? If the latter, how would we be better off without it?

Jason Cone replied on Mon, 2011/08/15 - 12:13pm

Very much agree on the immutability appreciation. After dipping my toe into Scala for just a bit, I found myself creating immutable objects in Java much more often. (The other thing I noticed is how annoyed I get at typing verbose variable declaration/initialization code in Java, now.)

Jens Schauder replied on Mon, 2011/08/15 - 4:30pm in response to: Loren Kratzke

I was trying to say: Once you have seen the power of matching in Scala it doesn't really matter if you can switch on  Enums, Integer and Strings or only on Enums and Integers in Java. Either way the switch statement just feels incredibly limited.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.