Expression Oriented Programming

Knoldus Inc.
Knoldus - Technical Insights
2 min readMar 31, 2016

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

[code language=”scala”]
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”
}
[/code]

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 ;)

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com