Categories
JavaScript

Node.js – The SubStack Pattern ( CommonJS )

Today I would like to have a short discussion about something that I learned about while working on a project; the SubStack Pattern.

…What?

The substack pattern is simply a way for you to export a specific functionality from a module, particularly a function.

Exhibit A

// File name is greetem.js
module.exports = (message) => {
  console.log(`greet1: ${message}`)
}

Why is this a good thing?

Well, think about it. When building an application, the best way to construct a module is to have that module be in charge of providing a specific functionality, you don’t wanna give a module “multiple-jobs”; as things can get ugly pretty quickly, making your code really difficult to understand… and we all know that we’re not supposed to be writing code for ourselves, but for other developers….and for those of you who never knew this…. in the words of the late Biggie Smalls, ” If yah don’t know, now yah know“.

“But RedEye, how exactly do we use this pattern, i’m use to using something like this”:

// File name is greetem.js

function greet1(message) {
	console.log(`greet1: ${message}`)
}
                
module.exports = {
  greet1: greet1
}

I’m glad you asked, it’s quite simple actually, check this out:, but first I’ll copy and paste the original code so you don’t have to scroll back up to compare, I know how annoying that can be.

Original code from above.

// File name is greetem.js
module.exports = (message) => {
  console.log(`greet1: ${message}`)
}

Here is how we use the code from above.

const greetings = require('./greetem')

greetings('Hello Universe (not world) from RedEyeCoding!!!')

Now if you have an application that you’re building and it contains a bunch of features…depending on what you’re doing, you’re much better off dedicating a module to THAT specific feature/ functionality.

So to break it down even more. Lets say I had an app where all it did was console.log out different greetings ( functionalities ):

  1. “Hello World”
  2. “Hello Universe”
  3. “Hello friend”
  4. “Hello foe”

I would then dedicate each module to it’s own greeting.

I will first create four different files ( modules ) like so:

// foe.js
module.exports.foe = (message) => {
  console.log(`foe: ${message}`)
}

// friend.js
module.exports.friend = (message) => {
  console.log(`friend: ${message}`)
}

// world.js
module.exports.world = (message) => {
  console.log(`world: ${message}`)
}

// universe.js
module.exports.universe = (message) => {
  console.log(`universe: ${message}`)
}

Then I will import them like so:

Remember that while importing them, you can name them whatever you want. You can see this below; as i prep-ended “RedEye” to each const variable.

const redEyeWorld = require('./world')
const redEyeFoe = require('./foe')
const redEyeFriend = require('./friend')
const redEyeUniverse = require('./universe')

redEyeWorld('Hello World')
redEyeFoe('Hello foe')
redEyeFriend('Hello friend')
redEyeUniverse('Hello universe')

So whats happening here?

Well, we’re simply requiring the module, giving it a name that best describes it’s purpose, and invoking it like any other function; that simple. My deepest apologies if you were in the mood for some intense reading; expecting a long explanation, lol.

Hope this helps.