# Functional composition & Object composition

If someone asks you What does Software Development means? how would you go to answer that question? Different people may define it in different ways. The best definition I came across so far or at least the best definition that I like the most is from “Composing Software” By Eric Elliot. In his book, he mentioned Software Development is “the act of breaking a complex problem down into smaller problems and composing simple solutions to form a complete solution to the complex problem.”. That whole sentence tells software developers what they really have to focus on. In this article, I’m not planning to talk about what software development is. Rather planning to share my learnings about functional and object composition.

Before even begin to discuss functional & object composition, let’s see the meaning of “Composition”. If you search on google, it will say something like “ the combination of parts or elements that make up something”. If that’s the case what does functional and object composition really means?

# Functional Composition

Function composition is, apply a function to an output of another function. We can simply represent that as f(g(x)). How many times we have used functional composition without realizing that we are composing a function.

Eg: Let’s say we want to hash a given string. Before hash, we need to remove trailing spaces.

const trim = str => trim(str);

const encrypt= str => md5(str + salt);

Every time we write a promise chain, we are composing functions. Popular functional programming libraries like Lodash includes utilities to make function composition easier and code look more readable.

Eg: pipe and compose methods in Lodash.

You might be wondering now what is object composition then?

# Object composition

Anytime you create any non-primitive data structure, you are composing an object. What does this really mean? Let’s look at an example here. There are mainly two data types. Primitive data types and composite or compound data types.

These are primitive data types.

const street = “78 Ranburge St”;

const suburb = “Camberwell”;

const postcode = “3124”;

This is a composite data type

street, suburb, postcode

};

# Conclusion

We were using functional & Object compositions even without realizing we were compositing. If we really know what we are doing, we can make it better. Otherwise, we’ll make a mess. Clean code, fewer bugs, happy developers :)

--

--

--

## More from Waruni W.

A simple person admires simple things in the world.Continuously find ways to learn and grow.

Love podcasts or audiobooks? Learn on the go with our new app.

## Waruni W.

A simple person admires simple things in the world.Continuously find ways to learn and grow.