Skip to content

add count_threshold option #142

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

Merged
merged 5 commits into from
Nov 7, 2016

Conversation

devvmh
Copy link
Contributor

@devvmh devvmh commented Oct 18, 2016

adds the option to only check duplication if the code is found in more than just two locations.

fixes #131

@devvmh devvmh force-pushed the feature/count-threshold branch from fc38551 to b39d39f Compare October 18, 2016 07:16
@@ -51,6 +51,7 @@ def report
violations = new_violations(issue)

violations.each do |violation|
next if (violation.occurrences + 1) < language_strategy.count_threshold
Copy link
Contributor Author

Choose a reason for hiding this comment

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

here's where the magic happens

Copy link
Contributor

@dblandin dblandin left a comment

Choose a reason for hiding this comment

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

This PR looks great to me! One minor style suggestion and I'm also wondering if there's a spec we can add that exercises the change in the Reporter class.

Thanks!

@@ -30,6 +30,11 @@ def mass_threshold_for(language)
end
end

def count_threshold_for(language)
threshold = fetch_language(language).fetch("count_threshold", nil)
threshold ? threshold.to_i : threshold
Copy link
Contributor

Choose a reason for hiding this comment

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

This ternary conditional is a bit difficult to read when the switch value is also the default return.

Given the similarity to the #mass_threshold_for method above, it might make sense to consolidate:

threshold_config_for(:mass, "ruby")
threshold_config_for(:count, "ruby")

def threshold_config_for(option, language)
  threshold = fetch_language(language)["#{option}_threshold"]

  if threshold
   threshold.to_i
  end
end

Copy link
Contributor Author

@devvmh devvmh Oct 21, 2016

Choose a reason for hiding this comment

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

I think (and my feelings on this are related to the origins of this PR), that there's no point in abstracting these two methods until there's 3-4 similar config options (and I really do think there won't be any more)

You could also even do

def count_threshold_for(language)
  fetch_language(language).dig("count_threshold")&.to_i
end

if we wanted to get really crazy (well, I guess in ruby 2.3 only)

I'll switch it back to the old if threshold syntax

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point! 👍

@devvmh
Copy link
Contributor Author

devvmh commented Oct 21, 2016

I tried to update the spec, but it looks pretty involved because of where I put the code. Maybe I'll try again next week if I have time

@dblandin
Copy link
Contributor

@devvmh I'm good rolling with this as is. Going to do some local testing off of this branch before merging.

@devvmh
Copy link
Contributor Author

devvmh commented Oct 25, 2016

Great, thanks!

@dblandin
Copy link
Contributor

Hey @devvmh,

I've tested this locally and it works well! One question I have now that I've thought about it a bit more is whether this option should be configured one level up instead of nested under a particular language.

Do you think it's likely that users will want to specify different count thresholds per language? The mass_threshold configuration option works that way because of the difference in sensitivity between different language parser ASTs.

I'm thinking that it might be better to support this option as a sibling to languages:

engines:
  duplication:
    enabled: true
    config:
      count_threshold: 3
      languages:
        - ruby

What do you think?

@devvmh
Copy link
Contributor Author

devvmh commented Oct 28, 2016

Yeah that's a good question. I think for my particular case I would do the same for ruby and javascript. I do think that it's more useful to be per-language than not though, don't you? Like, it's not harder to configure per-language (just 4 extra lines of YAML worst case).

If you'd like me to change it, just say and I can do that

@dblandin
Copy link
Contributor

dblandin commented Oct 28, 2016

Just took a quick poll internally, and the consensus was basically:

giphy

So I think it would be ideal if we supported the configuration option at the higher position, but also allow language-specific overrides.

engines:
  duplication:
    enabled: true
    config:
      count_threshold: 3
      languages:
        ruby:
        python:
        javascript:
          count_threshold: 5

Does that sound good to you?

@devvmh
Copy link
Contributor Author

devvmh commented Oct 29, 2016

Yeah! brilliant. I'll tty and implement that in tte jext few days

On Sat, Oct 29, 2016, 1:03 AM Devon Blandin notifications@github.com
wrote:

Just took a quick poll internally, and the consensus was basically:

[image: giphy]
https://cloud.githubusercontent.com/assets/123595/19814977/a0582e40-9d0e-11e6-8d2e-c5c7e6723c8c.gif

So I think it would be ideal if we supported the configuration level at
the higher position, but also allowed language-specific overrides.

engines:
duplication:
enabled: true
config:
count_threshold: 3
languages:

    ruby:
    python:
    javascript:
      count_threshold: 5

Does that sound good to you?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#142 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABVELFIPvdTKm5fTTe9zMgb1S2eTe9rOks5q4irSgaJpZM4KZcxt
.

@@ -30,6 +30,15 @@ def mass_threshold_for(language)
end
end

def count_threshold_for(language)
threshold = fetch_language(language).fetch("count_threshold", nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about pushing the default count_threshold value into the EngineConfig class since languages don't have specific defaults?

 module CC
   module Engine
     module Analyzers
       class EngineConfig
         DEFAULT_COUNT_THRESHOLD = 2

         def count_threshold_for(language)
           fetch_language(language)["count_threshold"] || count_threshold || DEFAULT_COUNT_THRESHOLD
         end

         private

         def count_threshold
           config["count_threshold"]
         end
 module CC
   module Engine
     module Analyzers
       class Base
          def count_threshold
            engine_config.count_threshold_for(self.class::LANGUAGE)
          end

@devvmh
Copy link
Contributor Author

devvmh commented Nov 5, 2016

Phew, OK, that's finally updated.

@dblandin
Copy link
Contributor

dblandin commented Nov 5, 2016

@devvmh Awesome! The code looks great 👍

Last thing: Would you mind adding an example or two to spec/cc/engine/analyzers/engine_config_spec.rb to cover EngineConfig#count_threshold_for?

@devvmh devvmh force-pushed the feature/count-threshold branch from 5289dbb to 5adf9d3 Compare November 7, 2016 18:42
@devvmh devvmh force-pushed the feature/count-threshold branch from 5adf9d3 to 96cd4ef Compare November 7, 2016 18:43
@devvmh
Copy link
Contributor Author

devvmh commented Nov 7, 2016

OK, updated. Should I squash this into one commit?

@dblandin
Copy link
Contributor

dblandin commented Nov 7, 2016

@devvmh Perfect 👌

Should I squash this into one commit?

No need. I'm going use GitHub's handy "Squash and merge" button.

Thanks for the contribution! 🎉

@dblandin dblandin merged commit df62039 into codeclimate:master Nov 7, 2016
@devvmh devvmh deleted the feature/count-threshold branch November 7, 2016 19:35
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.

Only report if there are 3 occurrences of the code?
2 participants