GitHub Copilot - Patterns & Exercises
GitHub 🌟
en 🇬🇧
en 🇬🇧
  • Introduction
  • Contributing to the Project
  • General
    • Code completion
    • Comment to code
    • Code to comment
    • Quick Q&A
    • Regular expression
    • Language translation
    • Type hinting
    • Code to document
    • Object generation from structured data
    • Showing examples
  • Client Side Tips
    • Copilot snnipet handling
    • GitHub Copilot Shortcuts
    • Go to definition
    • Pin the files you need
  • Design Patterns
    • AI readable naming convention
    • Consistent coding style
    • High-level architecture first
    • Working on small chunks
    • Context-less Architecture
    • Eliminating a tiny OSS dependency
  • Collaboration
    • AI friendly documentation
    • Coaching on prompts
  • Test
    • Creating unit tests
    • Specify how to generate test code
    • Writing failure case first
    • Writing test cases in natural language first
    • Test only what is necessary
  • Refactoring
    • Writing test code before refactoring
    • Making the calculation part independent
    • Asking with open-ended questions
  • Archived
    • GitHub Copilot Patterns & Exercises Guide
    • Translations
      • German 🇩🇪
      • Spanish 🇪🇸
      • French 🇫🇷
      • Italy 🇮🇹
      • Japanese 🇯🇵
      • Portuguese 🇵🇹
      • Chinese 🇨🇳
Powered by GitBook
On this page
  • Description
  • Samples
  • Exercise
  • Checklist for Further Learning
Edit on GitHub
  1. Design Patterns

Eliminating a tiny OSS dependency

Eliminating a tiny OSS dependency that can be actually implemented in a few lines of code

Last updated 1 year ago

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))
end

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?