Affordances in Programming Languages

27204e228cc521c6cafed3c92b95184c?s=47 Randy Coulman
November 17, 2014

Affordances in Programming Languages

From RubyConf 2014.

A good design communicates the intended use of the object. In the physical world, this communication is accomplished by "affordances" as discussed by Donald Norman in "The Psychology of Everyday Things".

Programming languages also have affordances, and they influence the kinds of solutions we develop. The more languages we know, the more we "expand our design space" so that we can come up with better solutions to the problems we face every day.

We will look at a few example problems and show how they can be solved using the affordances provided by different languages.

27204e228cc521c6cafed3c92b95184c?s=128

Randy Coulman

November 17, 2014
Tweet

Transcript

  1. 10.

    c l a s s P o i n t

    d e f i n i t i a l i z e ( x , y ) @ x = x @ y = y e n d e n d P o i n t . n e w ( 3 , 4 )
  2. 13.

    c l a s s P o i n t

    d e f i n i t i a l i z e ( x , y ) @ x = x @ y = y e n d e n d P o i n t . n e w ( 3 , 4 )
  3. 14.

    P o i n t c l a s s

    > > x : a n X y : a Y ^ s e l f n e w i n i t i a l i z e X : a n X y : a Y P o i n t > > i n i t i a l i z e X : a n X y : a Y x : = a n X . y : = a Y P o i n t x : 3 y : 4
  4. 15.

    P o i n t c l a s s

    > > r : r a d i u s t h e t a : a n g l e I n R a d i a n s ^ s e l f x : r a d i u s * a n g l e I n R a d i a n s c o s y : r a d i u s * a n g l e I n R a d i a n s s i n P o i n t r : 5 t h e t a : 0 . 9 2 7 2 9 5
  5. 17.

    c l a s s P o i n t

    d e f s e l f . x y ( x , y ) n e w ( x , y ) e n d d e f s e l f . p o l a r ( r , t h e t a ) x y ( r * M a t h . c o s ( t h e t a ) , r * M a t h . s i n ( t h e t a ) ) e n d p r i v a t e _ c l a s s _ m e t h o d : n e w # . . . r e s t s a m e a s b e f o r e e n d P o i n t . x y ( 3 , 4 ) P o i n t . p o l a r ( 5 , 0 . 9 2 7 2 9 5 )
  6. 19.

    # ( 2 4 6 8 ) d e t

    e c t : [ : e a c h | e a c h o d d ] " U n h a n d l e d e x c e p t i o n : E l e m e n t N o t F o u n d "
  7. 20.

    [ 2 , 4 , 6 , 8 ] .

    f i n d { | n | n . o d d ? } # = > n i l
  8. 21.

    # ( 2 4 6 8 ) d e t

    e c t : [ : e a c h | e a c h o d d ] i f N o n e : [ # n o n e ] " # n o n e "
  9. 23.

    [ 2 , 4 , 6 , 8 ] .

    f i n d ( - > { : n o n e } ) { | n | n . o d d ? } # = > : n o n e
  10. 24.

    Another example: a s s e r t _ d

    i f f e r e n c e in A c t i v e S u p p o r t : : T e s t i n g : : A s s e r t i o n s can take multiple blocks
  11. 25.

    Linguistic Relativity a.k.a. The Sapir-Whorf Hypothesis "[T]he structure of a

    language affects the ways in which its respective speakers conceptualize their world ... or otherwise influences their cognitive processes." -- http://en.wikipedia.org/wiki/Linguistic_relativity
  12. 26.

    When Code Cries Cory Foy at SCNA 2012 http://vimeo.com/53986875 What

    does a language allow you to say? What does a language force you to say?
  13. 27.

    The Power and Philosophy of Ruby Matz at OSCON 2003

    http://www.rubyist.net/~matz/slides/oscon2003/mgp00001.html "Languages are not only tools to communicate, but also tools to think."
  14. 30.

    i f ( ( e r r = S S

    L F r e e B u f f e r ( & h a s h C t x ) ) ! = 0 ) g o t o f a i l ; i f ( ( e r r = R e a d y H a s h ( & S S L H a s h S H A 1 , & h a s h C t x ) ) ! = 0 ) g o t o f a i l ; i f ( ( e r r = S S L H a s h S H A 1 . u p d a t e ( & h a s h C t x , & c l i e n t R a n d o m ) ) ! = g o t o f a i l ; i f ( ( e r r = S S L H a s h S H A 1 . u p d a t e ( & h a s h C t x , & s e r v e r R a n d o m ) ) ! = g o t o f a i l ; i f ( ( e r r = S S L H a s h S H A 1 . u p d a t e ( & h a s h C t x , & s i g n e d P a r a m s ) ) ! = g o t o f a i l ; g o t o f a i l ; i f ( ( e r r = S S L H a s h S H A 1 . f i n a l ( & h a s h C t x , & h a s h O u t ) ) ! = 0 ) g o t o f a i l ; / / . . . f a i l : S S L F r e e B u f f e r ( & s i g n e d H a s h e s ) ; S S L F r e e B u f f e r ( & h a s h C t x ) ; r e t u r n e r r ;
  15. 31.

    # i n c l u d e " s

    u p p o r t . h " # i n c l u d e < i o s t r e a m > v o i d f o o ( ) { R e s o u r c e * r e s o u r c e = a c q u i r e R e s o u r c e ( ) ; b a r ( r e s o u r c e ) ; i f ( b a z ( r e s o u r c e ) ! = 4 2 ) r e t u r n ; s t d : : c o u t < < " C o m p l e t e d s u c c e s s f u l l y ! " < < s t d : : e n d l ; r e l e a s e R e s o u r c e ( r e s o u r c e ) ; }
  16. 32.

    $ . / b r o k e n A

    c q u i r i n g r e s o u r c e C a u g h t e x c e p t i o n : o o p s !
  17. 33.

    # i n c l u d e " s

    u p p o r t . h " # i n c l u d e < i o s t r e a m > v o i d f o o ( ) { R e s o u r c e * r e s o u r c e = a c q u i r e R e s o u r c e ( ) ; t r y { b a r ( r e s o u r c e ) ; i f ( b a z ( r e s o u r c e ) = = 4 2 ) { s t d : : c o u t < < " C o m p l e t e d s u c c e s s f u l l y ! " < < s t d : : e n d l ; } } c a t c h ( s t d : : e x c e p t i o n & e ) { r e l e a s e R e s o u r c e ( r e s o u r c e ) ; t h r o w ; } r e l e a s e R e s o u r c e ( r e s o u r c e ) ; }
  18. 34.

    $ . / w o r d y A c

    q u i r i n g r e s o u r c e R e l e a s i n g r e s o u r c e C a u g h t e x c e p t i o n : o o p s !
  19. 35.

    # i n c l u d e " s

    u p p o r t . h " # i n c l u d e < i o s t r e a m > v o i d f o o ( ) { R e s o u r c e * r e s o u r c e = a c q u i r e R e s o u r c e ( ) ; t r y { b a r ( r e s o u r c e ) ; i f ( b a z ( r e s o u r c e ) = = 4 2 ) { s t d : : c o u t < < " C o m p l e t e d s u c c e s s f u l l y ! " < < s t d : : e n d l ; } } c a t c h ( s t d : : e x c e p t i o n & e ) { r e l e a s e R e s o u r c e ( r e s o u r c e ) ; t h r o w ; } r e l e a s e R e s o u r c e ( r e s o u r c e ) ; }
  20. 36.
  21. 39.

    # i n c l u d e " S

    a f e R e s o u r c e . h " # i n c l u d e " s u p p o r t . h " S a f e R e s o u r c e : : S a f e R e s o u r c e ( ) : r e s o u r c e ( a c q u i r e R e s o u r c e ( ) ) { } S a f e R e s o u r c e : : ~ S a f e R e s o u r c e ( ) { r e l e a s e R e s o u r c e ( r e s o u r c e ) ; } R e s o u r c e * S a f e R e s o u r c e : : g e t ( ) { r e t u r n r e s o u r c e ; }
  22. 40.

    # i n c l u d e " S

    a f e R e s o u r c e . h " # i n c l u d e " s u p p o r t . h " # i n c l u d e < i o s t r e a m > v o i d f o o ( ) { S a f e R e s o u r c e r e s o u r c e ; b a r ( r e s o u r c e . g e t ( ) ) ; i f ( b a z ( r e s o u r c e . g e t ( ) ) ! = 4 2 ) r e t u r n s t d : : c o u t < < " C o m p l e t e d s u c c e s s f u l l y ! " < < s t d : : e n d l ; }
  23. 41.

    $ . / r a i i A c q

    u i r i n g r e s o u r c e R e l e a s i n g r e s o u r c e C a u g h t e x c e p t i o n : o o p s !
  24. 44.

    b e g i n .. e n s u

    r e everywhere? Like t r y .. f i n a l l y in Java
  25. 46.
  26. 47.

    c l a s s S a f e R

    e s o u r c e d e f s e l f . a c q u i r e r e s o u r c e = s e l f . n e w y i e l d r e s o u r c e e n s u r e r e s o u r c e . r e l e a s e e n d d e f i n i t i a l i z e p u t s " A c q u i r i n g r e s o u r c e " @ r e s o u r c e = O b j e c t . n e w e n d d e f r e l e a s e p u t s " R e l e a s i n g r e s o u r c e " @ r e s o u r c e = n i l e n d
  27. 48.

    r e q u i r e _ r e

    l a t i v e " s u p p o r t " r e q u i r e _ r e l a t i v e " s a f e _ r e s o u r c e " d e f f o o S a f e R e s o u r c e . a c q u i r e d o | r e s o u r c e | b a r ( r e s o u r c e ) r e t u r n u n l e s s b a z ( r e s o u r c e ) = = 4 2 p u t s " C o m p l e t e d s u c c e s s f u l l y ! " e n d e n d
  28. 49.

    $ r u b y d r i v e

    r . r b A c q u i r i n g r e s o u r c e R e l e a s i n g r e s o u r c e C a u g h t e x c e p t i o n : o o p s !
  29. 51.

    ( d e f u n d e l e

    t e - e n c l o s e d - t e x t ( ) " D e l e t e t e x t b e t w e e n a n y p a i r o f d e l i m i t e r s . " ( i n t e r a c t i v e ) ( s a v e - e x c u r s i o n ( l e t ( p 1 p 2 ) ( s k i p - c h a r s - b a c k w a r d " ^ ( [ < > " ) ( s e t q p 1 ( p o i n t ) ) ( s k i p - c h a r s - f o r w a r d " ^ ) ] < > " ) ( s e t q p 2 ( p o i n t ) ) ( d e l e t e - r e g i o n p 1 p 2 ) ) ) )
  30. 52.

    ( d e f m a c r o s

    a v e - e x c u r s i o n ( & b o d y ) ` ( l e t ( ( s a v e d - b u f f e r ( c u r r e n t - b u f f e r ) ) ( s a v e d - p o i n t ( p o i n t - m a r k e r ) ) ( s a v e d - m a r k ( c o p y - m a r k e r ( m a r k - m a r k e r ) ) ) ) ( u n w i n d - p r o t e c t ( p r o g n , @ b o d y ) ( s e t - b u f f e r s a v e d - b u f f e r ) ( g o t o - c h a r s a v e d - p o i n t ) ( s e t - m a r k e r ( m a r k - m a r k e r ) s a v e d - m a r k e r ) ) ) )
  31. 54.

    d e f s a v e _ e x

    c u r s i o n s a v e d _ b u f f e r = c u r r e n t _ b u f f e r s a v e d _ p o i n t = p o i n t _ m a r k e r s a v e d _ m a r k = c o p y _ m a r k e r ( m a r k _ m a r k e r y i e l d e n s u r e s e t _ b u f f e r ( s a v e d _ b u f f e r ) g o t o _ c h a r ( s a v e d _ p o i n t ) s e t _ m a r k e r ( m a r k _ m a r k e r , s a v e d _ m a r k e r e n d
  32. 55.

    d e f d e l e t e _

    e n c l o s e d _ t e x t s a v e _ e x c u r s i o n d o s k i p _ c h a r s _ b a c k w a r d ( " ^ ( [ < > " ) p 1 = p o i n t s k i p _ c h a r s _ f o r w a r d ( " ^ ) ] < > " ) p 2 = p o i n t d e l e t e _ r e g i o n ( p 1 , p 2 ) e n d e n d
  33. 58.

    I m a g e R e a d e

    r c l a s s > > f r o m F i l e : a F i l e n a m e | r e a d e r C l a s s i m a g e S t r e a m r e a d e r | i m a g e S t r e a m : = a F i l e n a m e r e a d S t r e a m b i n a r y . [ r e a d e r C l a s s : = s e l f r e a d e r C l a s s F o r : i m a g e S t r e a m . r e a d e r C l a s s i f N i l : [ ^ s e l f e r r o r : ' U n k n o w n i m a g e t y p e : ' , a F i l e n a m e a s S t r i n g ] . r e a d e r : = r e a d e r C l a s s o n : i m a g e S t r e a m ] e n s u r e : [ i m a g e S t r e a m c l o s e ] . ^ r e a d e r
  34. 59.

    I m a g e R e a d e

    r c l a s s > > r e a d e r C l a s s F o r : i m a g e S t r e a m ^ s e l f s u b c l a s s e s d e t e c t : [ : e a c h C l a s s | i m a g e S t r e a m r e s e t . [ e a c h C l a s s c a n R e a d : i m a g e S t r e a m ] e n s u r e : [ i m a g e S t r e a m r e s e t ] ] i f N o n e : [ n i l ]
  35. 61.

    c l a s s I m a g e

    R e a d e r d e f s e l f . r e a d ( f i l e n a m e ) F i l e . o p e n ( f i l e n a m e , " r b " ) d o | i o | r e a d e r _ c l a s s = f i n d _ r e a d e r _ c l a s s ( i o ) r a i s e " U n k n o w n i m a g e t y p e : # { f i l e n a m e } " u n l e s s r e a d e r _ c l a s s r e a d e r _ c l a s s . n e w ( i o ) e n d e n d # . . . e n d
  36. 62.

    c l a s s I m a g e

    R e a d e r # . . . d e f s e l f . f i n d _ r e a d e r _ c l a s s ( i o ) s u b c l a s s e s . f i n d { | r e a d e r | b e g i n i o . r e w i n d r e a d e r . c a n _ r e a d ? ( i o ) e n s u r e i o . r e w i n d e n d } e n d # . . . e n d
  37. 63.

    c l a s s I m a g e

    R e a d e r # . . . d e f s e l f . i n h e r i t e d ( s u b c l a s s ) s u b c l a s s e s < < s u b c l a s s e n d d e f s e l f . s u b c l a s s e s @ s u b c l a s s e s | | = [ ] e n d # . . . e n d
  38. 64.

    c l a s s B M P I m

    a g e R e a d e r < I m a g e R e a d e r d e f s e l f . c a n _ r e a d ? ( i o ) i o . r e a d ( 2 ) = = " B M " e n d d e f r e a d _ i m a g e p u t s " R e a d i n g B M P " e n d e n d
  39. 65.

    c l a s s J P G I m

    a g e R e a d e r < I m a g e R e a d e r d e f s e l f . c a n _ r e a d ? ( i o ) i o . r e a d ( 2 ) = = " \ x F F \ x D 8 " . b e n d d e f r e a d _ i m a g e p u t s " R e a d i n g J P G " e n d e n d
  40. 66.

    c l a s s P N G I m

    a g e R e a d e r < I m a g e R e a d e r d e f s e l f . c a n _ r e a d ? ( i o ) i o . r e a d ( 8 ) = = " \ x 8 9 P N G \ r \ n \ x 1 A \ n " . b e n d d e f r e a d _ i m a g e p u t s " R e a d i n g P N G " e n d e n d
  41. 67.

    r e q u i r e _ r e

    l a t i v e " i m a g e _ r e a d e r s " % w { b m p j p g p n g } . e a c h d o | e x t | I m a g e R e a d e r . r e a d ( " e x a m p l e . # { e x t } " ) e n d
  42. 68.

    $ r u b y s u b c l

    a s s e s . r b R e a d i n g B M P R e a d i n g J P G R e a d i n g P N G
  43. 70.
  44. 79.

    References The Design of Every Day Things - Donald Norman

    Linguistic Relativity - Wikipedia article When Code Cries - Cory Foy at SCNA 2012 The Power and Philosophy of Ruby - Matz at OSCON 2003 Inverting Dependencies - Andre Bernardes Functional-ish Ruby - Kevin Buchanan