Main | All posts | Code

Code Complete: Using a Library or Writing Your Own Code

Time Reading ~4 minutes
Code Complete: Using a Library or Writing Your Own Code main picture

Is it worth using libraries for a few simple functions? Wouldn't writing them yourself be easier? These are questions regularly asked by novice developers and by almost anyone working on Hexlet projects. Let's take a closer look.

I have a related question about Lodash. I connected Lodash to the project and used only one function. How sensible is including a library for the sake of 1 function? I also solved the problem by hand with pure js. What problem does that solve?

First of all, these questions don't have one-size-fits-all answers. For each case, you need to consider many factors, ranging from the nature of the code in question to the runtime, i.e., where and on what the program operates. We'll talk more about these factors below.

Code size

In the backend, the size of the source code is almost irrelevant. A megabyte, or even ten, too many or too few means nothing. Modern software contains a huge number of resources which are orders of magnitude more important than any library. Even if it were a problem, a library that provides simple functions cannot, by definition, take up much space.

In the frontend, the situation is a little more complicated, but right now it all comes down to how the library is organized. If it has properly organized exports, the resulting bundle (assembled by, for example, a webpack) will only include functions that are actually used. There will only be a few.

Dependencies

Some developers, for whatever reason, try to avoid using dependencies (third-party libraries) in their code. Here you cannot do with small functions, and such projects often have quite a few of their own solutions for pre-prepared tools.

There isn't much to say here. Dependencies are fine as long as they allow developers to save time and focus more energy on application logic than infrastructure tasks.

What's the benefit?

Not all libraries are equally useful. There are a few basic themes that are usually poorly covered in languages. These include working with collections, strings, and dates. In addition to these, different languages have various, more specific areas that require expansion. It is in these situations that libraries are worthwhile, even if just for the sake of a single function. Here's why.

As a rule, where one function is needed, another will soon be needed. The function goes unnoticed, and other people write one as well. In the end, someone wrote a function, then someone else wrote another somewhere else, and they don't know about each other's functions. These situations quickly turn from "it's too early" to "it's too late" when there are a bunch of functions already existing in the relevant libraries throughout the code. Then, when programmers leave for other projects, the cycle repeats. As a result, there are a lot of standard functions scattered around different projects written by different people, contain bugs, have no documentation, and were most likely tested poorly, if at all.

Your own code will never be as thoroughly tested and documented as popular libraries. Try opening lodash and see the amount of documentation. Here's another advantage of libraries like this. They're so common and standardized that most developers know how to work with them. This means that the project code will become clearer and simpler for everyone working on it.

import _ from 'lodash';
// Quite a popular function that is often needed in projects
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

There is another interesting, less obvious advantage to using such libraries. They contain a large number of functions, which can be very difficult to figure out on your own. Using these libraries also raises the level of the developer themselves, and they learn how to solve most standard problems using minimal code with the right abstractions. Of course, this requires these features to be reviewed periodically, but it's worth it. In Hexlet projects, our students regularly reinvent the wheel just because they didn't know about the standard solutions. When they say: "I only need one function", it actually turns out that there is more than one, it's just that the amateur developer is not aware of this due to lack of experience.

// Useful examples:

_.intersection([2, 1], [2, 3]);
// => [2]

_.union([2], [1, 2]);
// => [2, 1]

_.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]

const object = { 'a': { 'b': 2 } };

_.has(object, ['a', 'b']);
// => true

_.get(object, 'a.b.c', 'default');
// => 'default'

Each language has its own set of libraries, which are seen as crucial for almost any non-trivial project. The main directions:

  • Dates
  • Lines
  • Collections

For other, rarer libraries, the situation can be more complicated, and it's not always obvious whether using a library is worthwhile. The main thing when making these decisions is to be guided by pragmatism and not be categorical.

Additional materials

User avatar Kirill Mokevnin
Kirill Mokevnin 12 August 2022
1
Related posts
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time
profession
Layout with the latest CSS standards
5 months
from scratch
under development
Start at any time
profession
new
Developing web applications with Django
10 months
from scratch
under development
Start at any time