English 中文(简体)
函数别名坏吗?
原标题:Are function aliases bad?

您有没有写过函数 Foo, 在您的程序中使用它, 然后最终决定重构您的代码, 重写另一个函数栏, 比 Foo 更好, 并决定“ 哦, 从现在开始, 我们用酒吧而不是 Foo ” 。 但是, 您没有删除 Foo 并替换代码中的 Foo, 而是决定将 Foo 宣布为函数栏的别名?

你们中有些人可能会说:为什么你们不只改写Foo 函数,并保留它的名字 Foo, 所以不用用 Bar 取代Foo 的每一个情况? 嗯,因为这个重新构思, Foo 不再是一个合适的名称, 因为它没有反映函数的作用, 所以我决定不保留它的名字。

这是某种逆向兼容性吗?

假设

  1. Foo was written by one of you coworkers
  2. said coworkers are used to Foo
  3. you re the moron in the story who thinks is smarter than the others and wrote Bar
问题回答
Foo was written by one of you coworkers
said coworkers are used to Foo
you re the moron in the story who thinks is smarter than the others and wrote Bar

询问您的同事并解释为什么您想要重命名它。 可能他们会不关心, 除非他们真的相信原名更好 。 如果该方法位于一个以公众为主的 API 中, 或被数百名开发商使用, 那么将它贬值 。 否则, 只要使用一个重命名工具, 在每个地方重命名它 。

如果您试图使代码像 DSL 一样阅读, 并且想要用两种方式(如Ruby ) 来表达同样的话, 您也可以用别名来表示它 。





相关问题
How would you refactor this bit of code?

This bit of code runs on Windows Compact Framework and what it does is obvious. It looks as it should be refactored (especially considering that I may want to add cmd.ExecuteResultSet() later), but I ...

How to avoid debugger-only variables?

I commonly place into variables values that are only used once after assignment. I do this to make debugging more convenient later, as I m able to hover the value on the one line where it s later ...

Java Null Conditional

I have the following pattern appearing many times in my Java code and was wondering how some of you might refactor it. Object obj1 = buildObj1(); if (obj1 != null) { return obj1; } Object obj2 = ...

Writing refactoring-friendly PHP code

As far as I know, and have gathered from other SO posts, there are no proper tools for refactoring PHP code yet, so when it comes to refactoring, it s probably good old search-and-replace for most of ...

Refactor packages in a Jar

I have a requirement that i need to load two versions of a jar at once. To avoid class path collisions I d like to rename the packages of one of the jars. Then in source you could always easily ...

热门标签