Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby / rails boolean method naming conventions

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?

like image 411
Dennis Avatar asked Nov 19 '25 00:11

Dennis


1 Answers

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?
like image 146
Pan Thomakos Avatar answered Nov 20 '25 15:11

Pan Thomakos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!