Adaptoring:通过适配器生成为库提供替代 API
Adaptoring: Adapter Generation to Provide an Alternative API for a Library
AI总结:
本文提出 adaptoring 方法,利用适配器模式自动生成调用原始库的新 API 库,避免直接修改或分叉原库,并支持自动推断、手动指定转换及适配器迁移,在 Python 上验证了对大型库的有效性。
摘要:
第三方库是快速应用开发的基石。为了实现高效使用,库必须提供设计良好的 API。晦涩难懂的 API 反而会拖慢学习过程,并可能导致错误使用。改进库 API 的通常做法是直接编辑其代码,要么保留旧 API 但将其标记为弃用(暂时增加 API 规模),要么直接移除旧 API(引入破坏性变更)。如果维护者不愿意做出此类更改,其他人就需要创建一个硬分叉(hard fork),并对其进行重构。但这样一来,就很难合并原始库的后续变更,例如错误修复或性能改进。在本文中,我们转而探索使用适配器模式(adapter pattern),以一个新库的形式提供新 API,该新库在内部调用原始库。这使得新库能够以零额外成本利用原始库的所有实现变更。我们将这种方法称为 adaptoring。为了使该方法切实可行,我们识别了可以自动生成适配器代码的 API 转换,并研究了哪些转换可以基于原始库的文档和使用模式自动推断。对于无法自动推断的情况,我们提供了一个工具,让开发人员可以手动指定 API 转换。最后,我们考虑了当原始库引入破坏性变更时迁移所生成适配器的问题。我们在 Python 上实现了我们的方法,证明了即使对于大型库,它也能快速有效地提供替代 API。
英文摘要:
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.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。