Thursday, April 7, 2011

Like C language and hate Java? Check Scala!

I've recently read a C praising article, that rants at Java:
http://www-cs-students.stanford.edu/~blynn/c/ch08.html

Author seems to take it personally and is more biased than a scientist should be. But he is mostly right in his java rant.

In another chapter he mentions C lang problems, that he beliefs could be walked around. And this is where he is mistaken. Most C programmers read glibc and linux code and take it as example. Since there are obscure macros there, most programmers will write the same. The result is that coding C is no longer simple and will never be. Obscure macros will make any language fall (and fail) - and this is why Clojure will vanish after few moments of glory.

Most blows at Java are blows at lack of functionality, speed and inheritent code bloat. Java also indeed does some things the worst possible way, namely multiple inheritance.

Author is forced to admit that Garbage Collector and code divided into packages are seriously importand features of Java platform. If he ever did any web deployment, he'd also notice that jars, wars and ears packaging in J2EE are unique and save a lot of hassle. No other language has working deployment out of the box (maven war - one liner that builds the world).
Ooh.. maven :) Another killer app. In fact so good, everyone want to reinvent it.

Let's go through C wishlist, having Scala in mind.

Scala is functional (mostly, pragmaticaly they allow object state and imperative loops) programming language written in Java and running on JVM. You get J2EE packages, deployment and state of the art Garbage Collection for free.

Scala has also one nice feature - it's C-style formatted language

  • Package system -> inherited from java
  • Text substitution keyword instead of #define -> luckyly Scala has no preprocessing macros, so we don't need to fix that
  • Multiple return values -> just return an inline-built object. Side note: In python you'd just unpack a tuple:
    edata, newfunc, value = fReturnTuple()
  • Anonymous functions -> lambdas all the way in Scala, go ahead
  • Multi-line literal strings -> Scala has exactly the same multi-line operator """ as python
  • Flexible array ranges (not starting from 0) -> C-only problem. Real world has enums, iterators...
  • Regular expressions -> the author should take a deeper look at regexp librariers. RE is no longer slow. A missfire.
  • Reflection -> lol, actually someone here likes java more than he admits. Sure, there is reflection in scala
  • Closures -> full support in Scala
  • Way to detect overflow -> JVM anyone?

Summary

C language is broken. Has no namespaces (all functions are visible everywhere), most code uses obscure macros. Pointers syntax is complicated to a perl-level. C is error prone. C requires deeper understanding of machine and memory (ever switched 32bit code to 64bits?) that is practical for average programmer to learn... Only bad things happen when you program in C.

Instead of fixing Java with C, and wishing C had JVM features, one should look at Scala.