Slides

705 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
705
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Slides

  1. 1. Design by Contract â Perl Âÿ÷åñëàâ Òèõàíîâñêèé (vti) 2012-12-22
  2. 2. showmetheco.de
  3. 3. ÎÎÏ
  4. 4. Moose, Moos, Moo, Mo, M?
  5. 5. Èíòåðôåéñû è íàñëåäîâàíèå îò àáñòðàêòíîãî êëàññà
  6. 6. package A b s t r a c t R e n d e r e r ;sub new { . . . }sub r e n d e r { die I m p l e m e n t me ! }
  7. 7. package TT ;use b a s e A b s t r a c t R e n d e r e r ;sub r e n d e r { my $ s e l f = s h i f t ; my ( $ t e m p l a t e , $ l a y o u t , %v a r s ) = @_; return $ r e n d e r e d ;}package Caml ;use b a s e A b s t r a c t R e n d e r e r ;sub r e n d e r { my $ s e l f = s h i f t ; my ( $ t e m p l a t e , %v a r s ) = @_; return $ r e n d e r e d ;}
  8. 8. Design by Contract
  9. 9. Ïðîâåðêà âõîäíûõ è âûõîäíûõ óñëîâèé
  10. 10. Ïðîâåðêà âõîäíûõ è âûõîäíûõ óñëîâèéÑëåäîâàíèå ïðèíöèïó ïîäñòàíîâêè Ëèñêîâ (Lâ SOLID) ïðè íàñëåäîâàíèè
  11. 11. Ïðîâåðêà âõîäíûõ è âûõîäíûõ óñëîâèéÑëåäîâàíèå ïðèíöèïó ïîäñòàíîâêè Ëèñêîâ (Lâ SOLID) ïðè íàñëåäîâàíèèÄîêóìåíòèðîâàíèå èíòåðôåéñà
  12. 12. package A b s t r a c t R e n d e r e r ;sub new { . . . }# R e q u i r e s ( STRING , %HASH_OF_ANY_VALUES)# E n s u r e s ( STRING )# Throws ( E x c e p t i o n : : TemplateNotFound )sub r e n d e r { my $ s e l f = s h i f t ; my ( $ t e m p l a t e , %v a r s ) = @_; ... return $ r e n d e r e d ;}
  13. 13. package TT ;use b a s e A b s t r a c t R e n d e r e r ;sub r e n d e r { . . . }package Caml ;use b a s e A b s t r a c t R e n d e r e r ;sub r e n d e r { . . . }
  14. 14. Íå äîëæíî ñíèæàòü ÷èòàåìîñòü
  15. 15. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâ
  16. 16. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìî
  17. 17. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìî
  18. 18. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõ
  19. 19. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõÏîíÿòíûå îøèáêè
  20. 20. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõÏîíÿòíûå îøèáêèÊàê ìîæíî ìåíüøå ìàãèè*
  21. 21. Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõÏîíÿòíûå îøèáêèÊàê ìîæíî ìåíüøå ìàãèè*Ïîõîæå íà Perl*
  22. 22. Class::Contract
  23. 23. use C l a s s : : C o n t r a c t ;contract { i n h e r i t s BaseClass ; c t o r new ; method methodname ; pre { . . . }; f a i l m s g E r r o r message ; post { . . . }; f a i l m s g E r r o r message ; impl { . . . };};
  24. 24. Sub::Contract
  25. 25. use Sub : : C o n t r a c t qw ( c o n t r a c t ) ;contract ( surface ) −>i n (& i s _ i n t e g e r , & i s _ i n t e g e r ) −>o u t (& i s _ i n t e g e r ) −>e n a b l e ;sub s u r f a c e { # no need to validate arguments anymore ! # just implement the logic : return $_ [ 0 ] ∗ $_ [ 1 ] ;}
  26. 26. Sub::Assert
  27. 27. assert pre => { p a r a m e t e r l a r g e r t h a n one => $PARAM [ 0 ] > }, post => $VOID o r $RETURN <= $PARAM [ 0 ] , sub => s q u a r e r o o t , c o n t e x t => n o v o i d , a c t i o n => c a r p ;
  28. 28. Carp::Datum
  29. 29. use Carp : : Datum ;sub r o u t i n e { DFEATURE my $f_ , " o p t i o n a l message " ; my ( $a , $b ) = @_; DREQUIRE $a > $b , " a > b" ; $a += 1 ; $b += 1 ; DASSERT $a > $b , " o r d e r i n g a > b p r e s e r v e d " ; my $ r e s u l t = $b − $a ; DENSURE $ r e s u l t < 0 ; return DVAL $ r e s u l t ;}
  30. 30. MooseX::Contract
  31. 31. c o n t r a c t add => a c c e p t s [ . . . t y p e . . . ] => r e t u r n s v o i d , with_context ( p r e => sub { ... }, p o s t => a s s e r t { ... } );sub add { my $ s e l f = s h i f t ; my $ i n c r = s h i f t ; $ s e l f −>{v a l u e } += $ i n c r ; return ;}
  32. 32. Attribute::Contract
  33. 33. package A b s t r a c t R e n d e r e r ;use A t t r i b u t e : : C o n t r a c t ;sub new { . . . }sub r e n d e r : C o n t r a c t R e q u i r e s (VALUE( S t r ) , %ANY) : C o n t r a c t E n s u r e s (VALUE( S t r ) ) : C o n t r a c t T h r o w s ( E x c e p t i o n : : TemplateNotFound ) {}
  34. 34. DbC è TDD
  35. 35. DbC è Defensive programming
  36. 36. Êîãäà ïðèìåíÿòü?Áèáëèîòåêè, ìîäóëèÈíòåðôåéñû
  37. 37. Êàê íå ïðèìåíÿòü?Íå âàëèäèðîâàòü äàííûåÍå ïðîèçâîäèòü ïîáî÷íûõ ýôôåêòîâ
  38. 38. Ñïàñèáî!

×