Tuesday, July 22, 2008

What Has Scala Got To Do With Being A Boiled Frog?

I'll take the analogy back to its analogue.

With limits approached on processor core speeds, multi-core processors have arisen to provide the additional performance that cannot be squeezed out of individual cores. That means that on my computer one application can be running in one core, and another in another core. If my computer has two cores, it seems that it is running those two applications twice as fast as it would have done if it had only one core.

That's true. And with each doubling of the number of cores in my computer's processor, it will be able to run more applications and all will seem well and good with Moore's Law -- from a computer user's perspective.

Computer users are not the potentially boiled frogs. It's computer programmers who will begin to feel those tiny bubbles swirling around their nether regions as they float around happily in a pot of soothing warm water with convective currents tickling their toes.

And guess what? All computer programmers are computer users. From a user perspective we see our computers getting faster. In that respect we are surprisingly good at seeing things from a user point of view. The irony is that in this case we should not be guiled by user experience. We have to think of ourselves for a change.

Our individual programs are not running much faster. Unless you are writing Fortran or Fortress (eventually) on a supercomputer and your code is pipelined by the compiler to be parceled out to multiple CPUs, then your programs are running in a single core. And those cores are likely to experience only marginal speed improvement in future now that some physical limits have been reached.

With Scala (and Erlang) it is possible to write functional style programs that are thread safe and more capable of utilising the power of multiple cores so your programs will run faster. Faster than those who don't adopt the functional style of programming. There's a lot coming to mind in this one sentence. Sure, you can write multi-threaded programs in Java. But it's a bit mind bending. And yes, it's a steep learning curve going from object oriented and procedural programming to functional programming, but the payoff may be immense. And you have to get yourself out of that pot before it's too late.

Where Scala is different is that it's a hybrid language that blends both object oriented and functional language facilities. That means you can begin learning Scala from a Java perspective and dip your toes into functional programming at your own pace. And you won't get your toes boiled. And because it runs on the JVM (like Groovy and JRuby) you can use all your familiar Java classes, and import from the amazing array of class libraries available for the Java platform.

And you could write one or two helper classes in Scala and use them into your Java programs, just to get your feet wet.

Are you dreaming wistfully of the next major advance in physics or nanotechnology that will lift your gently out of your predicament with a quantum leap in core performance? The steam may be already dulling your senses.

Let's assume that Moore's Law resumes its pace at the core level. Multi-core processors are not going to go away. As in economics or biology, once a business or plant variety has spread, it tends to take root and retain its selective advantage. A multi-core processor will always be better than a single-core processor. That won't get you out of the pot. Multi-core is here to stay. Programmers who realise this and take advantage of multi-core programming languages and techniques will do better than those who don't.

Don't be a boiled frog. Learn Scala now!

Here's a link I have found useful as I begin to transition from Java to (Java + Scala).

Scala for Java Refugees by Daniel Spiewak is a series of posts about Scala from a Java point of view. The comments too are of high quality, and he makes corrections based on those comments.

Wednesday, July 2, 2008

Groovy or Scala? Don't be a Boiled Frog!

Both!

Groovy is a fabulous language. Just read Groovy In Action and some of the other Groovy books. Just about every page brought a Wow response from this Java developer. And the Grails web stack builds on Groovy to provide a quick and powerful website building experience.

With Groovy I've written several scripts to do JSON processing. Groovy allowed me to choose an existing well tested Java class library and use it directly in my script. There was no sifting through CPAN-like libraries of varying quality and performance. And in Groovy I could throw in a few assertions and try-catch blocks. Try doing that in a shell script.

And Scala? It's harder to find documentation that is accessible to Java developers. But learning Groovy makes it easier, because there are some similar concepts.

Ted Neward has written some accessible Scala tutorials at DeveloperWorks, such as The Busy Java developer's guide to Scala: Functional programming for the object oriented.

Most of the Scala documentation that I've seen takes you to the deep end of the swimming pool very quickly. And some constructs are not well explained. For instance, what does => mean? And does it have multiple meanings in apparently syntactically similar formats?

But I find Scala intriguing, and I read on, and write small educational Scala scripts and programs despite the steep learning curve. If there were no new concepts, why would a Java developer bother learning Scala? (To paraphrase Ted Neward.)

Groovy has a gentle learning curve and the payoff in programmer productivity is rapid.

Scala (for me, a Java developer) has a steep learning curve. Is it worth it? The intellectual pleasure is substantial. Significant amounts of Scala documentation are based on Structure and Interpretation of Computer Programs which is itself a pleasure to read and keeps you thinking.

Probably type safety, versatility and performance alone make Scala well worth a close examination. Type safety doesn't eliminate the need for testing, but can catch subtle and silly bugs. Versatility: you can script it (like Groovy), delve into functional programming, and you've got all the Java libraries available to you (like Groovy).

Speed may be the killer feature. I've seen performance stats that indicate Scala is about as fast as Java. And with immutable objects and the Actor class library, it is possible to write safe multi-processor programs. This means that you can write software that can take advantage of multi-core processors.

Scala is already fast. And with its multi-threading capabilities it will get faster, along with other languages such as Erlang and Fortress.

As single-core developers we are like frogs in the pot of warm water heating gently to boiling point. For a while we are developing and deploying on machines using yesterday's high-end processors. But with Moore's Law stalled at the level of the individual core, we will fall behind the multi-core performance possibilities if we don't learn to program in multi-core environments in a scalable way. Using Java's existing multi-threading libraries is not a practical option for most developers. It's hard. And that's what Scala (and Erlang and Fortress) makes easier. Not dead easy, but easier.

Learn Groovy. Learn Scala.

Don't become a Boiled Frog!