Skip to content
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

Refactor Journey::Routes #8783

Merged
merged 1 commit into from
Jan 6, 2013

Conversation

goshacmd
Copy link
Contributor

@goshacmd goshacmd commented Jan 6, 2013

  • prefer do-end for multiline blocks
  • prefer or-equals over returns with checks

@rafaelfranca
Copy link
Member

I really don't like tap it doesn't make anything better in that case.

@goshacmd
Copy link
Contributor Author

goshacmd commented Jan 6, 2013

@rafaelfranca ok, reverted it.

end

def ast
return @ast if @ast
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change the behavior since if @ast is present it still will check if partitioned_routes.first is blank. The before code doesn't behave this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see if @ast is set anywhere besides this method. Therefore, if partitioned_routes.first.empty? ever returns true, @ast won't get assigned. I think it is safe to make this change. Also, tests do pass.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you didn't get. Before this change in the first call all the method body is called, and in the second call only the first line. So partitioned_routes.first.empty? is only called on the first call.

After this change partitioned_routes.first.empty? is called in every call of this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, I think it can be refactored as follows:

@ast ||= begin
  asts = partitioned_routes.first.map(&:ast)
  Nodes::Or.new(asts) unless asts.empty?
end

So that, if partitioned_routes.first is empty, partitioned_routes.first.map(&:ast) would also be empty. Therefore Nodes::Or.new(asts) unless asts.empty? will set @ast to nil in case it's empty, and to Nodes::Or otherwise. That behavior is completely identical to the original one.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committed that & rebased.

* prefer do-end for multiline blocks
* prefer or-equals over returns with checks
rafaelfranca added a commit that referenced this pull request Jan 6, 2013
@rafaelfranca rafaelfranca merged commit 4bfcae0 into rails:master Jan 6, 2013
@goshacmd goshacmd deleted the refactor-journey-routes branch January 6, 2013 22:31
@goshacmd
Copy link
Contributor Author

goshacmd commented Jan 6, 2013

Thanks.

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

Successfully merging this pull request may close these issues.

None yet

2 participants