How do you convert a class to functions in FP?
Tags: functional, discuss, question, advice
Published: 2020-11-04 20:03:21

## Intro

Hey all.

I've done a lot of reading lately on Object Oriented Programming (OOP) and Functional Programming (FP). I have a question to ask and have some ideas to share to get the conversation going.

I know the benefits and typical code for each approach. I do want to move to more FP, to make my code more modular and to be easier to test and debug. I need advice on how.

## Question

The question I have for you, is how do I take code that I have as a class with methods and rewrite it as functions? For current projects and for any new projects where I am tempted to use OOP but choose to use FP.

How do I setup my code with all functions and no classes - just so it looks good technically but is a way that feels natural/smooth and DRY, including when applied to large projects.

In one of my projects, I have a class which takes some variables on initialization and then some methods which use those initial values on the object and sometimes more arguments and then return something.

## My ideas

Here is how I might do calls in OOP to setup an instance and then call a method which adds a given amount to values in the class.

```javascript
foo = 1
bar = 2

myObj = new MyClass(foo, bar)
myObj.add(100)

myObj.foo
// 101
```

### Many function arguments

Should I take the arguments out of the init/constructor method and rather make them the first arguments of each function? An argument I've seen against this is that the code becomes verbose - especially if you want to pass an argument down several calls and then the top-level function has to take everything.

```javascript
foo = 1
bar = 2

add(foo, bar, 100)
// Returns [1, 2]
```

### Pass key-value pair

I had another idea where the first argument of the function in the FP approach is an object like a dictionary / hash / associative array. So instead of passing the same bunch of arguments to each function, you create an object with those values on it an then pass the as a single parameter to the function.


e.g.


```javascript
x = {foo: 1, bar: 2}

add(x, 100)
// Returns {foo: 101, bar: 102}
```

### Use a type

In TypeScript, you can even use an interface so it doesn't matter whether the object coming in is an associative array or a class instance, as long as it has the expected key-value pairs then the compiler will run. I've read about something similar I think for types or structs in other languages Haskell, where you guarantee at compile time that the object you pass in will have the necessary keys.


e.g.

```javascript
interface myType = {
  foo: Integer,
  bar; Integer
}
```

Then the function `add` would only accept argument `x` if it matches the structure of _interface_ `myType`. Or maybe it is a _type_ rather.

## Bad ideas

I thought of some approaches to avoid.

Using global variables magically called in functions.

Or using functions define inside functions (common in JavaScript, maybe because the `class` keyword came to JS very late). I hate this style because you can't use the inner function without the outer one called first, so it is like a class and methods but more chaotic for state and unit tests.


## Conclusion 

Please let me know your thoughts. By the way my background is Python and JavaScript.

Thanks.

## Credits

Cover image by [@hishahadat](https://unsplash.com/photos/gnyA8vd3Otc) on Unsplash.