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 😉

About Vikas Hazrati

Vikas is the CTO @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. We do niche product and project development on Scala, Spark and Java. We consult and coach on effective software development and agile practices. With our focus on software craftsmanship you can be assured of a good quality at the right price. To know more, send a mail to info@knoldus.com or visit www.knoldus.com
This entry was posted in Scala and tagged . Bookmark the permalink.

3 Responses to Expression Oriented Programming

  1. Andrea says:

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

    • 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s