I have a short question on ruby / rails method naming conventions or good practice. Consider the following methods:
# some methods performing some sort of 'action'
def action; end
def action!; end
# some methods checking if performing 'action' is permitted
def action?; end
def can_action?; end
def action_allowed?; end
So I wonder, which of the three ampersand-methods would be the "best" way to ask for permissions. I would go with the first one somehow, but in some cases I think this might be confused with meaning has_performed_action?.
So the second approach might make that clearer but is also a bit more verbose. The third one is actually just for completeness. I don't really like that one.
So are there any commonly agreed-on good practices for that?
I think that depends on the action you want to perform and the action you are checking for. I would aim for readability and least amount of confusion:
self.run
self.can_run? # can this object run?
self.running_allowed? # is running allowed here or by this user?
self.redeem!
self.redeemable? # is this object currently redeemable?
self.copy_to_clipboard
self.copy_to_dashboard
self.copyable? or self.can_be_copied? #can this object be copied
self.copy_allowed?(:clipboard) # is copying to the clipboard allowed by me?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With