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.
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.
- The code is easier to reason and most importantly unit test. If you are not returning anything, how do you test it?
- 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.
- 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 😉