More Expressions, Fewer Statements

Languages like Java, C#, and JavaScript have more statements than expressions—if statement, for statement, try, and so on. On the other hand, languages like Ruby, F#, Groovy, Haskell, and many others have more expressions than statements. Let’s discuss which is better before we discuss Kotlin’s preference.

Drawbacks of statements

While statements are prevalent, they have a dark side—they don’t return anything and have side effects. A side effect is a change of state: mutating a variable, writing to a file, updating to a database, sending data to a remote web service, corrupting the hard drive… Expressions are much nicer—they return a result and don’t have to modify any state in order to be useful.

Let’s look at an example to see the difference. Let’s write a piece of Kotlin code as we would in languages like Java and C#:

Get hands-on with 1400+ tech skills courses.