What issues do we concern when implementing and maintaining systems?
One of the most concern is debugging during maintenance: "this code crashed because it observed some unexpected value."Then, it turns out that the ideas of no side effects and immutability, which functional programming promotes, can help.
Shared mutable data is the root cause
Shared mutable data are read and updated by more than one of the methods. Share mutable data structures make it harder to track changes in different parts of your program.An immutable object is an object that can't change its state after it's instantiated so it can't be affected by the actions of a function. It would be a dream to maintain because we wouldn't have any bad surprises about some object somewhere that unexpectedly modifies a data structure.
A new thinking: Declarative programming
There are two ways thinking about implementing a system by writing a program.- Imperative programming: has instructions that mimic the low-level vocabulary of a computer (for example, assignment, conditional branching, and loops).
- Declarative programming: you give rules saying what you want, and you expect the system to decide how to achieve it.
Functional programming = side-effect-free + declarative programming
Functional programming exemplifies the idea of declarative programming ("just say what you want, using expressions that don't interact, and for which the system can choose the implementation") and side-effect-free which is mentioned above when using immutable objects approach.Reference
Alan Mycroft and Mario Fusco, Java 8 in Action: Lambdas, Streams, and Functional-style Programming. Part 4. Beyond Java 8.