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 salt = “Adfe#12HJH”;

const trim = str => trim(str);

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

const result = encrypt(trim(“password”));

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

const address = {

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 :)

--

--

--

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.

Recommended from Medium

A Minimalistic Guide to Building and Deploying Monolithic Spring Boot React Applications

Restarting The Game

The Frontenders Kaleidoscope ~ Ed.7 ~ 2018

Mastering the Camera Roll in React Native

Understanding React useEffect Hook in detail

Tending the p5.garden 🌸

Qianqian, a non-binary Chinese person with black short hair, wearing a black tank top, standing next to an apple tree, with a lush garden in the background.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Waruni W.

Waruni W.

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

More from Medium

Reactive JQuery using Vuex design pattern

Javascript and Asynchronous Programming

asynchronous programming

< Lets Git to it >_

Rest Services & Koa.js and React.js