forEach, map, filter, reduce, find, findIndex, some & every

Functional Programming is a paradigm where programs are constructed by applying and composing functions. The Functional Programming keys are:

  • You can pass them as arguments to other functions.
  • A function can return another function.
  • You can modify functions.

When a function is called with some given arguments, it will always return the same result, and cannot be affected by any mutable state or other side effects.

And now, we are going to take a look at the most common JavaScript functions that allow us to write Functional Programming: forEach, map, filter, reduce, find, findIndex


When PHP meets FP 🚀🚀🚀🌚

What is Functional Programming?

Functional Programming (FP) is a programming paradigm that was created in the late 1950s. It’s even older than Object-Oriented Programming (OOP).

The main concepts of this paradigm are:

  • Recursion: There are no loops. In order to get this approach, the functions can call themselves using recursivity.
  • Functions are First-Class: A function is treated as a variable, which means you can pass functions as function arguments.
  • Variables are immutables: A variable cannot change its value once it is declared, but it’s possible to create new ones.

Imperative vs Declarative

Let’s start with the…


Practical examples of immutability in PHP

A mutable object can be modified after its creation, an immutable cannot.

An immutable object will remain in the same state as it was created. Design and implementation will be much easier and consistent. In the case of problems, locate a potential bug is faster due that it won’t have side-effects.

Although, creating immutable objects sometimes require more code, and it doesn’t fit in all scenarios (entities need to be mutable for example).

Mutability examples and their impacts

<?php declare(strict_types=1);final class ProductTransfer
{
public function __construct(
public string $name,
public float $price,
public array $tags,
public \DateTime $releaseDate,
) {}


Being up-to-date and avoiding conflicts

Have you ever wondered how you could collaborate with open-source projects but you didn’t know how to start? It couldn’t be easier. Take a look:


Dummy, Stub, Spy, Mock or Fake

Test Doubles

A Test Double is an object that can stand-in for a real object in a test, similar to how a stunt double stands in for an actor in a movie.

As I wrote in “The importance of the Tests in our Software”, there are several types of tests. They are also known as Test Doubles instead of “Mocks”.

The five types of Test Doubles are:


TL;DR: No, and let me explain you why.

It causes bugs when refactoring due to the highly coupling

When we use reflection our tests get too fragile, we are allowing our tests to know so much information about the real implementation.
We need to hard-code the method name and we are coupling our test method to the production code.
Furthermore, we need to write a lot about boilerplate to test a simple method.

Q: I need to get at least an 80% of code coverage, how can I get it without Reflection class?
A: You should test ONLY your public methods and depend on the variables we pass, we should reach…


1.- What is a test?

A test is an empirical assertion that demonstrates the behaviour of an expected functionality from something.

The tests are classified by what they verify, the most important ones are the following:

Functional tests

  • Component test
  • Integration test
  • System test
  • Smoke test

Non-functional test

  • Security test
  • Stress test
  • Usability test

A software needs all of them, but the most important concerning developers are unit, integration and functional tests.

2.- Differences between test types

Unit test

Jesus Valera Reales

Competitive, entrepreneur and autodidact. Hard worker, lover of technology and free software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store