Brief history of software engineering
At the beginning of the software programming, the code can only be executed on the specific target machine which coupled to the hardware(yes, it’s machine code). It’s hardly to be ran on another machine. In fact there is no this kind of requirement, since the computer is handful all over world at that time.
As computers are getting cheaper and more popular, it is a trouble and tricky. The first evolution is codes are separated into two parts: high-level abstraction(from machine), and native assembly code(face to machine). The latter one is transparent to us, translated from the former one. The intermediate translator we call it “compiler“. The high-level abstraction is the language what we coding directly like COBOL, LISP. Since then there is a clear border between software engineering & hardware engineering.
Very soon the language C founded its kingdom, and it’s still popular & mainstream in nowadays. For C is well known as process-oriented programming, highly structured code style and flow. The code could be reused in the unit of functions, this is big improvement, but still there is an elephant in room that function is coupled with state: to keep state data the variable must be set global, it’s dangerous that all other functions have ability to change it as well.
To resolve this kind of issue, the languages(like C++) encapsulate data & function(method) into one unit which called “class“, this is object-oriented programming. The basic reuse level is class, by reference it directly or from hierarchy (inheritance). After so many year’s development, we all found that OOP is meeting the complex wall.
![Abstract Factory Pattern of GOF](/2017/11/28/FP-JS/Why-Functional-Programming/Abstract_Factory_Pattern.jpg)
What is Functional Programming
Focus on result over steps.
- Function is first-class citizen. Function can be passed around(as a parameter or returned result), create dynamically, cached as data structure, even compose whole program. (LISP is an example.) Create function is low cost and convenient. Some methods like lambda expression, anonymous function can help achieve this goal.
- Favor pure function. Pure function is the function that has no side effects, the output is always same with given input, despite of any state changes from external of function, as well as won’t change external state as well.
- Favor expression over statement. The statement executes and returns value immediately, but expression yields value(lazy evaluation).
- Immutable data. To good for pure function and state management, the data(variable) is usually immutable. Transform/create new data instead of modify existing data. One data structure is put into a function, and a new data structure comes out. This is explicit contrast with OOP, as what we talked above, OOP packs mutable state and operation into an object. One more benefit is that focus on immutable state will lead the code in more declarative style.
Below is am example to filter out the odd numbers and sort them:
1 |
|
below is pure functional programming version:
1 |
|
below is clojure version:
1 |
|
Voila !!!
So simple solution, so semantic code :)
Declarative vs Imperative Programming
From name we can assume the declarative programming focus on “What”: declare to computer what I want, no care of detail workflow; the imperative programming focus on “How”: telling the computer how to to it.
The SQL could be your most familiar codes about declarative:
1 | select name |
translate to javascript is this:
1 | let names = students |
Below is the way of imperative programming:
1 | let names = [] |
As you can see, the declarative programming could help us to describe what we want, no need to pay attention to the detailed implementation. The benefit is not just reduce lines of codes, performance potential improvement, but to lead us focus on higher level of abstraction to resolve what we want. Leave the implementation specific to pattern/framework/language, let us programmers handle the real problem.
Language Development Trends
Next generation of software programming
Let’s continue the history of software engineering, on the side of software design it’s meeting complex wall; on the side of software development(coding, debugging) programers ware suffering from manually memory allocation/release (garbage collection). To resolve the pain point, soon the garbage collection became one of standards of almost all following major language, such as Java/C#…
And functional pattern was included as major feature, lambda expression could be regarded as milestone, it was added in C++ 11, C# 3.0 by 2013; Java 8 by 2014… And for pure functional languages, such as Clojure(dialect of Lisp), Scala, F# were won lot’s of attention and market share as well.