Functional Programming
FP boils down to two things: side effects and determinism. These concepts form the basis of what we call FP, and they are also the easiest things for newcomers to grasp in this paradigm because they don't introduce new, complex patterns.
Side Effects
When writing a program, we often strive to get some form of side effect – a program without side effects is a very dull program, as nothing would happen. However, side effects are also a common headache when trying to test a program reliably as its state may change unpredictably.
A very useful class in Java is the Math
class; it contains all sorts of mathematical helpers and is likely to be used in all Java applications, either directly or indirectly. Here is an example of printing a pseudo-random number to the console:
public static void main(String[] args) { System.out.println(Math.random()); }
If we dig into the code of Math.java
and review the details of the...