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

Else Considered Harmful

Else Considered Harmful

People are bad at logic and at keeping state. “else” clauses are often used to implement complicated logic and require us to backtrack, a.k.a. to keep a lot of state.

Nikolay Bachiyski

November 29, 2014
Tweet

More Decks by Nikolay Bachiyski

Other Decks in Programming

Transcript

  1. I F
    JSCONF Argentina, buenos aires, nov 29th, 2014

    View Slide

  2. J O K E

    View Slide

  3. View Slide

  4. if  (eggs)  {
    }

    View Slide

  5. View Slide

  6. View Slide

  7. W E H AV E A
    P RO B L E M
    O B V I O U S LY

    View Slide

  8. I F ?

    View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  13. View Slide

  14. E L S E ?

    View Slide

  15. I N E N G L I S H B O O K S
    else
    If

    View Slide

  16. I N C O M P U T E R P R O G R A M S
    else
    If

    View Slide

  17. I N C O M P U T E R P R O G R A M S
    else
    If
    2.5 times more else!

    View Slide

  18. W H Y ?

    View Slide

  19. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }

    View Slide

  20. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true

    View Slide

  21. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true
    1: true, 2: true

    View Slide

  22. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true
    1: true, 2: true
    1: true, 2: false

    View Slide

  23. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true
    1: true, 2: true
    1: true, 2: false
    1: true, 2: false, 3: true

    View Slide

  24. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true
    1: true, 2: true
    1: true, 2: false
    1: true, 2: false, 3: true
    1: true, 2: false, 3: false

    View Slide

  25. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true
    1: true, 2: true
    1: true, 2: false
    1: true, 2: false, 3: true
    1: true, 2: false, 3: false
    1: false, 2 & 3 don’t matter

    View Slide

  26. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    1: true
    1: true, 2: true
    1: true, 2: false
    1: true, 2: false, 3: true
    1: true, 2: false, 3: false
    1: false, 2 & 3 don’t matter
    We have to context-switch

    View Slide

  27. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
               if  (⩸⩸⩸4)  {  
                   ⩸⩸        
                   ⩸⩸⩸⩸⩸⩸  
               }  
               ⩸⩸  
               ⩸⩸⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }

    View Slide

  28. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
               if  (⩸⩸⩸4)  {  
                   ⩸⩸        
                   ⩸⩸⩸⩸⩸⩸  
               }  
               ⩸⩸  
               ⩸⩸⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }
    1: true

    View Slide

  29. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
               if  (⩸⩸⩸4)  {  
                   ⩸⩸        
                   ⩸⩸⩸⩸⩸⩸  
               }  
               ⩸⩸  
               ⩸⩸⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }
    1: true
    1 & 2: TRUE

    View Slide

  30. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
               if  (⩸⩸⩸4)  {  
                   ⩸⩸        
                   ⩸⩸⩸⩸⩸⩸  
               }  
               ⩸⩸  
               ⩸⩸⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }
    1: true
    1 & 2: TRUE
    1 & 2 & 3: TRUE

    View Slide

  31. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
               if  (⩸⩸⩸4)  {  
                   ⩸⩸        
                   ⩸⩸⩸⩸⩸⩸  
               }  
               ⩸⩸  
               ⩸⩸⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }
    1: true
    1 & 2: TRUE
    1 & 2 & 3: TRUE
    1 & 2 & 3 & 4: true

    View Slide

  32. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
               if  (⩸⩸⩸4)  {  
                   ⩸⩸        
                   ⩸⩸⩸⩸⩸⩸  
               }  
               ⩸⩸  
               ⩸⩸⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }
    1: true
    1 & 2: TRUE
    1 & 2 & 3: TRUE
    1 & 2 & 3 & 4: true
    No switches, just adding :-)

    View Slide

  33. C H A N G E I S B A D

    View Slide

  34. B U I L D I N G I S G O O D

    View Slide

  35. W H Y ?

    View Slide

  36. we use complex logic:
    • More when we talk to computers
    • less when we talk to people

    View Slide

  37. P E O P L E A R E N OT G R E AT
    AT LO G I C

    View Slide

  38. View Slide

  39. P E O P L E A R E N OT G R E AT
    AT F O L LO W I N G S TAT E

    View Slide

  40. Pepole are graet at reizncinogg patetrns,
    but teirrlbe at foonwillg loigc.

    View Slide

  41. FA C E S
    P E O P L E A R E G O O D AT

    View Slide

  42. View Slide

  43. View Slide

  44. S O U N D S
    P E O P L E A R E G O O D AT

    View Slide

  45. View Slide

  46. https://www.flickr.com/photos/fdecomite/446394032/in/photostream/

    View Slide

  47. M O R E PAT T E R N S

    L E S S “ E L S E ”

    View Slide

  48. N OT N E C E S S A R I LY
    “ D E S I G N PAT T E R N S "

    View Slide

  49. 0 . E M P H A S I Z E E S S E N T I A L C O D E

    View Slide

  50. valid  =  validator(data)  
    if  (valid)  {  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
    }  else  {  
       return  null;  
    }  
    ⩸⩸⩸⩸⩸⩸  
    return  data;

    View Slide

  51. valid  =  validator(data)  
    if  (valid)  {  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸⩸⩸⩸  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
    }  else  {  
       return  null;  
    }  
    ⩸⩸⩸⩸⩸⩸  
    return  data;
    essential code

    View Slide

  52. valid  =  validator(data)  
    if  (!valid)  {  
       return  null;  
    }  
    ⩸⩸⩸⩸⩸⩸  
    ⩸⩸⩸⩸⩸  
    ⩸⩸⩸  
    ⩸⩸⩸⩸⩸⩸  
    ⩸⩸⩸⩸⩸⩸  
    return  data;
    essential code

    View Slide

  53. 1 . P O LYM O R P H I S M

    View Slide

  54. G R E E K MY T H O LO GY ?

    View Slide

  55. fieldAsHtml:  function(field)  {  
       if  ('text'  ===  field.type)  
           return  ;  
    else  if  ('number'  ===  field.type)  
           return  ;  
       else  if  ('textarea'  ===  field.type)  
           return    
       else  
           throw  new  IMissYou(type,  field)  
    }

    View Slide

  56. fieldAsHtml:  function(field)  {  
       if  ('text'  ===  field.type)  
           return  ;  
    else  if  ('number'  ===  field.type)  
           return  ;  
       else  if  ('textarea'  ===  field.type)  
           return    
       else  
           throw  new  IMissYou(type,  field)  
    }
    fieldAsHtml:  function(field)  {  
       renderer  =  renderers[field.type]  ||  throw…  
       return  renderer(field)  
    }

    View Slide

  57. Design pattern:

    strategy™

    View Slide

  58. refactoring:

    Replace Conditional with
    Polymorphism™

    View Slide

  59. P E O P L E ❤️ PAT T E R N S

    View Slide

  60. View Slide

  61. View Slide

  62. if  ('text'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('textarea'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('number'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('airplane  on  wheels'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('scooter  with  squirrel'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('name'  ===  field.type  &&  name.length  >  5  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('name'  ===  field.type  &&  name.length  <  5  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('textarea  on  JavaScript'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('textarea  raised  with  love'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  
       return  ⩸⩸⩸⩸⩸;

    View Slide

  63. if  ('text'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('textarea'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('number'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('airplane  on  wheels'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('scooter  with  squirrel'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('name'  ===  field.type  &&  name.length  >  5)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('name'  ===  field.type  &&  name.length  <  5)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('textarea  on  JavaScript'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  if  ('textarea  raised  with  love'  ===  field.type)  
       return  ⩸⩸⩸⩸⩸;  
    else  
       return  ⩸⩸⩸⩸⩸;

    View Slide

  64. 2 . P U T M O R E F O C U S
    O N A C T I O N

    View Slide

  65. if  (spring)  {  
       if  (weekday)  {  
           school();  
       }  elseif  (saturday)  {  
           if  (raining)  
               tv();  
           else  
               football();  
       }  else  {  
           homework();  
       }  
    }  else  if  (winter)  {  
       hibernate();  
    }

    View Slide

  66. if  (spring)  {  
       if  (weekday)  {  
           school();  
       }  elseif  (saturday)  {  
           if  (raining)  
               tv();  
           else  
               football();  
       }  else  {  
           homework();  
       }  
    }  else  if  (winter)  {  
       hibernate();  
    }
    if  (spring  &&  weekday)  
       school();  
    if  (spring  &&  saturday  &&  !raining)  
       football();  
    if  (spring  &&  saturday  &&  raining)  
       tv();  
    if  (spring  &&  sunday)  
       homework();  
    if  (winter)  
       hibernate();

    View Slide

  67. if  (spring)  {  
       if  (weekday)  {  
           school();  
       }  elseif  (saturday)  {  
           if  (raining)  
               tv();  
           else  
               football();  
       }  else  {  
           homework();  
       }  
    }  else  if  (winter)  {  
       hibernate();  
    }
    if  (spring  &&  weekday)  
       school();  
    if  (spring  &&  saturday  &&  !raining)  
       football();  
    if  (spring  &&  saturday  &&  raining)  
       tv();  
    if  (spring  &&  sunday)  
       homework();  
    if  (winter)  
       hibernate();

    View Slide

  68. 3 . N O E S C A P E

    View Slide

  69. E X T R A C T!

    View Slide

  70. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }

    View Slide

  71. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    if  (⩸⩸⩸⩸⩸1)  {  
       ⦾(⩸,  ⩸⩸⩸)  
    }  else  {  
       ⩸⩸⩸  
    }  
    function  ⦾(⩸,  ⩸⩸⩸)  {  
       ⪍  ⪍  ⪍  
       ⪍  
       ⪍  ⪍  ⪍  ⪍  
    }

    View Slide

  72. if  (⩸⩸⩸⩸⩸1)  {  
       ⩸⩸⩸⩸  
       ⩸⩸⩸⩸  
       if  (⩸⩸2)  {  
           ⩸⩸⩸⩸  
       }  else  {  
           ⩸⩸⩸  
           ⩸⩸⩸⩸⩸⩸  
           ⩸⩸  
           if  (⩸⩸⩸3)  {  
               ⩸⩸  
           }  else  {  
               ⩸⩸        
               ⩸⩸⩸⩸⩸⩸  
           }  
       }  
    }  else  {  
       ⩸⩸⩸  
    }
    var  ⪍  =  ⩸⩸⩸⩸⩸1;  
    if  (⪍)  {  
       ⦾(⩸,  ⩸⩸⩸)  
    }  else  {  
       ⩸⩸⩸  
    }

    View Slide

  73. I N D I R E C T I O N

    ! =

    A B S T R A C T I O N

    View Slide

  74. P RO G R A M M I N G

    +

    U S A B I L I T Y

    = ❤️

    View Slide

  75. View Slide

  76. P E O P L E > CO M P U T E R S

    View Slide

  77. T H A N KS !

    @ N I KO L AY B

    H T T P : // E X T R A P O L AT E . M E /

    View Slide