DEV Community

Cover image for Don't do this with your parameters in functions - functional programming🤢
PythonIsNotASnake
PythonIsNotASnake

Posted on

Don't do this with your parameters in functions - functional programming🤢

Functional programming is a different concept than object oriented programming. For example Java is object oriented meanwhile JavaScript is functional. Additional there are hybrids like Python which allow to use the language in a functional or object oriented way. In all this cases you have functions (for functional programming) or methods (for object oriented programming) which get parameters as input and return a value as output.


First I show you an easy example of a function and then we will analyze what we can do better.

function doSomething(p1) {
  p1 = p1 * 2;
}
Enter fullscreen mode Exit fullscreen mode

What can we see here? We see a function called doSomething which doubles the value of p1. Which problems can we identify when we look at the function? There are three main points.

1) Function name doesn't describe anything🧐

The first issue is the name of the function. If you read doSomething anywhere in a project you will never know what this function is doing. Best practice is to use function names with a describing name of the purpose or of the processing steps the function is doing. In our case a name like multiplyByTwo or doubleNumber is a much better choice.

2) What means p1?😵‍💫

The next issue is the name of the parameter. If you have only one parameters this is not so dramatically. But if you have three, four or more parameters no one can understand which parameter have which effect on the process or result of the function. In this case multiplier can be a valid choice or anything else which can be understand very well.

3) Never change parameter in function body🤬

You never want to change the input value in a function. It is best practice to create a copy of the input value and then do all the operations on this copy. At last return the copy. So the input value has no changes at all. And if anyone need the changes on the input value then the input value can be changed by the return value of the function.


Now we have evaluated all the issues and can make it better in a new try:

function multiplyByTwo(multiplier) {
  var copyOfMultiplier = multiplier;
  copyOfMultiplier = copyOfMultiplier * 2;
  return copyOfMultiplier;
}

var input = 3;
input = multiplyByTwo(input);
Enter fullscreen mode Exit fullscreen mode

How you can see the function never changed the value of multiplier. A copy of the input value is changed instead and returned by the function. Under the function a variable called input is instantiate and will be changed by the return value of the function. So the programmers can decide by their own if a variable value should be overwritten by a function or not. Not the function take the decision.


To be honest this code snippet will never look like this in reality. A regular programmer will be shorten the code massively to make it look like this now:

function multiplyByTwo(multiplier) {
  return multiplier * 2;
}

var input = 3;
input = multiplyByTwo(input);
Enter fullscreen mode Exit fullscreen mode

No copy of the input value is needed at all and the value is never changed before the function will be called in the last line of the snippet.

Conclusion

Clean code can make a huge different in readability of code and to understand the best practices of your chosen programming language or framework can let you avoid issues like changing parameters in function body.
If you have more tips and best practices for beginners of functional programming share them in the comment section.
Keep coding.

Top comments (0)