Skip to main content
edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Reduce cyclomatic complexity Validating the presence of properties in a document

added 464 characters in body
Source Link
Mike S
  • 131
  • 6

How can I reduce cyclomatic complexity of validation and refactor it better way?

Class Document
  def initialize(opts)
    @opts = opts
    return ArgumentError unless valid?
  end

  def valid?
    return false if @opts.nil?
    return false if @opts[:property_1].nil?
    return false if @opts[:property_2].nil?
    return false if !@opts[:property_4].nil? && @opts[:property_3].nil?
    # etc. 12 times more, otherwise valid will return true
    true
  end 

  def usage_example
   if @opts[:property_3]
      "#{@opts[:property_3]} #{@opts[:property_1]}"
   else
      "#{@opts[:property_4]} #{@opts[:property_2]}"
   end
  end
end

I am trying to avoid duplication of return false if and somehow perhaps list parameters that I want to verify

Possible interpretation of what I trying to reach is "verify set of rules before processing file"

How can I reduce cyclomatic complexity of validation and refactor it better way?

def valid?
  return false if @opts.nil?
  return false if @opts[:property_1].nil?
  return false if @opts[:property_2].nil?
  return false if !@opts[:property_4].nil? && @opts[:property_3].nil?
  # etc. 12 times more, otherwise valid will return true
  true
end

I am trying to avoid duplication of return false if and somehow perhaps list parameters that I want to verify

How can I reduce cyclomatic complexity of validation and refactor it better way?

Class Document
  def initialize(opts)
    @opts = opts
    return ArgumentError unless valid?
  end

  def valid?
    return false if @opts.nil?
    return false if @opts[:property_1].nil?
    return false if @opts[:property_2].nil?
    return false if !@opts[:property_4].nil? && @opts[:property_3].nil?
    # etc. 12 times more, otherwise valid will return true
    true
  end 

  def usage_example
   if @opts[:property_3]
      "#{@opts[:property_3]} #{@opts[:property_1]}"
   else
      "#{@opts[:property_4]} #{@opts[:property_2]}"
   end
  end
end

I am trying to avoid duplication of return false if and somehow perhaps list parameters that I want to verify

Possible interpretation of what I trying to reach is "verify set of rules before processing file"

edited body
Source Link
Mike S
  • 131
  • 6

How can I reduce cyclomatic complexity of validation and refactor it better way?

def valid?
  return false if @opts.nil?
  return false if @opts[:property_1].nil?
  return false if @opts[:property_2].nil?
  return false if !@opts[:property_1]property_4].nil? && @opts[:property_3].nil?
  # etc. 12 times more, otherwise valid will return true
  true
end

I am trying to avoid duplication of return false if and somehow perhaps list parameters that I want to verify

How can I reduce cyclomatic complexity of validation and refactor it better way?

def valid?
  return false if @opts.nil?
  return false if @opts[:property_1].nil?
  return false if @opts[:property_2].nil?
  return false if !@opts[:property_1].nil? && @opts[:property_3].nil?
  # etc. 12 times more, otherwise valid will return true
  true
end

I am trying to avoid duplication of return false if and somehow perhaps list parameters that I want to verify

How can I reduce cyclomatic complexity of validation and refactor it better way?

def valid?
  return false if @opts.nil?
  return false if @opts[:property_1].nil?
  return false if @opts[:property_2].nil?
  return false if !@opts[:property_4].nil? && @opts[:property_3].nil?
  # etc. 12 times more, otherwise valid will return true
  true
end

I am trying to avoid duplication of return false if and somehow perhaps list parameters that I want to verify

edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
added 164 characters in body
Source Link
Mike S
  • 131
  • 6
Loading
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
Source Link
Mike S
  • 131
  • 6
Loading