当所需名称已定义时的变量命名约定
当您想要的名称已由语言定义时,是否有命名变量的约定?举个例子,我目前正在编写一个 Lisp 函数,它需要两个参数:min 和 max。 Vim 的语法荧光笔为这些单词着色,所以看起来它们已经是 lisp 函数了。我认为最好为参数指定不同的名称。
我应该使用完全不同的名称吗? min 和 max 都很简短且具有描述性,所以如果可能的话我想使用它们。我应该使用前缀,例如 myMin 和 myMax 吗?我目前倾向于这个想法。任何建议都会有帮助。
Is there a convention for naming a variable when the name you want is already defined by the language? As an example, I'm currently coding a lisp function that takes two parameters, min and max. Vim's syntax highlighter colors those words though, so it looks like they're already lisp functions. I assume it'd be better to give the parameters different names.
Should I use completely different names? min and max are both short and descriptive though, so I'd like to use them if possible. Should I use a prefix, like myMin and myMax? I'm currently leaning towards that idea. Any suggestions would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在寻找一种非常通用的命名约定 - 能够与您的语言或框架中保留/定义的所有名称很好地配合的命名约定 - 我真的怀疑这种约定是否实用甚至有用。
我认为你应该根据(常见)具体情况来进行操作。
您的示例看起来像是这种常见情况之一:函数具有两个指定某个值范围的参数可能很常见。好吧,在这种情况下,我可能会选择像
minValue
和maxValue
这样的名称 - 它们似乎足够抽象,可以在大多数情况下很好地工作。顺便说一句,我不会使用像
my
这样的前缀。但是,如果您对 Apps Hungarian 的想法持开放态度(请参阅维基百科上的讨论和 Joel Spolsky 的文章),你的问题的答案会简单得多:只需在参数名称中使用正确的语义前缀(例如xMin
和xMax
表示 min和最大横坐标值)。If you are looking for a very general naming convention - the one that would work well with all names reserved/defined in your language or framework - I really doubt that such convention would be practical or even useful.
I think you should do it on a (common) case by case basis.
Your example looks like one of such common cases: it probably is quite common for functions to have two parameters specifying some range of values. Well, in that case, I'd probably go with names like
minValue
andmaxValue
- they seem to be abstract enough to work well in most situations.BTW, I would not use a prefix like
my
. However, if you were open to the idea of Apps Hungarian (see discussion on Wikipedia and Joel Spolsky's article), the answer to your question would be much simpler: just use a proper semantic prefix in the names of your parameters (e.g.xMin
andxMax
for min and max abscissa values).