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

Else Considered Harmful

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

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.

Avatar for Nikolay Bachiyski

Nikolay Bachiyski

November 29, 2014

More Decks by Nikolay Bachiyski

Other Decks in Programming

Transcript

  1. W E H AV E A P RO B L

    E M O B V I O U S LY
  2. I N E N G L I S H B

    O O K S else If
  3. I N C O M P U T E R

    P R O G R A M S else If
  4. 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!
  5. if  (⩸⩸⩸⩸⩸1)  {      ⩸⩸⩸⩸      ⩸⩸⩸⩸  

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

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

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

       if  (⩸⩸2)  {          ⩸⩸⩸⩸      }  else  {          ⩸⩸⩸          ⩸⩸⩸⩸⩸⩸          ⩸⩸          if  (⩸⩸⩸3)  {              ⩸⩸          }  else  {              ⩸⩸                    ⩸⩸⩸⩸⩸⩸          }      }   }  else  {      ⩸⩸⩸   } 1: true 1: true, 2: true 1: true, 2: false
  9. 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
  10. 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
  11. 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
  12. 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
  13. if  (⩸⩸⩸⩸⩸1)  {      ⩸⩸⩸⩸      ⩸⩸⩸⩸  

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

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

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

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

       if  (⩸⩸2)  {          ⩸⩸⩸⩸          ⩸⩸⩸⩸⩸⩸          ⩸⩸          if  (⩸⩸⩸3)  {              ⩸⩸              if  (⩸⩸⩸4)  {                  ⩸⩸                        ⩸⩸⩸⩸⩸⩸              }              ⩸⩸              ⩸⩸⩸⩸⩸⩸⩸⩸          }      }   } 1: true 1 & 2: TRUE 1 & 2 & 3: TRUE 1 & 2 & 3 & 4: true
  18. 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 :-)
  19. we use complex logic: • More when we talk to

    computers • less when we talk to people
  20. P E O P L E A R E N

    OT G R E AT AT LO G I C
  21. 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
  22. FA C E S P E O P L E

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

    L E A R E G O O D AT
  24. M O R E PAT T E R N S

    L E S S “ E L S E ”
  25. N OT N E C E S S A R

    I LY “ D E S I G N PAT T E R N S "
  26. 0 . E M P H A S I Z

    E E S S E N T I A L C O D E
  27. valid  =  validator(data)   if  (valid)  {      

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

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

     null;   }   ⩸⩸⩸⩸⩸⩸   ⩸⩸⩸⩸⩸   ⩸⩸⩸   ⩸⩸⩸⩸⩸⩸   ⩸⩸⩸⩸⩸⩸   return  data; essential code
  30. fieldAsHtml:  function(field)  {      if  ('text'  ===  field.type)  

           return  <input  ⩸⩸⩸⩸⩸⩸  />;   else  if  ('number'  ===  field.type)          return  <Number  ⩸⩸⩸⩸⩸⩸>;      else  if  ('textarea'  ===  field.type)          return  <textarea  ⩸⩸⩸⩸⩸⩸⩸⩸⩸⩸⩸>      else          throw  new  IMissYou(type,  field)   }
  31. fieldAsHtml:  function(field)  {      if  ('text'  ===  field.type)  

           return  <input  ⩸⩸⩸⩸⩸⩸  />;   else  if  ('number'  ===  field.type)          return  <Number  ⩸⩸⩸⩸⩸⩸>;      else  if  ('textarea'  ===  field.type)          return  <textarea  ⩸⩸⩸⩸⩸⩸⩸⩸⩸⩸⩸>      else          throw  new  IMissYou(type,  field)   } fieldAsHtml:  function(field)  {      renderer  =  renderers[field.type]  ||  throw…      return  renderer(field)   }
  32. 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  ⩸⩸⩸⩸⩸;
  33. 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  ⩸⩸⩸⩸⩸;
  34. 2 . P U T M O R E F

    O C U S O N A C T I O N
  35. if  (spring)  {      if  (weekday)  {    

         school();      }  elseif  (saturday)  {          if  (raining)              tv();          else              football();      }  else  {          homework();      }   }  else  if  (winter)  {      hibernate();   }
  36. 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();
  37. 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();
  38. if  (⩸⩸⩸⩸⩸1)  {      ⩸⩸⩸⩸      ⩸⩸⩸⩸  

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

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

       if  (⩸⩸2)  {          ⩸⩸⩸⩸      }  else  {          ⩸⩸⩸          ⩸⩸⩸⩸⩸⩸          ⩸⩸          if  (⩸⩸⩸3)  {              ⩸⩸          }  else  {              ⩸⩸                    ⩸⩸⩸⩸⩸⩸          }      }   }  else  {      ⩸⩸⩸   } var  ⪍  =  ⩸⩸⩸⩸⩸1;   if  (⪍)  {      ⦾(⩸,  ⩸⩸⩸)   }  else  {      ⩸⩸⩸   }
  41. I N D I R E C T I O

    N ! = A B S T R A C T I O N
  42. P RO G R A M M I N G

    + U S A B I L I T Y = ❤️
  43. 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 /