When to Program Functionally
If you have used only imperative programming languages so far, functional programming will feel uncomfortable and strange. It may do so for a while. Why should you learn about functional programming then? Here's a list of reasons, some pragmatic, some more idealistic:
-
Programming by function composition means that the output of a function is completely determined by its input. Functional programmers call this referential transparency. This simply means that there is no way for a function to refer to some global state whose contents may influence the function's behaviour. Passing information between program components indirectly via some shared state makes your program's data flow potentially hard to follow. Moreover, incorrect manipulation of this state is the root cause of the vast majority of bugs and security vulnerabilities of computer programs. Programming with pure functions eliminates this important source of bugs.
-
Many mainstream languages such as Python, Java, Rust, C++ borrow features from functional programming because of the advantage of referential transparency just outlined. While you may not program in a functional language for a living, the ideas of functional programming are here to stay, even in languages that are as such imperative. So you better learn what it means to program functionally so you can use modern, functional features of otherwise imperative languages effectively.
-
If you like math, then it's just cool that we can program simply via function composition.
-
You may never use a functional language in your industry job, even though the number of companies choosing functional languages for their projects is growing. You may not use a finite automaton or push-down automaton in that job either. Yet we make you learn about the latter, because that's part of a well-rounded computer science education. A university degree is not just about training you for your first job. It is about providing you with an education, about helping you develop a deeper understanding of computer science, so you have the tools to adapt to new developments in computer science or maybe even be a trailblazer and contribute to these new developments yourself over your 30+ year career. Just as the theory of computation is part of this education, part of this fabric of computer science, so is functional programming.
-
As you keep programming functionally, you may reach a point where you resent having to reach for an imperative language again when you do. If you really grok functional programming, it feels more elegant, and imperative programming feels pedestrian and tedious in comparison, at least for a significant fraction of problems. It may take a while for you to get there or you may never embrace functional programming. It is certainly worth a try though.
So is functional programming just better all around than imperative programming? No.
-
Imperative programming gives us much tighter control over our program's execution. When performance matters or when programming system-level code, we may need this level of control. In that case, we won't program in Python or Java either though. It's all the way to C, C++ or Rust then.
-
If most of what our program does is input and output and there are few non-trivial transformations it applies to the data between reading it and spitting it back out in some transformed state, functional code will look rather imperative: it ends up being a sequence of steps that perform the input and output. That's often more convenient to do in an imperative language, because imperative programming is all about sequencing steps; functional programming is all about transforming data.
-
Some algorithms—among them many of the ones you learn in CSCI 3110—are inherently imperative. You can express them functionally, but only if you accept a logarithmic slow-down and the program may look clunky even then. There are a number of seemingly inherently imperative algorithms though that can in fact be expressed very elegantly, possibly more elegantly, as a functional program than as an imperative program. It all depends on the algorithm.
There are numerous other scenarios where an imperative language is the more natural choice. Functional programming is simply a tool to add to your toolchest. You wouldn't want to omit the power drill from your set of tools only because you already have a hammer and know how to use it.