Skip to content

and / or keywords with raise / fail #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
smanolloff opened this issue Feb 11, 2015 · 8 comments
Closed

and / or keywords with raise / fail #415

smanolloff opened this issue Feb 11, 2015 · 8 comments

Comments

@smanolloff
Copy link

The idea of banning the and & or keywords is really cool.

However, there is one specific case in which they may be preferred:

authenticate_user! || fail AuthenticationFailure, 'Invalid credentials'
# SyntaxError: unexpected '\n', expecting :: or '[' or '.'

authenticate_user! or fail AuthenticationFailure, 'Invalid credentials'
# (this works ok)

Of course, we we could just use

fail AuthenticationFailure, 'Invalid credentials' unless authenticate_user!

... but doesn't it degrade readability?

Is the usage of and or or, immediately followed by a raise or fail, justified?
I would be happy if you could share your thoughts!

@philoserf
Copy link

I find the object.test or fail 'message' construction is very common in the wild.

@bbatsov
Copy link
Collaborator

bbatsov commented Feb 18, 2015

I find the object.test or fail 'message' construction is very common in the wild.

In Perl. :-) Seriously, though - yeah, it's popular to some extent but way more people use modifier if/unless for such code.

@smanolloff
Copy link
Author

Sure, i guess that is fine.
But we should not forget that the if / unless one-liners are not exactly the same as the boolean operators:

def current_user
  fail 'Authentication failed' unless @current_user ||= authenticate
  @current_user # need to return explicitly
end

def current_user
  @current_user ||= authenticate or fail 'Authentication failed'
end

@luizdamim
Copy link

👍 for allowing and/or followed by raise/fail. I use it quite frequently in my codebase.

@Ajedi32
Copy link

Ajedi32 commented Jul 10, 2015

I think the real crux of the issue is that and and or are perfectly fine to use for their intended purpose: as control flow operators. The problems they cause usually occur when people try to use them in place of || and && as boolean operators.

Then again though, if and unless can usually be used in place of or and and, so idk...

@bbatsov
Copy link
Collaborator

bbatsov commented Jul 10, 2015

I think the real crux of the issue is that and and or are perfectly fine to use for their intended purpose: as control flow operators.

Pretty sure those were never intended as flow of control operators. This is something which people came up with to justify their existence. It's pretty clear they were actually added to facilitate a style of programming with less parentheses in it, which is a good or a bad thing, depending who you ask.

@cheerfulstoic
Copy link

Avdi Grimm has a really great (and free) RubyTapas episode about this:

http://devblog.avdi.org/2014/08/26/how-to-use-rubys-english-andor-operators-without-going-nuts/

👍 on use as flow control operators!

@pirj
Copy link
Member

pirj commented Feb 21, 2021

Duplicate of #730

@pirj pirj marked this as a duplicate of #730 Feb 21, 2021
@pirj pirj closed this as completed Feb 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants