The /lib folder

TL,DR; The /lib folder is a good halfway point to sane application dependency control.

Posted by Jake Corn on February 19, 2020

TL,DR; The /lib folder is a good halfway point to sane application dependency control.

Why hand roll something when you can just download an open source dependency? Great Idea, if someone else has already done the work let's just use theirs.

So we start using their library throughout our application.

const someLib = require('someLib')

app.post('/route', (req, res) => {
  // ...
  const val = someLib.getValue()
  // ...
})

let's say we make 1000 calls to getValue() throughout our application.

Then imagine that we discover that getValue() has always been broken. work to fix is now. amountOfWorkToFixOnce X numCalls

at least I propose a halfway point to hand rolling and just calling libs throughout codebase. the /lib folder

const someLib = require('/lib/someLib')

app.post('/route', (req, res) => {
  // ...
  const val = someLib.getValue()
  // ...
})
module.exports = require('someLib')

WHY ?? do this????

control.

Breaking changes to libraries happen. You may want a piece of functionality that only an upgrade contains, that upgrade may also have a regression.

dependency optimization. Imagine you were relying on a string formatting library when you were building your prototype. Then later down the line your team finds that you would like to break that dependency. It is now much easier as you have a single point of control.

Extension. Imagine a piece of functionality you wish someLib had. you can now include it.

Learning testing. Imagine you want to ensure that your dependency works the way you think it does... you now just do something like....

/lib/someLib/index.js /lib/someLib/index.test.js

(learning tests allow you to constantly and automatically upgrade dependencies too, fwiw) Is that premature optimization?

Right? We don't KNOW quite yet if we will have a dependency issue with the library, why wrap it from the start? I can understand thinking that, but it's not quite what we're doing here. It would be optimizing if we were tweaking any library behaviors from the start, but all we're doing is establishing a controllable access point from the application to the libraries. Which is not optimization, it's a structural concern.

How do you maintain that effort? It seems only by convention...

lint rules. 'if not /lib folder and doesn't match require('/lib/*/')'

Many 3rd party libs also contain WAY more code than you actually need. You may want to optimize or retire that after a while.

The /lib folder is a good halfway point to sane application dependency control from prototype to mature production application.

-- jake