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

Thames Valley Meetup: Refactoring

Jack Franklin
November 05, 2014

Thames Valley Meetup: Refactoring

Jack Franklin

November 05, 2014
Tweet

More Decks by Jack Franklin

Other Decks in Technology

Transcript

  1. "Any fool can write code for a computer to understand.

    Good programmers write code that humans can understand" ! Martin Fowler
  2. var h = 400; var w = 400; var play

    = true; var calc = function()… ! for (var key in things)
  3. "There are only two hard things in Comp Sci, cache

    invalidation and naming things" ! Phil Karlton
  4. var placePin = function(x, y) ! var getLatLon = function(x,

    y) ! var user = { coordinates: [x, y] }
  5. if I were to hand the code over to you,

    what do I have to explain?
  6. var drawGraph = function(width, height) { width = 160 +

    width; height = 172.5 + height;
 }
  7. ! var drawGraph = function(width, height) { var graphWidthPad =

    160; width = graphWidthPad + width; …
 }
  8. function someFunc() { doSomething() and.then.something.else(); maybe.even.more(); var x = 2;

    var y = 3; keep.on.going(x); and.going.and.going(y); return on.and.on();
 }
  9. if params[:created_at][:gt] users = users.where("created_at >…") ! if params[:created_at][:lte] …

    ! if params[:created_at][:gte] … ! if params[:created_at][:lt] …
  10. filters = params[:created_at] ! map = { lte: '<=', gt:

    '>', … } ! filters.reduce(User.all) do |col, (key, val)| sym = map[key] col.where("created_at #{sym} ?", …) end
  11. filters = params[:created_at] users = User.all ! filters.reduce(users) do |coll,

    (key, val)| case key when :lte then coll.where(…) when :gt then coll.where(…) … end
  12. Can a user subscribe? • NO if they are the

    owner of the blog • NO if they are an admin of the blog • NO if they are already subscribed to the blog • NO if the blog is private • Else, totally.
  13. Can a user subscribe? • NO if they are the

    owner of the blog • NO if they are an admin of the blog • NO if they are already subscribed to the blog • NO if the blog is private • Else, totally.
  14. it "returns true if the user can" do res =

    UserSubscribe.can_subscribe? (user_id, blog) expect(res).to be(true) end
  15. it "returns true if the user can" … ! it

    "returns false if the blog is private" …
  16. it "returns true if the user can" … ! it

    "returns false if the blog is private" … ! it "returns false if the user owns the blog" …
  17. it "returns true if the user can" … ! it

    "returns false if the blog is private" … ! it "returns false if the user owns the blog" … ! it "returns false if the user is admin" …
  18. it "returns true if the user can" … ! it

    "returns false if the blog is private" … ! it "returns false if the user owns the blog" … ! it "returns false if the user is admin" … ! it "returns false if the user is subscribed" …
  19. class UserSubscribe def self.can_subscribe?(user_id, blog) if blog.private? false elsif user.owns?(blog)

    false elsif user.is_admin?(blog) false elsif user.is_subscribed?(blog) false else true end end end
  20. class UserSubscribe def self.can_subscribe?(user_id, blog) if blog.private? || user.owns?(blog) ||

    user.is_admin?(blog) || user.is_subscribed?(blog) false else true end end end
  21. class UserSubscribe def self.can_subscribe?(user_id, blog) ! return false if blog.private?

    return false if user.owns?(blog) return false if … return false if … true end end