语法糖方法的命名约定

发布于 2025-01-01 13:06:22 字数 609 浏览 1 评论 0原文

我正在构建一个用于通用报告的库,Excel(使用电子表格),大多数时候我会写东西在最后创建的工作表上(或我主要指的活动工作表)。

所以我想知道对于普通/无糖方法来说大部分是糖的方法是否有命名约定。

例如,我看到了一篇博客文章 ,向下滚动到 Composite,不久前作者使用了 #method 来表示加糖,以及 #method!当不加糖/手动时。
这可以说是一种正常的做事方式,还是只是一种奇怪的实现?

我现在想做的是:

add_row(data)  
add_row!(sheet, data)

这感觉很适合我,但是对于这些方法应该如何命名是否有共识?

编辑 我知道!用于“危险”方法,?用于查询/布尔响应。这就是为什么我很好奇 Prawn(博客文章)中的用法是否可以说是正常的。

I'm build a library for generic reporting, Excel(using Spreadsheet), and most of the time I'll be writing things out on the last created worksheet (or active as I mostly refer to it).

So I'm wondering if there's a naming convention for methods that are mostly sugar to the normal/unsugared method.

For instance I saw a blog post, scroll down to Composite, a while ago where the author used the #method for the sugared, and #method! when unsugared/manual.
Could this be said to be a normal way of doing things, or just an odd implementation?

What I'm thinking of doing now is:

add_row(data)  
add_row!(sheet, data)

This feels like a good fit to me, but is there a consensus on how these kinds of methods should be named?

Edit
I'm aware that the ! is used for "dangerous" methods and ? for query/boolean responses. Which is why I got curious whether the usage in Prawn (the blog post) could be said to be normal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

深空失忆 2025-01-08 13:06:22

我认为可以公平地说,您的定义:

add_row(data)  
add_row!(sheet, data)

会让 Ruby 用户感到困惑。 Ruby 社区有很多命名约定,它们被认为是事实上的命名标准。例如,bang 方法旨在修改接收器,请参阅map地图!。另一个约定是将 ? 添加为返回布尔值的方法的后缀。请参阅全部?有吗?供参考。

I think it's fair to say that your definitions:

add_row(data)  
add_row!(sheet, data)

are going to confuse Ruby users. There is a good number of naming conventions is the Ruby community that are considered like a de-facto standard for naming. For example, the bang methods are meant to modify the receiver, see map and map!. Another convention is add the ? as a suffix to methods that returns a boolean. See all? or any? for a reference.

墨落成白 2025-01-08 13:06:22

我曾经将 bang-methods 视为常规命名方法的更危险版本:

  • Array#reverse! 修改数组本身,而不是返回元素顺序相反的新数组。

  • ActiveRecord::Base#save! (来自 Rails)验证模型并保存它(如果它有效)。但与常规版本不同,常规版本根据模型是否保存返回 truefalse ,如果模型无效,则会引发异常。

我不记得曾将 bang-methods 作为常规方法的加糖替代品。也许我会给这些方法赋予自己独特的名称,而不是在常规版本名称中添加一个感叹号。

I used to see bang-methods as more dangerous version of a regular named method:

  • Array#reverse! that modifies array itself instead of returning new array with reversed order of elements.

  • ActiveRecord::Base#save! (from Rails) validates model and save it if it's valid. But unlike regular version that return true or false depending on whether the model was saved or not raises an exception if model is invalid.

I don't remember seeing bang-methods as sugared alternatives for regular methods. May be I'd give such methods their own distinct name other then just adding a bang to regular version name.

伪装你 2025-01-08 13:06:22

为什么有两个单独的方法?例如,您可以将工作表设为可选参数,例如

def add_row(sheet = active_sheet, data)
  ...
end

默认值不必只是静态值 - 在本例中,它调用 active_sheet 方法。如果我的记忆在 ruby​​ 1.9 之前是正确的,那么您必须交换参数,因为可选参数后面不能跟非可选参数。

我同意其他答案,即 ! 对我来说具有相当不同的含义。

Why have two separate methods? You could for example make the sheet an optional parameter, for example

def add_row(sheet = active_sheet, data)
  ...
end

default values don't have to just be static values - in this case it's calling the active_sheet method. If my memory is correct prior to ruby 1.9 you'd have to swap the parameters as optional parameters couldn't be followed by non optional ones.

I'd agree with other answers that ! has rather different connotations to me.

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