5 ms·
Shameless plug: After years working with Ruby, I felt that "bang" methods should only be used for methods which mutate the receiver. Many methods which do do s
by pmarreck 10y ago
Shameless plug:
After years working with Ruby, I felt that "bang" methods should only be used for methods which mutate the receiver. Many methods which do do so do not have a "!" at the end.
To that end I created a little library called "Bang All The Things" :) https://github.com/pmarreck/ruby-snippets/blob/master/bang_all_the_things.rb https://github.com/pmarreck/ruby-snippets/blob/master/bang_a...
I'm convinced that this simplification helps prevent bugs, at the minor cost of changing some conventions possibly borrowed from other languages.
I have since (mostly) moved on to Elixir which does not have the "mutability problem" at all.
- steveklabnik 10y ago> I felt that "bang" methods should only be used for methods which mutate the receiver. I mentioned this upthread too, but it's a bit broader than that. Consider http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-first http://api.rubyonrails.org/classes/ActiveRecord/FinderMethod... vs http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-first-21 http://api.rubyonrails.org/classes/ActiveRecord/FinderMethod... for example. The ! version raises an exception rather than return nil.
- hawkice 10y agoI actually like the ActiveRecord convention more than destructive-to-receiver -- it was even adopted by the elixir community (which makes even more sense than ruby, as there really aren't destructive updates in elixir).
- pmarreck 10y agoGood points. It would be great if there were two separately available suffixes, one to signify receiver mutation and one to signify raising vs nil. I certainly think Elixir went the right way here on the latter front (as it doesn't have to worry about receiver mutation).
- vinhboy 10y agoAgreed. For whatever reason, my mind really gravitate towards the idea of bang being a mutator. That should have been a standard. `delete` always gets me.