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

Programming by Hyperbole

Programming by Hyperbole

Lightning talk from the Booster 2013 conference.

Einar W. Høst

March 13, 2013

Transcript

67 slides
  1. Take  a  small  task  from  work   Take  a  small

     but   real  problem  you   encounter  at  work.  
  2. Normal   You  see  a  string  in  your   program

     and  you  think...   NORMAL  
  3. “Hey,  that’s  a  string.”   HYPERBOLE   You  see  a

     string  in  your   program  and  you  think...  
  4. “Hey,  that’s  a  string.”   HYPERBOLE   “I  could  put

     a  programming   language  in  there.”  
  5. 5  +  2  *  3   Actual  Business  Case  

    In  my  HTML  form,  how  can  I  validate     that  a  DateTime  value  is  between     two  other  DateTime  values?  
  6. 5  +  2  *  3   ASP.NET  MVC  Model  Valida@on

      public  class  User   {          [  Required  ]          public  string  UserName  {  get;  set;  }          [  Required  ]          [  MinLength(12)  ]          public  string  Password  {  get;  set;  }   }  
  7. 5  +  2  *  3   Data  Annota@on  Validator  Adributes

      >  Required   >  Range   >  RegularExpression   >  MinLength   >  MaxLength   >  ...  
  8. 5  +  2  *  3   Data  Annota@on  Validator  Adributes

      >  Required   >  Range   >  RegularExpression   >  MinLength   >  MaxLength   >  ...  
  9. 5  +  2  *  3   Data  Annota@on  Validator  Adributes

      >  Required   >  Range   >  RegularExpression   >  MinLength   >  MaxLength   >  ...  
  10. 5  +  2  *  3   Actual  Business  Solu@on  

    Custom  valida@on  to  solve     a  very  specific  problem.  
  11. 5  +  2  *  3   Hyperbolic  Developer  Ques@on  

    Is  there  a  generic  solu@on?  
  12. 5  +  2  *  3   Hyperbolic  Developer  Ques@on  

    What  would  be  the  ul@mate  approach?  
  13. 5  +  2  *  3   Hyperbolic  Developer  Ques@on  

    Can  this  be  solved  once  and  for  all?  
  14. 5  +  2  *  3   Hyperbolic  Developer  Ques@on  

    What  is  the  final  word  on  valida@on?  
  15. 5  +  2  *  3   Custom  Valida@on  Adribute  

    [  Custom(........)  ]   Severely  limited  
  16. 5  +  2  *  3   Custom  Valida@on  Adribute  

    [  Custom(“foo”)  ]   ...but  strings  are  OK  
  17. 5  +  2  *  3   Custom  Valida@on  Adribute  

    [  Custom(“foo”)  ]   ...strings  are  OK?  
  18. 5  +  2  *  3   Custom  Valida@on  Adribute  

    [  Custom(“foo”)  ]   I  could  put  a     programming     language  in  there!  
  19. 5  +  2  *  3   My  Hyperbolic  Idea  

    Create  a  rule  DSL  for  expressing   arbitrary  constraints  on  property     values  in  a  LISP-­‐like  syntax.  
  20. 5  +  2  *  3   Why?   Do.  Or

     do  not.   There  is  no  why.  
  21. 5  +  2  *  3   Why?   If  it

     fits,  I  sits  
  22. 5  +  2  *  3   Why?   >  Custom

     Valida@on   >  S-­‐expressions   >  Expression  Trees   >  Func@onal  JavaScript   >  Code  Genera@on   >  Code  Interpreta@on   Learning  opportunity  
  23. (+  5  (*  2  3))   [  Mkay(“(and  (>  A)

     (<  B))”)  ]   Introducing  Mkay   One  valida@on  adribute  to  rule  them  all  
  24. (+  5  (*  2  3))   Mkay  expressions   >

     Constants   >  Property  access   >  Logical  operators   >  Comparison  operators   >  Simple  func@ons  
  25. (+  5  (*  2  3))   Example  #1   [

     Mkay(“>=  (max  A  B)”)  ]  
  26. (+  5  (*  2  3))   Example  #2   [

     Mkay(“==  (len  .)  5”)  ]  
  27. (+  5  (*  2  3))   Example  #3   [

     Mkay(“and  (>  “31.01.2006”)  (<=  (now))”)  ]  
  28. Rule  AST   Cons  Cells   .NET     Expression

     Trees   JavaScript   JSON   Server-­‐side  valida@on   Client-­‐side  valida@on   Rule  DSL   String   .NET     Func@on   JavaScript   Func@on  
  29. (+  5  (*  2  3))   Mkay  Rule  DSL  

    (>  .  (+  A  B  C))  
  30. (+  5  (*  2  3))   Abstract  Syntax  Tree  

    symbol   >   symbol   +   symbol   .   nil   symbol   A   symbol   B   symbol   C   nil   (>  .  (+  A  B  C))  
  31. (+  5  (*  2  3))   Expression  Tree   Member

      .   Member   B   Member   A   Member   C   Binary   +   Binary   >   Binary   +   (>  .  (+  A  B  C))  
  32. (+  5  (*  2  3))   JSON   {  

       "type":  "call",      "value":  ">",      "operands":  [          {              "type":  "property",              "value":  "X"          },          {              "type":  "call",              "value":  "+",              "operands":  [                  {                      "type":  "property",                      "value":  "A"                  },                  {                      "type":  "property",                      "value":  "B"                  },                  {                      "type":  "property",                      "value":  "C"                  }              ]          }      ]   }   (>  .  (+  A  B  C))  
  33. (+  5  (*  2  3))   Composed  Valida@on  Func@on  

    function()  {      return  greater-­‐than-­‐function      (          read-­‐property-­‐function("."),          plus-­‐function(              plus-­‐function(                  plus-­‐function(                      0,                        read-­‐property-­‐function("C")),                  read-­‐property-­‐function("B")),              read-­‐property-­‐function("A"))      );   }   Pseudo-­‐JavaScript  
  34. 5  +  2  *  3   Demo   public  class

     Person     {            [  Mkay("(<  (len  .)  5)",  ErrorMessage  =  "That's  too  long,  my  friend.")  ]            public  string  Name  {  get;  set;  }            [  Mkay("(>=  .  \"31.01.1900\")")  ]            public  DateTime  BirthDate  {  get;  set;  }            [  Mkay("(<=  .  (now))")  ]            public  DateTime  DeathDate  {  get;  set;  }            [  Mkay("(and  (>=  .  BirthDate)  (<=  .  DeathDate))")  ]            public  DateTime  LastSeen  {  get;  set;  }     }  
  35. “Hey,  that’s  a  string.”   HYPERBOLE   You  see  a

     string  in  your   program  and  you  think...  
  36. “Hey,  that’s  a  string.”   HYPERBOLE   “I  could  put

     a  programming   language  in  there.”  

More Decks by Einar W. Høst

Other Decks in Programming