functionnal-programming-kieblog

Functional Programming – In God we trust

Today I have task to change layout of one application in my comanpy. These is nothing to talk if this function in Backend site and need to refactor.
Not really happy but I want to apply the Functional Programming to it. After do that, look at code and think a little bit. I think functional programming is very helpful

Function can be write for you. If interesting, just visit and understand “How to write good function?“. Ok, don’t so excited something I’m write below.

Functional Programming and OOP – The WAR beginning

1. I love the pure function

Because pure functions don’t change any states and are entirely dependent on the input, they are simple to understand. Just give pure function arguments, they make a result and don’t change anything from the argument.

One pure function take less than 2 minutes to understand. So it’s really nice point from functional programming

// This is some example about pure function in Javascript
/** This function is pure:
*   it does not depend on any variables outside its scope.
*/
const employee = item => {
  const name = ['Kien', 'Nam', 'Ngoc', 'Vinh'];
  return name.indexOf(item) !== -1;
};
/** This function is also pure: 
*   all variables are passed in as arguments.
*/
const isInStock = (item, array) => {
  return array.indexOf(item) !== -1;
};

Before want to understand functional programming, you need to know “Pure function”. Below is three properties about Pure Function:

  • The same arguments must always lead to the same outcome.
  • A pure function cannot depend on any variable declared outside its scope.
  • There must be no side effects (which mean no changes to external varables).
functional-programming-function-should-do-only-one-thing
Scala Pure Function. Nguồn/ Source: oreilly.com

2. Easy for debug

Because each function doesn’t do too much thing. Inside it not contain complex business logic. So if we got a problem, just try to find what function makes this bug?.

Let me show you a example. If we write a function with more than 500 lines, when we debug, need to add to watch each variable. Why?, because we don’t know when it’s changed value, so keep focus in any variable.

Otherwise, what’s happen if debug in function?. Because the scope of variable is belong to function. Outside of function, they are nothing -> just focus to look value when debug in function. When the code in function is finish, no need to care.

3. Call function everywhere

Before do the function programming, please consider to sperate the business to a small problem. The function do one thing!.

Example: If I want to disable 4 icon -> let make a function disable. If I want to add a new item for groups -> let create a function with the argument is item and groups to add.
When the business is big and has many common problems.

functional-programming-function-should-do-only-one-thing
Please remember and follow the rule

Now it’s time for Functional Programming. Just add the function and call it each time we need. Amazing, right?. Write one time and call many times.

4. Function argument function

When seperate function as small as we can do, the return from function maybe become a argument in another function.

Some people think it’s very complex and difficult to handle incase we have bug. But when apply it in my code, it not like we think. Just set a debug in a smallest function and find where problem come.

functional programming
Oh yeah, please fell free to play with argument in function

Futhermore, if function is argument of another function. The function total line code is not too bug. Easy to understand and handle, it’s another best point from Functional Programming.

// This is not pure function.
// But I think sometime we should try to make it pure
// If not possible, just make it simple and easy to understand
public static int sum(int a, int b) {
    return new Random().nextInt() + a + b;
}

5. Reference more about functional programming

End. Thank you so much, please like the facebook page

Có gì thắc mắc cứ comment đây nha! - Please feel free to comment here!
Chia sẻ bài viết