8000 Refactor method to reduce ABC Metric size by ashmaroli · Pull Request #6529 · jekyll/jekyll · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Refactor method to reduce ABC Metric size #6529

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 3 commits into from
Nov 7, 2017

Conversation

ashmaroli
Copy link
Member
@ashmaroli ashmaroli commented Nov 7, 2017

Assignment Branch Condition Size for the method on repo branches (as reported by RuboCop) :

  • master : 29.7
  • PR-branch: 20.1 13.64

jekyll_options = opts[:JekyllOptions]
ssl_cert = jekyll_options["ssl_cert"]
ssl_key = jekyll_options["ssl_key"]
return if !ssl_cert && !ssl_key
Copy link
Member

Choose a reason for hiding this comment

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

return unless ssl_cert || ssl_key

Copy link
Member Author

Choose a reason for hiding this comment

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

the original line defined it along similar lines..

return if !opts[:JekyllOptions]["ssl_cert"] && !opts[:JekyllOptions]["ssl_key"]

I just replaced with local variables..

Copy link
Member

Choose a reason for hiding this comment

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

As long as it’s being refactored, it should be refactored to be more clear 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

@pathawks local testing showed me that the two are functionally different even though they appear same..

Copy link
Contributor
@envygeeks envygeeks Nov 7, 2017

Choose a reason for hiding this comment

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

def enable_ssl(opts)
  key, cert =<
10000
/span> opts[:JekyllOptions].values_at("ssl_cert", "ssl_key")
  return if cert.nil? && key.nil?

  unless cert && key
    raise RuntimeError, "missing --ssl_cert or " \
      "--ssl_key.  Both are required."
  end
  
  src = Pathutil.new(opts[:JekyllOptions]["source"])
  opts[:SSLPrivateKey ] = OpenSSL::PKey::RSA.new(src.join(key).cleanpath.read)
  opts[:SSLCertificate] = OpenSSL::X509::Certificate.new(src.join(cert).cleanpath.read)
  opts[:SSLEnable] = true
end

Copy link
Member Author

Choose a reason for hiding this comment

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

sample file:

def iflog(input="foo", alpha=false, beta=false)
  return if !alpha && !beta
  print "INPUT: "
  puts input
end

def unlog(input="foo", alpha=false, beta=false)
  return unless alpha && beta
  print "INPUT: "
  puts input
end
  • load sample in irb
  • exec following couplets:
    iflog # => nil
    unlog # => nil
    
    iflog("bar", true) # => INPUT: "bar"\n nil
    unlog("bar", true) # => nil
    
    iflog("bar", false, true) # => INPUT: "bar"\n nil
    unlog("bar", false, true) # => nil
    
    iflog("bar", true, true) # => INPUT: "bar"\n nil
    unlog("bar", true, true) # => INPUT: "bar"\n nil

Copy link
Member

Choose a reason for hiding this comment

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

I don’t get it.
By De Morgan’s Law, if we want to negate the statement, we have to negate the whole thing:

if !a && !b
if !!(!a && !b)
unless !(!a && !b)
unless !!a || !!b
unless a || b

Copy link
Contributor
@envygeeks envygeeks Nov 7, 2017

Choose a reason for hiding this comment

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

Guys, the first one checks to see if neither exists and then leaves, the second one checks to see if either one doesn't exist and then throws. The first says if both don't exist then cool, lets bail out, the second says that if both don't exist then this is garbage.

Copy link
Contributor

Choose a reason for hiding this comment

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

Writing it as so should make life much easier and avoid this debate later.

return if a.nil? && b.nil?
raise RuntimeError, "need both" unless a && b

Copy link
Contributor

Choose a reason for hiding this comment

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

Who would have thought nil? would solve a semantics problem. 🤷‍♂️

 - use parallel assignment via Hash#values_at
 - use nil checks to solve a semantics issue when using negated expressions
   with &&
 - output better message when an exception is raised

---

Though he suggested the use of Pathutil lib for both sanitizing the paths
to the SSL-Certificate & SSL-Key files, and for reading them, I elected to
abstract existing logic to a new private method.
@DirtyF DirtyF added this to the v3.7.0 milestone Nov 7, 2017
@DirtyF
Copy link
Member
DirtyF commented Nov 7, 2017

@jekyllbot: merge +dev

@jekyllbot jekyllbot merged commit 758ee7e into jekyll:master Nov 7, 2017
jekyllbot added a commit that referenced this pull request Nov 7, 2017
@ashmaroli ashmaroli deleted the reduce-ABC-size branch November 8, 2017 06:13
@ashmaroli
Copy link
Member Author

Thanks for your inputs on this one @envygeeks

@jekyll jekyll locked and limited conversation to collaborators Jul 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0