Successfully Implementing Clean & Functional Javascript

An introduction to how we used clean and functional concepts to help build our new full-stack Javascript codebase at 15gifts

James Fowler
Jun 25, 2018 · 4 min read

For a while now we’ve been working on re-building our legacy back-end codebase at 15gifts from Perl into Node.js, and one of the main factors that we wanted to achieve when undertaking this challenge was code readability and code shareability, allowing our front-end team to be able to understand, modify and work with our back-end code base, and where possible, be able to use code either on the back-end or the front-end (Which we have now successfully achieved!). Here are some of the conventions that we decided upon, along with examples of how we achieved them:

  • Well defined, clearly named files
  • Using variables and functions with meaningful names
  • Immutability, and reducing side-effects
  • Statelessness and pure functions
  • Composition over inheritance

Well defined, clearly named files

Small file sizes
Small file sizes, optimally less than 100 lines, allow developers to quickly see the intention of a file and what it was designed to do at a glance. It also helps with solving diffs in merge conflicts.

Naming files based on what they do
Naming files explicitly by what they do helps developers to be able to see at a glance what their intention is. e.g question-finder.js is better than question-logic.js.

Easy to locate in the file structure
By naming files using a clear convention like this, it should be easy for anyone to find a certain file as they should all follow a similar format.

Only one or two public functions in each file
Inside the file, public functions should always be listed at the top, and there should only ever be one, or in some cases, two. Private functions should be listed underneath prefixed with an underscore.

Using variables and functions with meaningful names

Giving variables meaningful names helps developers to understand why they exist.

Naming functions by what they do, in as much detail as possible, helps developers to understand why functions exist and what they have been built to do.

Immutability, and reducing side-effects

Use const instead of var or let
Using const guarantees that a variable’s binding cannot change after it has been set. Using const in a codebase also implies to developers that a variable’s value cannot change. If all developers working on a codebase are aware of this, then it makes it easier to modify code with confidence. We do sometimes use let in our codebase, when we know that a variable may change. This is very rare though.

Use filter, map and reduce over forEach
Using filter, map and reduce instead of forEach makes it easier to ensure that a variable cannot change once it has been set.

Statelessness and pure functions

No global state
By avoiding the use of global variables and ensuring that our local variables have the correct scope, we can be confident that they are only created and initialised when they are needed. This makes it clear to developers what the intention of a variable is, suggesting that it’s value cannot change.

Where possible, avoid calling out to the database or external services
Pure functions which contain only logic and no calls to retrieve data from a database or external services are much easier to understand than functions which need to do this.

Sharing pure functions with the front-end
If a file only contains pure functions then we know that it can be shared with the front-end and it’s logic imported and used whether it is being run on the server or the client. This greatly helps with code re-use and speeding up development time.

Composition over inheritance

No classes
Not using classes means that functions can just be imported from files and called directly, rather than needing classes to be imported and instantiated. Instead of inheritance, common code can be separated into it’s own file and imported where it is needed.

Using a plugin based architecture means that code can be dynamically injected at runtime, depending on certain parameters. This helps us to share our codebase between different clients and channels with relative ease.


This article showed an overview of some of the conventions we have chosen to use to successfully produce readable code in our full-stack javascript codebase. This has helped us to develop our code more quickly, maintain it more easily, and share a lot of our code between the back-end and the front-end.

15gifts Engineering

15gifts Engineering

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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