Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Empty line before each control structure #82

Closed
gawlista opened this Issue · 3 comments

4 participants

Mathias Gawlista Ivo Wever Caleb Thompson Bozhidar Batsov
Mathias Gawlista

Regarding breaking up a method into logical paragraphs how about an empty line before control structures like if, while, return / return values, break, maybe also the ternary operator, etc. UNLESS it's the first line at the current "block".

Code sample:

def enforce_ssl?(req)
  enforce = false
  keys_by_type = { 
    :hosts => [:only_hosts, :except_hosts], :path => [:only, :except], 
    :methods => [:only_methods, :except_methods] 
  }

  if !keys_by_type.values.flatten.compact.any? { |option| @options[option] }
    return true
  end

  keys_by_type.keys.each do |type|
    enforce = enforce_ssl_for?(keys_by_type[type], req)

    next unless enforce

    keys_by_type.each do |other_type,sub_keys|
      next if type == other_type || !sub_keys.any? { |option| @options[option] }

      enforce = enforce_ssl_for?(sub_keys, req)

      break unless enforce
    end

    break if enforce
  end

  enforce   
end
Ivo Wever

I think that is a bit too much. I removed some newlines and the resulting version seems more coherent to me:

def enforce_ssl?(req)
  enforce = false
  keys_by_type = { 
    :hosts => [:only_hosts, :except_hosts], :path => [:only, :except], 
    :methods => [:only_methods, :except_methods] 
  }

  if !keys_by_type.values.flatten.compact.any? { |option| @options[option] }
    return true
  end

  keys_by_type.keys.each do |type|
    enforce = enforce_ssl_for?(keys_by_type[type], req)
    next unless enforce

    keys_by_type.each do |other_type,sub_keys|
      next if type == other_type || !sub_keys.any? { |option| @options[option] }
      enforce = enforce_ssl_for?(sub_keys, req)
      break unless enforce
    end

    break if enforce
  end

  enforce   
end
Mathias Gawlista

Looks good for me, too :-)

Caleb Thompson

I like to organize code into logical blocks like @Confusion's example. Lines can still belong together even if they aren't in the same control block.

Bozhidar Batsov bbatsov closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.