Thames Valley Meetup: Refactoring

Aea964cf59c0c81fff752896f070cbbb?s=47 Jack Franklin
November 05, 2014

Thames Valley Meetup: Refactoring

Aea964cf59c0c81fff752896f070cbbb?s=128

Jack Franklin

November 05, 2014
Tweet

Transcript

  1. 3.
  2. 5.
  3. 6.
  4. 7.
  5. 9.

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

    Good programmers write code that humans can understand" ! Martin Fowler
  6. 10.
  7. 14.

    var h = 400; var w = 400; var play

    = true; var calc = function()… ! for (var key in things)
  8. 16.

    "There are only two hard things in Comp Sci, cache

    invalidation and naming things" ! Phil Karlton
  9. 17.

    var placePin = function(x, y) ! var getLatLon = function(x,

    y) ! var user = { coordinates: [x, y] }
  10. 18.
  11. 20.

    if I were to hand the code over to you,

    what do I have to explain?
  12. 21.

    var drawGraph = function(width, height) { width = 160 +

    width; height = 172.5 + height;
 }
  13. 22.
  14. 23.

    ! var drawGraph = function(width, height) { var graphWidthPad =

    160; width = graphWidthPad + width; …
 }
  15. 25.

    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();
 }
  16. 44.

    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] …
  17. 45.
  18. 46.

    filters = params[:created_at] ! map = { lte: '<=', gt:

    '>', … } ! filters.reduce(User.all) do |col, (key, val)| sym = map[key] col.where("created_at #{sym} ?", …) end
  19. 47.
  20. 49.

    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
  21. 50.
  22. 57.

    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.
  23. 62.

    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.
  24. 63.

    it "returns true if the user can" do res =

    UserSubscribe.can_subscribe? (user_id, blog) expect(res).to be(true) end
  25. 65.
  26. 66.

    it "returns true if the user can" … ! it

    "returns false if the blog is private" …
  27. 67.
  28. 69.
  29. 70.

    it "returns true if the user can" … ! it

    "returns false if the blog is private" … ! it "returns false if the user owns the blog" …
  30. 71.
  31. 73.
  32. 74.

    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" …
  33. 76.

    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" …
  34. 77.

    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
  35. 78.
  36. 81.
  37. 82.

    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
  38. 83.
  39. 84.

    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
  40. 85.
  41. 91.
  42. 95.