Why should I avoid using local modifiable variables in Scala?

In your particular case there is another solution:

def max4(a: Int, b: Int,c: Int, d: Int): Int = {
  val submax1 = if (a > b) a else b
  val submax2 = if (c > d) c else d

  if (submax1 > submax2) submax1 else submax2
}

Isn't it easier to follow? Of course I am a bit biased but I tend to think it is, BUT don't follow that rule blindly. If you see that some code might be written more readably and concisely in mutable style, do it this way -- the great strength of scala is that you don't need to commit to neither immutable nor mutable approaches, you can swing between them (btw same applies to return keyword usage).

Like an example: Suppose we have a list of ints, how to write a function that returns the sublist of ints which are divisible by 6? Can't think of solution without local mutable variable.

It is certainly possible to write such function using recursion, but, again, if mutable solution looks and works good, why not?


It's not so related with Scala as with the functional programming methodology in general. The idea is the following: if you have constant variables (final in Java), you can use them without any fear that they are going to change. In the same way, you can parallelize your code without worrying about race conditions or thread-unsafe code.

In your example is not so important, however imagine the following example:

val variable = ...
new Future { function1(variable) }
new Future { function2(variable) }

Using final variables you can be sure that there will not be any problem. Otherwise, you would have to check the main thread and both function1 and function2.

Of course, it's possible to obtain the same result with mutable variables if you do not ever change them. But using inmutable ones you can be sure that this will be the case.

Edit to answer your edit:

Local mutables are not bad, that's the reason you can use them. However, if you try to think approaches without them, you can arrive to solutions as the one you posted, which is cleaner and can be parallelized very easily.

How to iterate over collection then in functional languages like Scala?

You can always iterate over a inmutable collection, while you do not change anything. For example:

val list = Seq(1,2,3)
for (n <- list)
  println n

With respect to the second thing that you said: you have to stop thinking in a traditional way. In functional programming the usage of Map, Filter, Reduce, etc. is normal; as well as pattern matching and other concepts that are not typical in OOP. For the example you give:

Like an example: Suppose we have a list of ints, how to write a function that returns sublist of ints which are divisible by 6?

val list = Seq(1,6,10,12,18,20)
val result = list.filter(_ % 6 == 0)