Exit Full View

Variables versus Values

From Comparing Programming Languages.

There are three different places you find "variables" : class fields, local variables and function arguments.

In Java a "variable" can be $$final$$, which prevents it being re-assigned. C# uses the same semantics but uses the $$readonly$$ keyword. Kotlin is different, it has two keywords $$var$$ meaning it can be re-assigned and $$val$$ meaning it can't.

I really like the Kotlin style. In fact I'd go so far as saying that Java and C# are quite simply wrong!

If I review my old Java code, there is no information that tells me if the $$final$$ keyword was omitted on purpose, or through lack of thought. What's worse, once I have reviewed all of my code, there is still no information that tells me that each "variable" has been considered.

I think names matter, if you use the wrong name, you will tend to think incorrectly. C# uses the keyword $$readonly$$, and IMHO, this is wrong. You can define a List to be $$readonly$$, but that only means it cannot be re-assigned to a different List. This List is still mutable. Neither Java's $$final$$ nor Kotlin's $$var$$ / $$val$$ keywords are misleading.

If you are keeping score : Kotlin 2, Java 1, C# zero ;-)

Kotlin has taken a more radical approach to function arguments - they are never re-assignable. I'm undecided if I approve of this. Time will tell.