Skip to content

Instantly share code, notes, and snippets.

@branneman branneman/coupling.js
Last active Aug 16, 2019

Embed
What would you like to do?
JavaScript examples: No coupling, Loose coupling, Tight coupling
//
// Tight coupling from A to B => A needs B to be defined, callable and adhere to an interface
// No coupling from B to A => B does not need A in any way (pure fn)
//
function a() { b() }
function b() {}
a()
//
// No coupling from A to B => A and B work independently,
// although there a chance of naming collision in `store`
// Tight coupling from A to store => Direct reference to `store`, mutates original
// Tight coupling from B to store => Direct reference to `store`, mutates original
//
function a() { store.foo = 'x' }
function b() { store.bar = 'y' }
const store = {}
a()
b()
//
// Loose coupling between A and B => A and B both operate on the numeric `counter` property,
// although they will not error when it does not exist
// Tight coupling from A to store => Direct reference to `store`, mutates original
// Tight coupling from B to store => Direct reference to `store`, mutates original
//
function a() { store.counter = (store.counter || 0) + 1 }
function b() { store.counter = (store.counter || 0) + 10 }
const store = {}
a()
b()
//
// No coupling between A and B => A and B work independently,
// although there a chance of naming collision in `store`
// Loose coupling from A to store => Because DI and immutability (pure fn)
// Loose coupling from B to store => Because DI and immutability (pure fn)
//
function a(store) { return Object.assign({}, store, { foo: 'x' }) }
function b(store) { return Object.assign({}, store, { bar: 'y' }) }
const store0 = {}
const store1 = a(store0)
const store2 = b(store1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.