Functional composition is an extremely useful tool when coding in JavaScript. It has a bit of a learning curve but can remove lots of lines of duplicate code while preserving a declarative flow. However, it doesn’t always play nicely with voids, i.e. functions with no explicit returns.

Say we have a function that parses some user data:

const users = [
{ first: 'Syl', age: 21 },
{ first: 'Bill', age: 47 },
{ first: 'Hank', age: 55 },
const getTargetUsers = pipe(
filter(({ age }) => age > 30),
getTargetUsers(users); // ['Bill', 'Hank']);

Easy enough! But what if we want to add some analytics? Specifically, say we want to record all data from users in our target demographic before parsing further. Imagine we have a third party analytics method that does just this. The bad news? It’s a void and doesn’t return any of the user data. It just fires a post request to the server and dies. We could wrap the…

I have yet to work anywhere where there was consensus on coding style. The fact that formatters like Prettier exist also tell you this isn’t a unique problem. I’d like to over one such convention that I think could use a revisit: nested ternaries.

A ternary is basically a shorthand if-else statement. Consider the following

if (person.age < 18) {
person.type = 'minor';
} else {
person.type = 'adult'

There’s a bit of repetition and the if-else block can be a bit verbose. As a ternary we could write

person.type = person.age < 18
? 'minor'
: 'adult';

With a ternary the if resolution comes after question mark whereas the fallback resolution (else) comes after the colon. …

Functional programming with the help of utility libraries like Ramda or Lodash can make composing certain functions a breeze. A trivial example in JavaScript:

One of the knocks against functional programming — and JavaScript in general — is that despite its declarative centering, composing functions in this manner removes any input-output signature from the method and relies on thorough documentation, semantically relevant naming conventions, and devs who invest time into both.

Enter TypeScript: the self-documenting type guard and validator JavaScript pre-compiler. …

Aaron Dilley

Front End Developer and Crossword Enthusiast

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