Functional Programming

When building any web apps, mobile apps or general software, you will write a lot of functions.

Side effects

Functions should be pure and simple, with no side effects. Which means a functions should only work with its parameters and modify nothing outside, not even the parameters.

$newArray = arraySort($oldArray);
function arraySort($array) {
   //sort the array and return a new array object
}
$oldArray = [];
arraySort($oldArray);
$newArray = $oldArray;
function arraySort(&$array){
  // work with the array object it self and modify directly.
}

The first example is a function with no side effect. In the second example, variable oldArray is modified after the execution of arraySort. This is the side effect. functions with side effects should be called procedure or process.

Side effect also include write to the files or DB or any global variables.

Unit Test on Functions

Unit test should test a single piece of logic of the system. It is a better practice to write simple and pure functions for easier and better unit test.

Unit test on procedures should be really called integration test. I  is a lot harder and more resource demanding to run and maintain. When your test suite takes hours to run, that will have an impact on your CI/CD.

Breaking up your code into many little functions would often result in another issue. You will find your code is calling functions which call another function. It makes the logic harder to trace since it abstract lots of details into function calls which now you can only guess what it dose by reading its name. Eventually you will end up with a structure like reverse pyramid. Some core functions at the foundation of the code base is now used everywhere, you hate to touch them anymore because you are too afraid.

Fortunately this is not true. Since your function is small, has no side effect and is well covered by unit test, you should be a lot more confident to update it as long as it passes unit test. This is the whole point here.

Too Many parameters

One common issue working with function is working with a set of data. when a function has more than 4 parameters maybe you should think about refactoring.

There is a pattern called Introduce Parameter Object, which is basically organizing parameters into struct, shape or class. It is all the same concept.

For example you have a function to display a introduction about a person based on his information

function getIntro($firstName, $lastName, $address, $phone)

What you should do instead is:

function getIntro(Person $person)

If you have a function that needs to combine 4 person’s name into whatever message.

function getMessage($firstPersonName, $secondPersonName, $third, $fourth)

Maybe you should think a bit higher and create a function that combine any number of people’s name

function getMessage(GroupOfPeople $group)

What if you have some strange requirement to work with a person’s name, a dog’s name, an apple’s color and an alien’s hometown. That could happen. Well, still the same way, you group them together into whatever form that make sense to you and everyone else.

Always think in strongly typed way

Generally if you working with strongly typed language like Java. The language force you to think about those kind of issue and you end up with better structured code. Well other language like php or javascript offers you ‘flexibility’ on that, which might be good for small projects. However in my opinion, code writing should be definite, you want people reading your code and knows exactly what it dose. not digging into debug console and dump all those ugly parameters.