small medium large xlarge

Back to: All Forums  PragPub
09 Mar 2012, 19:32
Guilherme Silveira (1 post)

Its right, Scala code is much smaller.

But, as we can see, both Java and Scala are composed by a single block of conditionals, loops and IO, everything in one method.

What I worry about is that people move out from writing not good code in one language to writing not good code in another language. The Java solution is horrible and not even what a Java developer should write. Using nothing but java:

After 12 years of Java, one should not use those Java techniques, they are old. Some are even C techniques (declaring the variables ahead, without any need).

One could also make use of libraries to make the Java code easier to understand, such as any collections library.

And thats what I am afraid of. Writing code that is hard to understand in one language, migrating to another, and writing code that is still hard to understand.

Comparing two languages, without its environments, just does not make sense. Not in 2012. Not leaving behind several other facts, such as scala compilation time and feedback cycle and much more.

One definitely writes less code in Scala, as we can see in our current project (+1 year) but both in Java and in Scala, the code presented is not a good example.

It is not the number of characters that you type that makes your program great. It is the good practices used.

09 Mar 2012, 22:24
Juan Lopes (1 post)

I tend to agree with both arguments, but for different reasons.

Completely agree that the code should be broken into logical pieces that interact with others by well defined contracts. But I think the article Guilherme pointed wasn’t only trying to show what one can do in less keystrokes. It tried to show how the APIs in Scala are more concise than the ones in pure Java. Specially the sequence APIs.

Guilherme argued we can use lambdaj, lazyrecords, etc. to achieve the same functional writing, and I agree with that, but we cannot deny Java is postponing some essential language features for too long. It’s becoming an old language with a modern virtual machine. That is, we still cannot write expressive funcional code in Java the way the language is (without lazy lists, lambdas or closures).

It doesn’t mean the code in Java will be ugly all the time, specially if you consider that Java strongly discourages the one-liners, that make most code unreadable. But many trivial tasks (e.g. traversing a collection) have proven to be written more easily (and in a more readable way) using the functional approach.

I think this is the main reason why Java developes find themselves so amazed by languages like Clojure and Scala, that give them the expressiveness Java is lacking.

Again: it doesn’t mean the code in these languages will have a better design, as it depends more on the developer’s maturity than the language expressiveness, but it wouldn’t hurt if Java gave us some features to make the code itself prettier.

As an exercise, I leave here my as-concise-as-possible implementation of the proposed problem, in C#.

10 Mar 2012, 19:46
Brian Tarbox (41 posts)

I absolutely agree with the statement “It is not the number of characters that you type that makes your program great. It is the good practices used.”

I’ll just observe that the Java code I showed is representative of the code I tend to see. This isn’t to argue that its “good practice”, quite the opposite, but its what I see getting checked in.

You must be logged in to comment