Expression Oriented Programming

In a conversation with one of the lead architects of a large publishing company, we were discussing around the coding standards and suddenly the term EOP brought the discussion to a standstill. Ok, just for a few seconds. Once I explained it, it was like “Ah, is that it ?” If you are doing Scala, you can be assured that you are good with it already but let us define it anyway.

With Scala one of the basic premise is that we would be writing purely functional code or rather code in which 80% of the code is pure functions and 20% is reserved for things like I/O. This way we end up following the rule of 80/20 with functional programming.

Scala is mostly EOP, well, OK it does have statements but the basic philosophy is EOP. What is the different between a statement and an expression. A statement does not return a value but an expression does.

employee.calculateSalary() is a statement
val salary = calculateSalary(employee) is an expression

All pure functional programming languages are expression oriented. That said, though Scala is an impure functional programming language but it is largely expression oriented. Almost everything in Scala returns a value.

For example

val x = {
  val a = 10
  val b = 90
  a + b
}

if (1 < 10) "Hello" else "Hi"

var t = 10 match {
case 11 => "Ricky"
  case _ => "Rocky"
}

All of them are expressions.

Writing expressions like this is feature of most functional programming languages, and Scala makes using them feel natural and intuitive. This also results in concise, expressive code.

There are several benefits to EOP.

  1. The code is easier to reason and most importantly unit test. If you are not returning anything, how do you test it?
  2. Since expressions do not have any side effect, they could be executed in parallel thereby increasing the parallelism factor of the code thus helping us with the multi-core world.
  3. The resultant code is concise and expressive.

So next time when someone asks you about EOP (Expression Oriented Programming) and you are doing Scala then you know that you are covered 😉

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

3 thoughts on “Expression Oriented Programming

  1. So what is the difference between expressions and functions? To me at least [Expressions <: Functions]

    1. Andrea, sorry for the delayed response. I would say on the verge of being slightly incorrect that all functions are expressions but all expressions might not be functions (in the pure ()=>() ) sense

Leave a Reply

%d bloggers like this: