Day 13: Write Functions in a Functional Style

Mathematical functions are a simple transformation from inputs to outputs. Given a set of parameters, a function performs a transformation based on these parameters, and returns the desired results. Because of their simple character, mathematical functions are a good analogy for what can be done by a computer. Since they are independent entities, functions can be used anywhere, whatever the context is. For example, one can use the sine function to calculate the sine of any angle, no matter where the angle occurs.

Functions in programming languages are usually not so nice. They are frequently defined not only by their parameters but also by the environment where they occur. For example, if we define a function to return the amount of memory used by the application, it will necessarily have to search for information that is particular to the programming environment.

A more insidious problem with functions in programming languages is that they may change the environment where they run. For example, the previous function could very well change internal data structures in order to get the required memory information. The next invocation can, for example, use internal data instead of accessing the disk for this information. Whenever this happens, the function is said to have side-effects.

Using functions with side effects may lead to several problems. Many of these problems are debated by researchers on functional programming theory. But the main practical problem is that it is hard to understand such a function in isolation, because it may change other parts of the program. As a result, one needs to consider the program as a whole when checking the validity of a function with side-effects, instead of just looking at its definition.

Object oriented programs make this process a little simpler, because, if properly used, methods can only modify data that is part of the object. But still the issue remains, especially when programmers are not careful about how objects are manipulated.

Functional Programming to the Rescue

Functions that follow the mathematical model are said to be pure functions. There are whole programming languages, such as Haskel and F#, that have been developed in order to explore programming with pure functions. While most people don’t work with functional languages, we can still benefit by using a functional-oriented style.

One of the rules used to approximate the functional style is to avoid side effects in programs as much as possible. One can not always do this, but the general strategy consists of dividing functions into two classes: the ones with side effect and the ones with no side effects. The principle is that each method or function should have only one of these purposes.

The simplest example are getters and setters. A getter is a function without side effects because it just returns the value of something inside the first object. On the other hand, setters are free to make lasting changes to the environment. From this point of view, we see that making this separation is good not only for style reasons. It helps a lot when we known that our functions are classified by their use.

The next step is to minimize the number of functions with side-effects. By doing so, it becomes easier to understand how a program works: one can look only at the current function or method to have an idea of what is going on. It is also easier to test functions that have no side effects, because they can be tested in isolation from the rest of the program.

Finally, a program written in this style is easier to change. Functions and methods in such programs have only local effects. Thus, if something needs to be changed, we need to look only at the inputs and outputs of each function, not to the whole program.

Summary

By understanding the difference between functions with side effects and pure functions we can develop a more systematic way to create software that is easier to test and change.

Although there are whole languages that use functional programming as its main style, we don’t need to use functional languages to benefit from the idea. Separating functions between pure function and function with side-effects we can capture many of the advantages of functional programming, while keeping with whatever language we are using for the current project.

Further Reading

There are several books about functional programming the ones I like most are:

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

Post a Comment