如何解决某些函数名称中需要使用撇号的问题?
当我编程时,我经常发现自己编写的函数应该(正确的英语)包含撇号(太糟糕了,C 开始让每个人都认为撇号是一个合适的分隔符)。例如: get_user's_group() -> get_users_group() 。你们怎么处理这个forced-bad-english模棱两可的英语?就忽略撇号吗?创建不同的措辞?
When I'm programming, I often find myself writing functions that -should- (to be proper english) contain apostrophes (too bad C started everyone thinking that an apostrophe was an appropriate delimiter). For example: get_user's_group() -> get_users_group()
. What do you guys do with that forced-bad-english ambiguous english? Just ignore the apostrophe? Create a different phrasing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在这种情况下,我会执行
get_group_for_user()
。所以,是的,我会“创建一个不同的措辞”:)
要么是这样,要么是
user.get_group()
。In that case, I would do
get_group_for_user()
.So, yes, I would "create a different phrasing" :)
Either that, or
user.get_group()
.或者
or
我原来的答案
忽略它,继续!
是不完整的。您应该忽略不能在方法/函数名称中使用'
的事实。但您应该继续查看它们的命名,以更好地解释它们的作用。我认为这是编程中值得追求的。选择 JavaScript,如果您想使用撇号,您可以:
但不要这样做
My original answer of
Ignore it, move on!
is incomplete. You should ignore the fact you can't use'
in your method/function names. But you should continue to look at the naming of them to better explain what they do. I think this is a worthwhile pursuit in programming.Picking on JavaScript, you could if you wanted to use apostrophes:
But don't do that ????
Taking it further, you could if you wanted to, use a transpiler to take your grammatically correct name and transform it into something you never see.
Again with JavaScript as an example, maybe you could write a babel transform.
But don't do that ????
As others have said, if there is context available from an object, that's a nice option:
Failing that, the context of the surrounding code should be enough to make this your choice:
getGroupByUser
怎么样?How about
getGroupByUser
?要么
get_user_ApostropiceShouldBeHereButLanguageWillNotLetMe_s_group
要么忽略它,因为它真的不重要。Either
get_user_ApostropheShouldBeHereButLanguageWillNotLetMe_s_group
or just ignore it because it really doesn't matter.我忽略撇号
getGroupyUser
和group_from_user
都是完全可以理解的。担心函数名称中的语法是否正确是浪费时间,并且会偏离拥有清晰易懂的用户名的正确目标。I ignore the apostraphe
getGroupyUser
andgroup_from_user
are both perfectly understandable. Worrying about having correct grammer in your function names is a waste of time and distracts from the correct goal of having clear and understandable user names.函数命名中正确的英语有点极端......
我的意思是为什么撇号会困扰你,但 _ 而不是空格却不会?
the point of proper english in function naming is a bit extreme ...
i mean why is the apostrophe bothering you but the _ instead of a space is not ?
根据编程语言,您可能能够使用 Unicode 变量名称,这个 SO 线程列出了一些。
对于 Unicode 标识符,您可以使用 unicode 撇号 为变量名称提供正确的英语语言格式。虽然这只是推测。而且维护起来会很困难。事实上,现在想来,这听起来简直是邪恶的。
Depending on the programming language you may be able to use Unicode variable names, this SO thread lists a few.
With Unicode identifiers you could use one of the unicode apostrophes to give the proper english language formatting to your variable name. Though this only speculative. And it would be hard to maintain. Actually, now that I think about it, it sounds downright evil.
两点:首先,如果可以避免的话,不要使用需要撇号的名称。其次,您对歧义的担忧是正确的。例如,您可以:
getUsersGroup
:获取用户列表的组。如果您使用面向对象的语言,则这可能包含比组 ID 字符串更多的信息。您还可以有类似createUsersGroup
的东西,它会根据传入的用户列表创建一个组对象。getGroupOfUser
:接受某种用户对象;返回用户组的名称getGroupByUserId
:获取用户名或与该用户关联的唯一 ID;返回用户组的名称描述所有这些之间差异的最佳方法是仅使用解释方法名称的标准方法注释。这取决于您使用的语言以及您的组织通常使用的方法注释风格。
Two points: First, don't use a name that would otherwise require an apostrophe if you can avoid it. Second, you are right in being concerned about ambiguity. For example, you could have:
getUsersGroup
: gets the group of a list of users. If you are using an object-oriented language, this could have more information than just a group ID string. You could also have something likecreateUsersGroup
, which would create a group object from a list of users passed in.getGroupOfUser
: takes in some sort of user object; returns the name of the group of the usergetGroupByUserId
: takes in the user's name or a unique ID associated with that user; returns the name of the group of the userThe best way to delineate the difference between all of these is to just use standard method comments that explain the method names. This would depend on what language you are working with and what style of method comments your organization conventionally uses.
通常我只是删除撇号,但是反引号有效吗? (获取用户的组)
Normally I just drop the apostrophe, but do back-ticks work? (get_user`s_group)
getGroupOfUser
?获取用户组
?它是一种编程语言,而不是文学……
用正确的英语来说,它应该是
getBackgroundColour
(而不是getBackgroundColor
)getGroupOfUser
?getUserGroup
?It's a programming language, not literature...
It would be
getBackgroundColour
in proper English (rather thangetBackgroundColor
)就我个人而言,我会写
get_user_group()
而不是get_group_for_user()
因为它对我来说读起来更好。当然,我使用一种允许在名称中使用撇号的编程语言:尽管如此,一些多产的非英语母语欧洲用户将其用作单词分隔符:
我想每个人都有自己的分隔符。
Personally I'd write
get_user_group()
rather thanget_group_for_user()
since it feels like it reads better to me. Of course, I use a programming language where apostrophes are allowed in names:Although, some of the more prolific non-English-native European users use it as a word separator:
to each his own I guess..