Monday, March 21, 2011

Scala for Java developers

I noticed that while migrating to Scala I often tend to write a code in old Java style. For example I write loops when map-like functions are more appropriate or excessively use exceptions. Other common mistakes that I can mention are:

  • Using own getters/setters instead of methods kindly generated by Scala
  • Using null instead of Option
  • For me it is easier to follow functional programming style encouraged by Scala when functions return non-Unit type
  • It is also a good practice to use var only when it is really needed, for example for counters or some other store of the current program state
  • Finally, Vector in Scala is not an outdated class as java.collections.Vector is, but a powerful Scala collection with "effectively" random access time and "effectively" constant time for insert/delete operations