Programming for People

Programming for People

638d7e4bb8d455e8149b2f46355c0973?s=128

Samson Ootoovak

June 09, 2013
Tweet

Transcript

  1. 3.
  2. 4.

    But as you learn more about programming, it is good

    to remember that programming is not just for computers.
  3. 8.
  4. 12.

    In 1952 woman named Grace Hopper wrote the first compiler.

    The was the first step in abstraction away from the 1's and 0's and towards code that was easier for people to read. (Side note: Grace Hopper was also the person the coined the term debugging after finding and removing an actual moth from a computer.)
  5. 13.

    Fast forward a few years... and now we have programming

    languages like Ruby. “For me, the purpose of life is, at least partly, to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.” -- Yukihiro Matsumoto (a.k.a. “Matz”), 2000
  6. 14.

    But even Ruby code can get scary. c l a

    s s R o b o t S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d e n d
  7. 16.

    Well We write programs so we can communicate with computers.

    Also, there are programs everywhere and they all need to be kept up to date. But we write these programs for people to use.
  8. 18.
  9. 19.
  10. 20.

    And when it comes to code we are all standing

    on the shoulders of others.
  11. 21.

    So even if you are writing code by yourself you

    are still going to have to read other peoples' code And more than likely other people will also be reading your code So now more than ever you are writing code for people.
  12. 23.

    Object Oriented Programming You have been using this already in

    Rails, yay! When we write objects the goal is to make sure they only do 1 thing When they try to do more than one thing
  13. 24.

    Then this happens. c l a s s R o

    b o t S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d e n d
  14. 25.

    A programmer named Sandi Metz proposed 4 rules to help

    with this 1. Your class can be no longer than 100 lines of code. 2. Your methods can be no longer than 5 lines of code. 3. You can pass no more than 4 parameters and you can’t just make it one big hash. 4. When a call comes into your Rails controller, you can only instantiate 1 object to do whatever it is that needs to be done.
  15. 27.

    We are just going to use rule #2 to help

    fix the large messy code I showed you earlier. Your methods can be no longer than 5 lines of code.
  16. 28.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d e n d
  17. 29.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t 6 . t i m e s d o @ r o b o t _ x + = i @ r o b o t _ y + = i e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d 6 . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d d e f p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e e n d e n d
  18. 30.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t w a l k _ f o r w a r d _ s t e p s ( 6 ) i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d w a l k _ f o r w a r d _ s t e p s ( 6 ) i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d d e f w a l k _ f o r w a r d _ s t e p s ( n u m b e r _ o f _ s t e p s ) n u m b e r _ o f _ s t e p s . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d e n d d e f p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e e n d e n d
  19. 31.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d d e f c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d e n d d e f w a l k _ f o r w a r d _ s t e p s ( n u m b e r _ o f _ s t e p s ) n u m b e r _ o f _ s t e p s . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d e n d d e f p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e e n d e n d
  20. 32.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) t u r n _ r i g h t w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) t u r n _ r i g h t i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d d e f t u r n _ r i g h t u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d e n d d e f c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d e n d d e f w a l k _ f o r w a r d _ s t e p s ( n u m b e r _ o f _ s t e p s ) n u m b e r _ o f _ s t e p s . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d e n d d e f p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e e n d e n d
  21. 33.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) t u r n _ r i g h t w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) t u r n _ r i g h t y a y _ f o u n d ? ( f o u n d ) e n d d e f y a y _ f o u n d ? ( f o u n d ) i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d d e f t u r n _ r i g h t u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d e n d d e f c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d e n d d e f w a l k _ f o r w a r d _ s t e p s ( n u m b e r _ o f _ s t e p s ) n u m b e r _ o f _ s t e p s . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d e n d d e f p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e e n d e n d
  22. 34.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t s e a r c h ( c u p c a k e _ x , c u p c a k e _ y ) s e a r c h ( c u p c a k e _ x , c u p c a k e _ y ) y a y _ f o u n d ? ( f o u n d ) e n d d e f s e a r c h ( c u p c a k e _ x , c u p c a k e _ y ) w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) t u r n _ r i g h t e n d d e f y a y _ f o u n d ? ( f o u n d ) i f ( f o u n d _ c u p c a k e ) p u t s " F o u n d c u p c a t e a t p o s i t i o n # { @ r o b o t _ x } , # { @ r o b o t _ y } ! " e l s e p u t s ' I g i v e u p . ' e n d e n d d e f t u r n _ r i g h t u n l e s s ( @ f o u n d _ c u p c a k e ) @ r o b o t _ d i r e c t i o n = 9 0 e n d e n d d e f c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) i f ( @ r o b o t _ x = = c u p c a k e _ x & & @ r o b o t _ y = = c u p c a k e _ y ) p u t s ' Y o u f o u n d t h e c u p c a k e ! ' @ f o u n d _ c u p c a k e = t r u e e n d e n d d e f w a l k _ f o r w a r d _ s t e p s ( n u m b e r _ o f _ s t e p s ) n u m b e r _ o f _ s t e p s . t i m e s d o @ r o b o t _ x + = 1 @ r o b o t _ y + = 1 e n d e n d d e f p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t @ r o b o t _ x = 0 @ r o b o t _ y = 0 @ r o b o t _ d i r e c t i o n = 0 @ f o u n d _ c u p c a k e = f a l s e e n d e n d
  23. 35.

    c l a s s R o b o t

    S e a r c h d e f f i n d _ c u p c a k e ( c u p c a k e _ x , c u p c a k e _ y ) p u t _ r o b o t _ a t _ s t a r t i n g _ p o i n t s e a r c h ( c u p c a k e _ x , c u p c a k e _ y ) s e a r c h ( c u p c a k e _ x , c u p c a k e _ y ) y a y _ f o u n d ? ( f o u n d ) e n d d e f s e a r c h ( c u p c a k e _ x , c u p c a k e _ y ) w a l k _ f o r w a r d _ s t e p s ( 6 ) c h e c k _ i f _ f o u n d _ c u p c a k e ? ( c u p c a k e _ x , c u p c a k e _ y ) t u r n _ r i g h t e n d e n d
  24. 36.
  25. 38.

    Talk inspired by Sandi Metz ( ), Katrina Owen (

    ), and Avdi Grimm ( ) You can find me on Twitter , on Github at and these slides on Speaker Deck here @sandimetz @kytrinyx @avdi @ootoovak https://github.com/ootoovak https://speakerdeck.com/ootoovak