首页 > 解决方案 > How to refactor Clojure source code, especially to rename the keywords of a map collection?

问题描述

When I develop Clojure project, sometimes, I encounter a situation: It is somewhat difficult to rename the keywords of a map collection.

For example, if I have a map collection that is shared by several namespaces. Then, those namespaces will depend on the keywords of this map collection because the functions defined in these namespaces use map destruction syntaxes. When I rename the keywords of this map collection, I will definitely need to exhaustively check those namespaces.

Is there any tools or effective methods to use in this kind of situation?

标签: dictionaryclojurerefactoringrenamekeyword

解决方案


当您使用不合格:x的四十个不同含义然后决定重命名其中一个时,这将是一个挑战。如果您没有(并且不想制作)足以检测预期参数丢失的测试用例,那么挑战将变得令人头疼。

下一次,考虑为将广泛传播的用途指定(唯一的)命名空间限定关键字。替换是明确的,工具更有可能将其自动化。

一些语言功能可以在您修改代码时提供帮助,具体取决于您决定重命名内容的范围。

先决条件为您提供了一个断言解构参数的地方(或者仅记录它,如果您需要逐个判断成功)。

纯函数允许您克隆一堆代码,重命名克隆版本中的内容(或修改任何逻辑),然后并行运行两个版本并断言它们得出相同的结果。例如,克隆函数ff-1and f-2、 reworkf-2和 reprogramf所以它同时调用f-1andf-2和断言结果是相同的。当他们同意时,您可以删除原件。

使用内置的测试clojure.test非常容易(并且有更复杂的替代方案)。


推荐阅读