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

How to Write Pleasant Code

A8461a517a89e1e2711883e1b51b0346?s=47 Roman Kofman
November 12, 2019

How to Write Pleasant Code

[RubyConf 2019 - Roman Kofman]

As we grow from beginner to intermediate developers, we tend to learn tools and Best Practices for writing good code. But. The more we learn, the more contradictions show up -- and the murkier it gets to find the right answers. Senior developers sometimes chime in with "it depends!". Which -- while being technically accurate, is also completely unhelpful. What does it depend on? How do we reconcile Best Practices when they conflict with each other? Who is "good" code actually good for? Is perfectly clean code even possible?

We'll learn to find the answers to all of these questions by exploring the discipline of Design (think fancy chairs in museum); and stealing from it shamelessly.

A8461a517a89e1e2711883e1b51b0346?s=128

Roman Kofman

November 12, 2019
Tweet

Other Decks in Programming

Transcript

  1. https://jake-ketchum.squarespace.com/blog/2017/4/20/metal-working-deconstruction-hammer ROMAN KOFMAN - @RKOFMAN How to write pleasant code

  2. https://www.flickr.com/photos/matthewbraid/2298403782 @rkofman

  3. https://www.flickr.com/photos/philipandlab/10685836895 @rkofman

  4. The code we write is itself a tool for producing

    future software. @rkofman
  5. For me the purpose of life is partly to have

    joy. … So Ruby is designed to make programmers happy. Yukihiro Matsumoto @rkofman
  6. How do we make code a better tool? @rkofman

  7. @rkofman

  8. https://refactoring.com/ @rkofman

  9. https://refactoring.com/ @rkofman

  10. @rkofman

  11. Presentation Title PRESENTER’S NAME https://www.flickr.com/photos/brighton/6878091316 @rkofman

  12. Code Design Principles • DRY - Don’t repeat yourself •

    High Cohesion / Low Coupling • SOLID @rkofman
  13. Code Design Principles • DRY - Don’t repeat yourself •

    High Cohesion / Low Coupling • SOLID • Single Responsibility • Open / Closed • Liskov Substitution • Interface Segregation • Dependency Inversion @rkofman
  14. @rkofman

  15. Code Design Principles • YAGNI - Ya aren’t gonna need

    it • Wrong Abstraction vs Duplication • Composition over Inheritance @rkofman
  16. get '/employee/:id' do |id| employee = Employee.find id EmployeePresenter.new(employee).serialize end

    @rkofman
  17. get '/employee/:id' do |id| employee = Employee.find id EmployeePresenter.new(employee).serialize end

    get '/employees' do employees = Employee.find_all employees.map do |employee| EmployeePresenter.new(employee).serialize end.to_s end @rkofman
  18. class EmployeePresenter def initialize employee @e = employee end def

    serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.family_name}" end end @rkofman
  19. Feature: Add job title to employee page @rkofman

  20. get '/employee/:id' do |id| employee = Employee.find id EmployeePresenter.new(employee).serialize end

    @rkofman
  21. class EmployeePresenter def initialize employee @e = employee end def

    serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.family_name}" end end @rkofman
  22. class EmployeePresenter def initialize employee @e = employee @job =

    Job.find employee.job_id end def serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary}, job: #{@job.title} \}} end def full_name "#{@e.given_name} #{@e.family_name}" end end @rkofman
  23. get '/employee/:id' do |id| employee = Employee.find id EmployeePresenter.serialize employee

    end get '/employees' do employees = Employee.find_all employees.map do |employee| EmployeePresenter.serialize employee end.to_s end @rkofman
  24. get '/employees' do employees = Employee.find_all employees.map do |employee| EmployeePresenter.serialize

    employee end.to_s end @rkofman
  25. employees.map do |employee| EmployeePresenter.serialize employee end.to_s @rkofman

  26. Fail! @rkofman

  27. class EmployeePresenter def initialize employee @e = employee @job =

    Job.find employee.job_id end def serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary}, job: #{@job.title} \}} end def full_name "#{@e.given_name} #{@e.family_name}" end end @rkofman
  28. class EmployeePresenter def initialize employee, job @e = employee end

    def serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary}, job: #{@job.title} \}} end def full_name "#{@e.given_name} #{@e.family_name}" end end @rkofman
  29. Presentation Title PRESENTER’S NAME https://www.flickr.com/photos/jerthebear/5850272087/ @rkofman

  30. class EmployeePresenter def initialize employee @e = employee end def

    serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.f... end end class EmployeePresenter def initialize employee @e = employee end def serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.fam end end @rkofman
  31. class EmployeePresenter def initialize employee @e = employee end def

    serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.f... end end class EmployeeListPresenter def initialize employee @e = employee end def serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.fam end end @rkofman
  32. @rkofman

  33. https://www.flickr.com/photos/librariesrock/14796401088/ @rkofman

  34. def fibonacci n if n == 1 || n ==

    2 1 else fibonacci(n-1) + fibonacci(n-2) end end def fibonacci n second_last = 1 (1..n-2).inject(1) do |last, i| current = second_last + last second_last = last current end end Which is better? @rkofman
  35. Which is better? https://www.flickr.com/photos/aikos/9431411118 https://www.flickr.com/photos/cogdog/30819427850 @rkofman

  36. https://www.flickr.com/photos/foilman/26792892279 @rkofman

  37. We need to focus on humans, on how humans care

    about doing programming. — Matz @rkofman
  38. Design https://commons.wikimedia.org/wiki/File:Centre_norv%C3%A9gien_de_design_et_darchitecture_(DOGA)_(4852913778).jpg @rkofman

  39. Human Centered Design @rkofman

  40. Presentation Title PRESENTER’S NAME https://www.flickr.com/photos/jeanbaptisteparis/5863913142 @rkofman

  41. @rkofman

  42. Person Action Context @rkofman

  43. Person https://www.flickr.com/photos/wocintechchat/25388752744 @rkofman

  44. Person • Be very specific; make them real • Explore

    edge-cases @rkofman
  45. Context https://www.flickr.com/photos/stevensnodgrass/30950768880 @rkofman

  46. Context Story time! @rkofman

  47. Context • Knowledge • Intention • Social environment @rkofman

  48. Action @rkofman

  49. Action • Perception • Action • Feedback @rkofman

  50. Person Action Context Human Centered Design @rkofman

  51. class EmployeePresenter def initialize employee @e = employee end def

    serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.f... end end class EmployeePresenter def initialize employee @e = employee end def serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.fam end end @rkofman
  52. class EmployeePresenter def initialize employee @e = employee end def

    serialize employee %{\{ full_name: #{full_name}, salary: #{@e.salary} \}} end def full_name "#{@e.given_name} #{@e.family_name}" end end @rkofman
  53. get '/employees' do employees = Employee.find_all employees.map do |employee| EmployeePresenter.new(employee).serialize

    end.to_s end @rkofman
  54. get '/employees' do employees = Employee.find_all without_db do # throws

    errors if DB queries are attempted employees.map do |employee| EmployeePresenter.new(employee).serialize end.to_s end end @rkofman
  55. Inspiration + Research Code Quality • Sarah Mei — Livable

    Code (RailsConf 2018) • Joe Mastey — Bringing UX to Your Code (RailsConf 2015) • Sandi Metz — Practical Object-Oriented Design in Ruby • Martin Fowler — Refactoring • Jim Weirich - Connascence Examined @rkofman
  56. Inspiration + Research Design • Steve Krug — Don’t Make

    Me Think • Robin Williams — Non-Designer’s Design Book • Don Norman — Design of Everyday Things • Webitects — (Where I first learned about Design + Contextual Research) @rkofman
  57. https://jake-ketchum.squarespace.com/blog/2017/4/20/metal-working-deconstruction-hammer How to write pleasant code ROMAN KOFMAN - @RKOFMAN

  58. square.com How to write pleasant code ROMAN KOFMAN - @RKOFMAN