Sometimes You Don't Need A Dependency

When developing new features it is all too easy to go immediately to packagist.org and see if that wheel has already been invented. Most of the time this is the right approach. We don't need new wheels.

The trouble starts when you need want to add a small, self contained feature. In my case, this was estimating the reading time of a piece of content. Searching Packagist for "read time" returns many results with well over a 100,000 downloads between them.

Any one of these packages would probably have done the job but they all come with a hidden cost. The cost is hidden because you don't have to pay it until you need to do somekind of upgrade (PHP, frameworks etc) and can't because the package hasn't been maintained and is now incompatible.

In my case all I needed was this:

function readingTime(string $content, int $wordsPerMinute = 234): CarbonInterval
{
    $wordCount = str_word_count(strip_tags($content));
       
    return \Carbon\CarbonInterval::minutes(ceil($wordCount / $wordsPerMinute));
}

echo readingTime("some content")->forHumans();

This relies only on the Carbon library which has huge momentum and isn't being abandoned anytime soon.

Ask Yourself: Is It Worth It?

Don't get me wrong: I'm not advocating against using dependencies in general. We need to be aware they aren't free. When we reach for a dependency we need to ask ourselves - is it worth it? Is this package going to save me more time than it will eventually cost me? For all the SDKs and big libraries out there the answer is almost certainly yes but for smaller packages maybe not.

There are other benefits of being selective about which packages you install including supply chain attacks and the 2016 left-pad incident.

Popular Reads

Subscribe

Keep up to date

Please provide your email address
Please provide your name
Please provide your name
No thanks