Third-party libraries are a cornerstone of fast application development. To enable efficient use, libraries must provide a well-designed API. An obscure API instead slows down the learning process and can lead to erroneous use. The usual approach to improve the API of a library is to edit its code directly, either keeping the old API but deprecating it (temporarily increasing the API size) or dropping it (introducing breaking changes). If maintainers are unwilling to make such changes, others need to create a hard fork, which they can refactor. But then it is difficult to incorporate changes to the original library, such as bug fixes or performance improvements. In this paper, we instead explore the use of the adapter pattern to provide a new API as a new library that calls the original library internally. This allows the new library to leverage all implementation changes to the original library, at no additional cost. We call this approach adaptoring. To make the approach practical, we identify API transformations for which adapter code can be generated automatically, and investigate which transformations can be inferred automatically, based on the documentation and usage patterns of the original library. For cases where automated inference is not possible, we present a tool that lets developers manually specify API transformations. Finally, we consider the issue of migrating the generated adapters if the original library introduces breaking changes. We implemented our approach for Python, demonstrating its effectiveness to quickly provide an alternative API even for large libraries.
翻译:第三方库是快速应用开发的基石。为确保高效使用,库必须提供设计良好的API。晦涩的API反而会拖慢学习进程,并可能导致错误使用。改进库API的常规方法是直接编辑其代码,要么保留旧API但标记为弃用(暂时增加API规模),要么直接移除它(引入破坏性变更)。如果维护者不愿进行此类修改,其他人则需要创建硬分叉,并对分叉进行重构。但这样做很难整合原始库的变更(如错误修复或性能改进)。本文转而探索使用适配器模式,将新API作为新库提供,该新库在内部调用原始库。这使得新库能够以零额外成本利用原始库的所有实现变更。我们将此方法称为适配器生成。为使该方法切实可行,我们识别了可自动生成适配器代码的API转换类型,并研究了基于原始库的文档和使用模式可自动推断的转换类型。对于无法自动推断的情况,我们提供了一种工具,允许开发者手动指定API转换。最后,我们考虑了当原始库引入破坏性变更时迁移所生成适配器的问题。我们针对Python实现了该方法,证明了即使在大型库中也能快速提供替代API的有效性。