Avoid adding a new library to the project

Suppose you need to implement an X functionality in your project. Theorists of software development will say that you have to take the already existing library Y, and use it to implement the things you need. In fact, it is a classic approach in software development – reusing your own or others’ previously created libraries (third-party libraries). And most programmers use this way.

However, those theorists in various articles and books, forget to mention what hell it will become to support several dozen third-party libraries in about 10 years.

скачанные файлы (9)

We strongly recommend avoiding adding a new library to a project. Please don’t get it wrong. We are not saying that you shouldn’t use libraries at all, and write everything yourself. This would be insufficient, of course. But sometimes a new library is added to the project at the whim of some developer, intending to add a little cool small “feature” to the project. It’s not hard to add a new library to the project, but then the whole team will have to carry the load of its support for many years.

Tracking the evolution of several large projects, we have seen quite a lot of problems caused by a large number of third-party libraries. We will probably enumerate only some of the issues, but this list should already provoke some thoughts:

  1. Adding new libraries promptly increases the project size. In our era of fast Internet and large SSD drives, this is not a big problem, of course. But, it’s rather unpleasant when the download time from the version control system turns into 10 minutes instead of 1.
  2. Even if you use just 1% of the library capabilities, it is usually included in the project as a whole. As a result, if the libraries are used in the form of compiled modules (for example, DLL), the distribution size grows very fast. If you use the library as source code, then the compile time significantly increases.
  3. Infrastructure connected with the compilation of the project becomes more complicated. Some libraries require additional components. A simple example: we need Python for building. As a result, in some time you’ll need to have a lot of additional programs to build a project. So the probability that something will fail increases. It’s hard to explain, you need to experience it. In big projects something fails all the time, and you have to put a lot of effort into making everything work and compile.
  4. If you care about vulnerabilities, you must regularly update third-party libraries. It would be of interest to violators, to study the code libraries to search for vulnerabilities. Firstly, many libraries are open-source, and secondly, having found a weak point in one of the libraries, you can get a master exploit to many applications where the library is used.
  5. One the libraries may suddenly change the license type. Firstly, you have to keep that in mind, and track the changes. Secondly, it’s unclear what to do if that happens. For example, once, a very widely used library softfloat moved to BSD from a personal agreement.
  6. You will have troubles upgrading to a new version of the compiler. There will definitely be a few libraries that won’t be ready to adapt for a new compiler, you’ll have to wait, or make your own corrections in the library.
  7. You will have problems when moving to a different compiler. For example, you are using Visual C++, and want to use Intel C++. There will surely be a couple of libraries where something is wrong.
  8. You will have problems moving to a different platform. Not necessarily even a totally different platform. Let’s say, you’ll decide to port a Win32 application to Win64. You will have the same problems. Most likely, several libraries won’t be ready for this, and you’ll wonder what to do with them. It is especially unpleasant when the library is lying dormant somewhere, and is no longer developing.
  9. Sooner or later, if you use lots of C libraries, where the types aren’t stored in namespace, you’ll start having name clashes. This causes compilation errors, or hidden errors. For example, a wrong enum constant can be used instead of the one you’ve intended to use.
  10. If your project uses a lot of libraries, adding another one won’t seem harmful. We can draw an analogy with the broken windows theory. But consequently, the growth of the project turns into uncontrolled chaos.
  11. And there could be a lot of other downsides in adding new libraries, which I’m probably not aware of. But in any case, additional libraries increase the complexity of project support. Some issues can occur in a fragment where they were least expected to.

Again, we should emphasize; we don’t say that we should stop using third-party libraries at all. If we have to work with images in PNG format in the program, we’ll take the LibPNG library, and not reinvent the wheel.

But even working with PNG we need to stop and think. Do we really need a library? What do we want to do with the images? If the task is just to save an image in *.png file, you can get by with system functions. For example, if you have a Windows application, you could use WIC. And if you’re already using an MFC library, there is no need to make the code more sophisticated, because there’s a CImage class (see the discussion on StackOverflow). Minus one library – great!

Let us give you an example from our own practice. In the process of developing the PVS-Studio analyzer, we needed to use simple regular expressions in a couple of diagnostics. In general, we are convinced that static analysis isn’t the right place for regular expressions. But sometimes you just need to find something in a string with the help of a regular expression.

It was possible to add existing libraries, but it was clear that all of them would be redundant. At the same time we still needed regular expressions, and we had to come up with something.

Absolutely coincidentally, exactly at that moment one developer was reading a book “Beautiful Code” (ISBN 9780596510046). This book is about simple and elegant solutions. And there he came across an extremely simple implementation of regular expressions. Just a few dozen strings. And that’s it!

We decided to use that implementation in PVS-Studio. And you know what? The abilities of this implementation are still enough for us; complex regular expressions are just not necessary for us.

Conclusion: Instead of adding a new library, we spent half an hour writing a needed functionality. We suppressed the desire to use one more library. And it turned out to be a great decision; the time showed that we really didn’t need that library. And we are not talking about several months, we have happily used it for more than five years.

This case really convinced us that the simpler solution, the better. By avoiding adding new libraries (if possible), you make your project simpler.

Readers may be interested to know what the code for searching regular expressions was. We’ll type it here from the book. See how graceful it is. This code was slightly changed when integrating to PVS-Studio, but its main idea remains unchanged. So, the code from the book:

 // regular expression format
// c Matches any "c" letter
//.(dot) Matches any (singular) symbol 
//^ Matches the beginning of the input string
//$ Matches the end of the input string
# Match the appearance of the preceding character zero or
// several times

int matchhere(char *regexp, char *text);
int matchstar(int c, char *regexp, char *text);

// match: search for regular expression anywhere in text
int match(char *regexp, char *text)
  if (regexp[0] == '^')
    return matchhere(regexp+1, text);
  do { /* must look even if string is empty */
   if (matchhere(regexp, text))
     return 1;
  } while (*text++ != '\0');
  return 0;

// matchhere: search for regexp at beginning of text
int matchhere(char *regexp, char *text)
   if (regexp[0] == '\0')
     return 1;
   if (regexp[1] == '*')
     return matchstar(regexp[0], regexp+2, text);

   if (regexp[0] == '$' && regexp[1] == '\0')
     return *text == '\0';
   if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
     return matchhere(regexp+1, text+1);
   return 0;

// matchstar: search for c*regexp at beginning of text
int matchstar(int c, char *regexp, char *text)
  do {   /* * a * matches zero or more instances */
            more instances */
    if (matchhere(regexp, text))
      return 1;
  } while (*text != '\0' && (*text++ == c || c == '.'));
  return 0;

Yes, this version is extremely simple, but for several years there was need to use more complex solutions. It really has got limited functionality, but there was no need to add anything more complicated, and we don’t think there will be. This is a good example of where a simple solution turned out to be better than a complex one.


Don’t hurry to add new libraries to the project; add one only when there is no other way to manage without a library.

Here are the possible workarounds:

  1. Have a look if the API of your system, or one of the already used libraries has a required functionality. It’s a good idea to investigate this question.
  2. If you plan to use a small piece of functionality from the library, then it makes sense to implement it yourself. The argument to add a library “just in case” is no good. Almost certainly, this library won’t be used much in the future. Programmers sometimes want to have universality that is actually not needed.
  3. If there are several libraries to resolve your task, choose the simplest one, which meets your needs. As I have stated before, get rid of the idea “it’s a cool library – let’s take it just in case”
  4. Before adding a new library, sit back and think. Maybe even take a break, get some coffee, discuss it with your colleagues. Perhaps you’ll realise that you can solve the problem in a completely different way, without using third-party libraries.

Written by Andrey Karpov.
This error was found with PVS-Studio static analysis tool.

2 thoughts on “Avoid adding a new library to the project

  1. Thank you for the post! I have learned a lot about the tit-bits of why not to use a new lib to project.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.