Which methods in Ruby should contain a bang sign?
Sunday, February 1st, 2009I always thought the bang sign (!) in Ruby methods were meant to say: “I’m destructive, I’ll change your object”. Like this:
def do_something
end
def do_something!
end
Now I know better. Actually the bang sign (!) is only meant to give you and other developers a warning, like “I’m more dangerous than the other version, the non-bang one.”
Which methods should have a bang sign?
Matz says, on January 28, 2009 (EST):
The bang (!) does not mean “destructive” nor lack of it mean non destructive either. The bang sign means “the bang version is more dangerous than its non bang counterpart; handle with care”. Since Ruby has a lot of “destructive” methods, if bang signs follow your opinion, every Ruby program would be full of bangs, thus ugly.
And I (David A. Black) say:
So please stop writing bang methods that have no non-bang equivalent! The bang in isolation literally means nothing. It’s purely ornamental, and dilutes the conventional usage (see above) to the point where it’s
unrecognizable and pointless.
(Exception: things like Builder, where the bang/non-bang distinction is completely redefined, and documented as having been redefined, for the sake of domain-specific semantics.)
Source: this comment.