Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Code

Luís Ferreira
March 01, 2013
1.6k

Clean Code

A brief overview of how to write clean code,

Luís Ferreira

March 01, 2013
Tweet

Transcript

  1. 1. Looks like you cared 2. Pretty much what you

    expected 3. Language looks as perfect fit for problem My definition
  2. bad code class Report def initialize @title = 'Monthly Report'

    @text = [ 'Things are going', 'really, really well.' ] end def output_report ...HTML specific stuff... end end template method: reports
  3. template method: reports better code class Report def initialize @title

    = 'Monthly Report' @text = ['Things are going', 'really, really well.'] end def output_report output_start output_head @text.each do |line| output_line(line) end output_end end def output_start; end def output_head output_line(@title) end def output_line(line) raise 'Called abstract method: output_line' end def output_end; end end
  4. S O L I D ingle responsibility pen closed iskov

    substitution nterface segragation ependency inversion Uncle Bob Martin
  5. Can’t tell what it does at a glance, it’s TOO

    LONG More than one level of nesting, probably TOO LONG TOO LONG More than one level of abstraction, may be Flog score greater than 15, may be TOO LONG REFACTOR
  6. Need to scroll to know what it does, it’s TOO

    BIG More private methods than public, it’s TOO BIG TOO BIG More than seven methods, probably Flog score greater than 50, may be TOO BIG REFACTOR
  7. Smelling feature envy: Local variables or params that are used

    more than instance variables and methods
  8. Extract method: If possible move part of a method to

    the appropriate class Solution #1