Javascript Functional Testing — Pt.1
My original background is Java. During many years I have been doing my unit tests, the TDD-way: declare a TestCase, declare my unit testing methods, and for each individual method, write the necessary setUp() and tearDown() methods. Part of my current job implies that I search for technological solutions for problems we, software developers face everyday, and also make myself updated with the latest trends on this subject. For that particular reason I’ve decided to use this ‘medium’ as a way of documenting this process. My intention is to publish at least one article per week related to software development. As a starting point, I’ve decided to talk about three different subjects, that will be split in 3 different parts:
- Functional programming as a programming style
- Unit testing
- Immutable data structures
Functional Programming
The first part will target the Functional Programming subject — what it is, what are the alternatives and provide some background on it.
Early programming languages imposed an imperative style of programming. The whole idea behind this is that the programmer write statements that can change the state of the program being run. Procedural programming is a type of the imperative programming where multiple functions will be written as pieces of the software being produced. Those pieces of software can be all wired up through a main entry point of the application composing what we call a program. That’s the essence of structured programming. There are several examples of imperative languages — FORTRAN, C, Ada, etc.
In the 80’s & 90’s, object oriented languages’s increased popularity. C++ and Java are just two examples of that. The concept of structured programming is expanded through the use of classes. The main idea is to build a domain model using those classes which can represent everyday objects like a ‘Car’. Those objects can have attributes and behaviour. For example, a ‘Car’ can have the following attributes:
- brand
- color
- engine spec
- a license plate
That ‘Car’ can have some behavior:
- accelerate
- stop
- turn left
- turn right
The object oriented languages may offer a typed system that will ultimately improve code reuse and that will eventually have some impact on software maintenance.
Get Ivan Frias’s stories in your inbox
Join Medium for free to get updates from this writer.
Functional programming on the other hand, is a programming style that follows a declarative ‘way’ of doing this, so we can say that it is declarative by nature. Like a mathematical function it expects an input and return an output no matter the circunstances. That leads us to another characteristic of functional programming — it’s purity. That purity means there should be no side-effects from the usage of a particular function and that only happens because the scope of the mutated data should exist only for a particular function. A simple analogy would be the Car Factory. Someone supplies the factory with raw materials and hopefully in the end there will be a car. That same car should represent the same outcome everytime — a product that allows the transportation from A to B.
There are several examples of functional languages out there: Common Lisp, Scheme, Clojure, Herlang, Haskell, F# to name a few. Some imperative languages also provide additional constructs that can used in a functional way: e.g. Perl, Java 8, C# ,etç. The benefits of functional programming can be resumed to the following points:
- Modularity
- Testability
- Less Race conditions
- Faster
- Easier to read
And that’s it for today. Next part will focus on Unit Testing as software development discipline. I will be talking about how we can testcases using TDD and BDD approaches and what are the differences between those two approaches.
References:






