jmcnevin / rubypants

RubyPants: SmartyPants for Ruby
Other
29 stars 20 forks source link

Reduce calls to `#entity` using local variables #18

Closed ashmaroli closed 6 years ago

ashmaroli commented 6 years ago

Slight improvement to #educate_quotes by dropping unnecessary calls to #entity

agriffis commented 6 years ago

Hi @ashmaroli

Thanks for the PR. This trades calls for temporary variables. It's not a clear win. Could you explain your reasoning?

ashmaroli commented 6 years ago

It's not a clear win.

You're right. Benchmarks don't report much of a gain. (The minor gain tilts towards this patch, albeit not so discernible) My rationale for submitting the PR anyways is reduced "code-smell" due to reduced calls to a method with the same argument.

ashmaroli commented 6 years ago

I also planned on submitting a PR assigning the various regexps to constants so that they are not created per call to #educate_quotes

agriffis commented 6 years ago

You're right. Benchmarks don't report much of a gain. (The minor gain tilts towards this patch, albeit not so discernible) My rationale for submitting the PR anyways is reduced "code-smell" due to reduced calls to a method with the same argument.

For code smell, I'd be more interested in accessing @entities directly rather than creating temporary vars.

I also planned on submitting a PR assigning the various regexps to constants so that they are not created per call to #educate_quotes

I'm more interested in code clarity and maintainability than performance unless there's a significant measurable improvement.

ashmaroli commented 6 years ago

I'd be more interested in accessing @entities directly

calling @entities[:single_left_quote] multiple times is functionally identical to entity(:single_left_quote) being called multiple times.. I don't see how that's an improvement in code-smell

agriffis commented 6 years ago

no you're right, it doesn't improve code smell. It just removes an unnecessary indirection.

But to me, this is bad code smell:

foo = something(:foo)
bar = something(:bar)
baz = something(:baz)
qux = something(:quz)

It's just a lot of literal repetition to pull data into same-name temporaries. Easy to introduce errors since it's manual instead of programmatic.