Skip to content

Commit

Permalink
Prefer GuardClauses to if/else
Browse files Browse the repository at this point in the history
  • Loading branch information
veganstraightedge committed Sep 23, 2018
1 parent 42182e6 commit e5bb728
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
6 changes: 0 additions & 6 deletions ruby-gatekeeper/.rubocop_todo.yml
Expand Up @@ -59,12 +59,6 @@ Style/GlobalVars:
- 'app/helpers/method_helpers.rb'
- 'server.rb'

# Offense count: 3
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'app/helpers/method_helpers.rb'

# Offense count: 8
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Expand Down
25 changes: 11 additions & 14 deletions ruby-gatekeeper/app/helpers/method_helpers.rb
Expand Up @@ -5,14 +5,13 @@ def all_letters_or_digits(str)

# returns true if the string contents map to a positive, non float, integer
def positive_integer?(sample_rate)
if !/\A\d+\z/.match(sample_rate)
# sample rate string maps to a negative number or non-integer
# false
unless /\A\d+\z/ =~ sample_rate
# sample rate string maps to a negative number or non-integer false
raise BadSampleRate.new("error": 'bad sample rate provided')
else
# Is all good ..continue
sample_rate
end

# Is all good ...continue
sample_rate
end

def validate_write_key(users_write_key)
Expand Down Expand Up @@ -40,11 +39,9 @@ def resolve_dataset(given_dataset)
def grab_partition(given_dataset)
available_partitions = given_dataset.partition_list

if available_partitions.length <= 0
raise 'no partitions found'
else
return available_partitions.sample
end
raise 'no partitions found' if available_partitions.length <= 0

available_partitions.sample
end

def get_schema(_given_dataset)
Expand All @@ -62,9 +59,9 @@ def get_schema(_given_dataset)
Rack::Honeycomb.add_field(env, 'hit_schema_cache', hit_cache)

# let's just fail sometimes to pretend
if rand(60) == 0
raise SchemaLookupFailure.new("error": 'failed to resolve schema')
end
return unless rand(60).zero?

raise SchemaLookupFailure.new("error": 'failed to resolve schema')
end

def write_event(event)
Expand Down

0 comments on commit e5bb728

Please sign in to comment.