替换 API/类的命名约定
对于正在逐步取代执行相同功能/担任相同角色的旧版本的 API 或类,您是否有命名约定?
例如,Windows 通过在函数末尾添加“Ex”来实现此目的:
ShellExecute // old
ShellExecuteEx // new
您喜欢什么,您的理由是什么?
- 是否要附加
2
、V2
、New
、NowInStereo
? - 将旧 API 从
Something
一次性重命名为SomethingOld
并使用Something
来处理新内容? 当涉及到版本控制时,这个选项让我担心,但它似乎也最不可能在未来受到V3
或ReallyNew
问题的困扰。 - 编造一个完全不同的名称可能不太准确地描述该功能,但至少是不同的。
Do you have a naming convention for APIs or Classes that are being phased in to replace an older version that performed the same function / filled the same role?
E.g. Windows does this by adding "Ex" to the end of the function:
ShellExecute // old
ShellExecuteEx // new
What do you prefer, and what are you reasonings?
- Appending
2
,V2
,New
,NowInStereo
? - Doing a one-time rename of the old API from
Something
toSomethingOld
and usingSomething
for the new stuff? This option worries me when it comes to version control, but it also seems the least likely to be burdened with aV3
orReallyNew
problem in the future. - Making up a completely different name that may describe the function less accurately, but at least is different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
大多数时候,您可以更改包名称,而不是类名称本身。
Much of the time you can get away with changing the package name, rather than the class name itself.
如果新类执行与旧类相同的功能,我将删除旧类并用同名的新类替换它。 如果需要的话,我仍然可以在版本控制中查阅旧版本。
If the new class performs the same function as the old one, I remove the old one and replace it with the new one with same name. I can still consult the old one in version control if needed.
为什么要改变它呢? 接口本身就是契约,为什么在所有东西都在使用接口之后还要破坏接口呢?
编辑:很抱歉搞砸了你的评论,乔什……
我认为,如果你不厌其烦地创建界面,你需要尽一切努力来维护它。 你之前评论的时候在想什么样的错误选择?
Why change it at all? Interfaces being the contracts that they are, why would you break the interface after everything is using it.
Edit: Sorry to botch your comment Josh...
I think that if you've gone to the trouble to create the interface you need to do everything you can to maintain it. What kind of bad choice were you thinking about when you commented before?
仅当函数的参数因函数的新要求而更改时,您才需要创建新函数。
API 的最佳命名约定是 API 用户所期望的命名约定。 如果 API 仅在 Windows 上使用,则添加 Ex(或下一版本的 Ex2)可能是合适的。 我不知道其他平台上的其他约定。 此外,您的编程语言可能有扩展 API 方法的约定。
如果您使用面向对象的语言并且这是一个对象方法,则不必更改名称,因为您可以拥有具有相同名称和不同签名的方法。 但是,提供新名称以让用户知道您希望他们迁移到新方法可能仍然有意义。
You only need to create a new function if the arguments to the function are changed due to new requirements for the function.
The best naming convention for an API is the one the users of the API would expect. If the API is used only on Windows then adding an Ex (or Ex2 for the next rev) may be appropriate. I'm not aware of other conventions on other platforms. Also, your programming language may have a convention for extending API methods.
If you are using an object oriented language and this is an object method, you do not have to change the name because you can have methods with the same name and different signatures. However, it may still make sense to provide a new name to let users know that you want them to migrate to the new method.