Incomplete Library Class

When a library class doesn't fully meet requirements and is difficult to change it directly.

Sign of Smell

Nowadays, developers rarely write code from scratch. In the software industry, there is a saying that encourages us not to reinvent the wheel repeatedly. There are numerous useful open-source frameworks or third-party libraries available that can help us code more efficiently on a daily basis. However, we eventually discovered that libraries don't always perfectly meet our needs. Furthermore, the library author has refused to implement them or has simply ignored the pull request for a long time. In this case, we face the code smell called “Incomplete Library Class”.

This is a special code smell because it does not belong to any of the code smell catalogs. It is always listed under "other code smells.”

Reason of Smell

Here are some reasons why an incomplete library class can be considered a code smell:

  • Poor Documentation: Incomplete library classes often lack proper documentation, making it difficult for developers to understand how to use them correctly. This can lead to errors, confusion, and wasted time.

  • Limited Functionality: If a library class doesn't provide the expected or necessary functionality to perform a task, developers may have to work around it or write additional code, leading to increased complexity and reduced productivity.

  • Unpredictable Behavior: Incomplete library classes may exhibit unpredictable behavior or fail to handle edge cases properly, causing unexpected bugs or crashes in applications that depend on them.

  • Increased Technical Debt: Developers may need to create workarounds or patches to make up for the missing functionality in an incomplete library class. This adds technical debt to the codebase, making it harder to maintain and extend the software in the future.

  • Risk of Abandonment: If a library class remains incomplete for an extended period, there's a risk that it may be abandoned by its maintainers, leaving users with unresolved issues and no updates.

  • Maintenance Issues: Incomplete library classes are often indicative of poor maintenance practices. A library or framework should evolve over time to address user needs, fix bugs, and improve performance. If it doesn't, it can lead to problems as the software ecosystem around it changes.

Refactoring Recipes

Unfortunately, there are situations where it is not possible to directly modify the library. In such cases, it is necessary to address the issue by creating an additional layer or introducing a foreign method.

  • Introduce Foreign Method

  • Introduce Local Extension

Introduce Foreign Method

When you have code that uses the data and methods of a certain class but can't add or modify the class itself, such as when it is located in a read-only third-party library, you can consider adding the needed method to a client class. This involves passing an object of the library class as an argument to the client class.

Introduce Local Extension

When you encounter a situation where a library class lacks the necessary methods that you require, and you find yourself unable to add those methods to the class due to certain constraints or limitations, here is a solution. You can create a new class that extends the existing library class, thus incorporating the additional methods you need.

This approach allows you to either inherit from the utility class or wrap around it, providing you with the flexibility to seamlessly enhance its functionality according to your specific requirements locally.

Reference

https://refactoring.guru/smells/incomplete-library-class

https://luzkan.github.io/smells/incomplete-library-class

https://refactoring.guru/introduce-foreign-method

https://refactoring.guru/introduce-local-extension

Last updated