如何解决某些函数名称中需要使用撇号的问题?

发布于 2024-08-17 20:02:59 字数 182 浏览 8 评论 0原文

当我编程时,我经常发现自己编写的函数应该(正确的英语)包含撇号(太糟糕了,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

纸短情长 2024-08-24 20:02:59

在这种情况下,我会执行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().

随波逐流 2024-08-24 20:02:59
getGroupForUser() 

或者

getGroupByUser()
getGroupForUser() 

or

getGroupByUser()
时光暖心i 2024-08-24 20:02:59

我原来的答案忽略它,继续! 是不完整的。您应该忽略不能在方法/函数名称中使用 ' 的事实。但您应该继续查看它们的命名,以更好地解释它们的作用。我认为这是编程中值得追求的。

选择 JavaScript,如果您想使用撇号,您可以

const user = {
    "get_user's_group": () => console.log("Naming things! Am I right?!")
}

user["get_user's_group"]()

但不要这样做

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:

const user = {
    "get_user's_group": () => console.log("Naming things! Am I right?!")
}

user["get_user's_group"]()

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:

user.get_group()

Failing that, the context of the surrounding code should be enough to make this your choice:

get_users_group()
临走之时 2024-08-24 20:02:59

getGroupByUser 怎么样?

How about getGroupByUser?

无力看清 2024-08-24 20:02:59

要么 get_user_ApostropiceShouldBeHereButLanguageWillNotLetMe_s_group 要么忽略它,因为它真的不重要。

Either get_user_ApostropheShouldBeHereButLanguageWillNotLetMe_s_group or just ignore it because it really doesn't matter.

伊面 2024-08-24 20:02:59

我忽略撇号 getGroupyUsergroup_from_user 都是完全可以理解的。担心函数名称中的语法是否正确是浪费时间,并且会偏离拥有清晰易懂的用户名的正确目标。

I ignore the apostraphe getGroupyUser and group_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.

蘑菇王子 2024-08-24 20:02:59

函数命名中正确的英语有点极端......

我的意思是为什么撇号会困扰你,但 _ 而不是空格却不会?

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 ?

情愿 2024-08-24 20:02:59

根据编程语言,您可能能够使用 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.

单身狗的梦 2024-08-24 20:02:59

两点:首先,如果可以避免的话,不要使用需要撇号的名称。其次,您对歧义的担忧是正确的。例如,您可以:

  • 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 like createUsersGroup, 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 user
  • getGroupByUserId: takes in the user's name or a unique ID associated with that user; returns the name of the group of the user

The 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.

尽揽少女心 2024-08-24 20:02:59

通常我只是删除撇号,但是反引号有效吗? (获取用户的组)

Normally I just drop the apostrophe, but do back-ticks work? (get_user`s_group)

随遇而安 2024-08-24 20:02:59

getGroupOfUser获取用户组

它是一种编程语言,而不是文学……

用正确的英语来说,它应该是 getBackgroundColour(而不是 getBackgroundColor

getGroupOfUser? getUserGroup?

It's a programming language, not literature...

It would be getBackgroundColour in proper English (rather than getBackgroundColor)

梦幻之岛 2024-08-24 20:02:59

就我个人而言,我会写 get_user_group() 而不是 get_group_for_user() 因为它对我来说读起来更好。当然,我使用一种允许在名称中使用撇号的编程语言:

proc get_user's_group {id} {#...}

尽管如此,一些多产的非英语母语欧洲用户将其用作单词分隔符:

proc user'group {id} {#...}

我想每个人都有自己的分隔符。

Personally I'd write get_user_group() rather than get_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:

proc get_user's_group {id} {#...}

Although, some of the more prolific non-English-native European users use it as a word separator:

proc user'group {id} {#...}

to each his own I guess..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文