Scala is a multi-paradigm language. It has characteristics of object oriented programming, functional programming, meta programming (language oriented programming or DSL). So a java programmer might write scala code which looks completely different than a Clojure or Haskell programmer writing the same scala code. This is because java developer might use more of Java style (mutable variables, for loops) whereas the developers from functional programming style might use more of functional aspect of scala.
Here is a sample example, different ways to calculate sum of a List of Double.
As a result, a new developer gets confused which is the right way of doing thing. Think that you write a piece of code and gave it for review and even reviewers fight over the right way of doing it.
Overall as a rule of thumb, I think it is better if we consider Scala as a "functional language" and try to use the best practices from functional world. This is why I have started advising Java developers to learn one functional language like Haskell or Clojure before picking up the "Scala" book. This tunes java developers mind towards functional world and come out of habits of using mutable variables or explicit for loops.
This is however a delicate topic and would love to hear more from users who transformed from Java to scala developer.
Here is a sample example, different ways to calculate sum of a List of Double.
class Test {
def sum(data : List[Double]) : Double = {
//uses a mutable variable to produce the sum, this is most of the java developer might use
//but it is not the best way to do so. Because most of the functional language does not like
//to have mutable variable
var sum : Double = 0
data.foreach( v => sum = sum+v)
//Fold and Reduce are two techniques calculate the value at one shot (without mutable variable)
val totalSum = data.foldLeft(0.0D){ (total,n) =>
total + n
}
val anotherSum = data.reduceLeft[Double]{(acc,n) =>
acc + n
}
//now short hand of fold and reduce. Underscore is positionally matched arg
val yetAnotherSum = data.foldLeft(0.0D)(_+_)
println("Sum = " + sum + " total sum = " + totalSum + " and another sum = " + anotherSum + " yet another sum = " + yetAnotherSum)
return sum
}
}
As a result, a new developer gets confused which is the right way of doing thing. Think that you write a piece of code and gave it for review and even reviewers fight over the right way of doing it.
Overall as a rule of thumb, I think it is better if we consider Scala as a "functional language" and try to use the best practices from functional world. This is why I have started advising Java developers to learn one functional language like Haskell or Clojure before picking up the "Scala" book. This tunes java developers mind towards functional world and come out of habits of using mutable variables or explicit for loops.
This is however a delicate topic and would love to hear more from users who transformed from Java to scala developer.