-
Notifications
You must be signed in to change notification settings - Fork 3
Allow use of and/or operators for control flow #23
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
Open
davepilling
wants to merge
1
commit into
master
Choose a base branch
from
and-or-operators-control-flow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we want to use...
instead of a guard
if
?How do we choose? A bit wary of opening up a "two ways to do one thing" situation, when the existing codebase is consistently using a form that looks just as powerful.
The feeling I have is that
and
/or
are more readable when the method call reads like an action:while guard
if
/unless
work better with boolean methods:However, the first case requires the method to return a truth-y value, which feels like... I suppose it reminds me of PHP, where most methods return
false|value
andand
/or
are often used for control flow – but there are not guardif
s in PHP!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would go with the
return...if
if that case. I do think think this is a poor example when you could use a guard if instead.As you say, I think and/or work better when you're checking whether an action was successful. You can achieve the same thing with if/unless but I find that is actually less readable in the action case:
Happy to modify the examples if you think that pattern makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our examples above,
do_something
needs to return a truth-y value, but does not guarantee a boolean asdo_something?
would.Are we putting a invisible dependency on the called method then, and risk someone changing
do_something
in a perfectly valid way, which breaks the implicit truth-y-ness return requirement? Is that an actual risk, and is it acceptable in respect to what we gain from relaxing the rules aroundand
/or
?(Maybe the above is not very clear, I'll try again with a fresh brain tomorrow)