Eliminating a tiny OSS dependency
Hi!👋 This is a community-driven, beta document, NOT an official GitHub document. Your contribution to make this even better is super welcome!🚀 Please dive in and contribute🙌

Eliminating a tiny OSS dependency #

This may be of limited applicability. As more cases are discovered, this maturity level will increase.

Description #

Do you know about the left-pad issue? In 2016, the left-pad library was suspended from npm, causing well-known libraries that depended on it to cease working. left-pad is a simple JavaScript library that only fills the left side of a string with a specified number of characters, or spaces if not specified. Excluding blank lines, it’s a simple code with only about 10 lines.

There are many ideas to avoid reinventing the wheel, On the other hand, we must also pay attention to external code that can have a significant impact. If the provided code’s scope is very limited, it might be better to contain it internally rather than depending on an external source.

Samples #

Implementing a left-pad function can be done as shown below:

def leftpad(string, length, char = ' ')
    string.rjust(length, String(char))

Exercise #

  • Exercise 1: Implement the left-pad function that takes the string, length, and character to pad with. The default character should be a space.
  • Exercise 2: Consider a scenario where multiple small external dependencies exist in a project. Identify a small utility function that can replace one such dependency and implement it.

Checklist for Further Learning #

  • Have you considered the trade-offs between using an external dependency and implementing the code yourself?
  • How might this pattern of eliminating tiny dependencies affect the maintainability of the code?
  • What principles or guidelines can be used to decide when to replace a tiny external dependency with an internal implementation?