What is Functional Programming?

2 minute read

In this blog, we will talk about the nature of Functional Programming.

Effect and Side-Effect

In the world of Functional Programming, we are familiar with the Side-Effect1 of function, it may means such things

  • Modifying a variable
  • Modifying a data structure in place
  • Setting a field on an object
  • Throwing an exception or halting with an error
  • Printing to the console or reading user input
  • Reading from or writing to a file
  • Drawing on the screen

But we rarely mentioned Effect. Let’s try to find out in the following section.

What is Effect?

In most cases we talk about the effect of function.

Extensible Effects: an alternative to Monad Transformers2 give some definition of Effect

An effect is most easily understood as an interaction between a sub-expression and a central authority that administers the global resources of a program.

An effect can be viewed as a message to the central authority plus enough information to resume the suspended calculation.

It’s a little bit long and hard to understand for me, I would like to say An effect is just a message from the inner of function to the outside of function

For example

The effects of add function to the outside are returned Int value 1 and console output Input is 1

Usually we call the effect not sent by the return expression as Side-Effect.

Pure Function

The definition of pure function is

  • For all input, same input produce same output
  • No Side-Effect

The first rule should be easy to understand. For example, if we have a function like this

def addRandom(x:Int):Int = {
  x + Random.nextInt
}

It produce different output for same input every time. How should we test it? we can’t predict the output according to input, then we can’t give the expectation.

The no Side-Effect in the second rule means all the message from the inner of function should be sent by the return expression or we can know all the message of the function according to its return type

For example

The return type of add is Int, so I know the add function will only return an integer and I don’t need to care about if it will throw exception or return null, then I don’t need to add more code to handle these scenarios.

If we have a function with console output like this

How do you test the console output? if we don’t care about it, why do we need this code?

If we have a function with exception like this

How do you ensure all the consumer of this function to handle the exception?

Functional Programming

Ideally Functional Programming means construct programs using only pure function. But this is not possible, we always need to print log, send http request, read file, save file…., without the Side-Effect our program can do nothing.

So let’s make a workaround, Functional Programming means construct most parts of programs using only pure function and centralize the parts with Side-Effect(usually we will put them in main function).

Seems it’s pretty simple, but it’s not easy to make every function to be pure. To achieve this, we need to involve lots of Algebraic Data Type and Type Classes. Let’s talk about them in other blog.

References

Comments