• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Slides
 

Slides

on

  • 325 views

 

Statistics

Views

Total Views
325
Views on SlideShare
325
Embed Views
0

Actions

Likes
1
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Slides Slides Presentation Transcript

    • Design by Contract â Perl Âÿ÷åñëàâ Òèõàíîâñêèé (vti) 2012-12-22
    • showmetheco.de
    • ÎÎÏ
    • Moose, Moos, Moo, Mo, M?
    • Èíòåðôåéñû è íàñëåäîâàíèå îò àáñòðàêòíîãî êëàññà
    • 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 ! }
    • 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 ;}
    • Design by Contract
    • Ïðîâåðêà âõîäíûõ è âûõîäíûõ óñëîâèé
    • Ïðîâåðêà âõîäíûõ è âûõîäíûõ óñëîâèéÑëåäîâàíèå ïðèíöèïó ïîäñòàíîâêè Ëèñêîâ (Lâ SOLID) ïðè íàñëåäîâàíèè
    • Ïðîâåðêà âõîäíûõ è âûõîäíûõ óñëîâèéÑëåäîâàíèå ïðèíöèïó ïîäñòàíîâêè Ëèñêîâ (Lâ SOLID) ïðè íàñëåäîâàíèèÄîêóìåíòèðîâàíèå èíòåðôåéñà
    • 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 ;}
    • 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 { . . . }
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòü
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâ
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìî
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìî
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõ
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõÏîíÿòíûå îøèáêè
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõÏîíÿòíûå îøèáêèÊàê ìîæíî ìåíüøå ìàãèè*
    • Íå äîëæíî ñíèæàòü ÷èòàåìîñòüÍå òðåáóåò áîëüøèõ âû÷èñëèòåëüíûõðåñóðñîâÎòêëþ÷àåìîÍàñëåäóåìîÇàïðåùàåò ìåíÿòü êîíòðàêò â äî÷åðíèõêëàññàõÏîíÿòíûå îøèáêèÊàê ìîæíî ìåíüøå ìàãèè*Ïîõîæå íà Perl*
    • Class::Contract
    • 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 { . . . };};
    • Sub::Contract
    • 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 ] ;}
    • Sub::Assert
    • 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 ;
    • Carp::Datum
    • 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 ;}
    • MooseX::Contract
    • 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 ;}
    • Attribute::Contract
    • 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 ) {}
    • DbC è TDD
    • DbC è Defensive programming
    • Êîãäà ïðèìåíÿòü?Áèáëèîòåêè, ìîäóëèÈíòåðôåéñû
    • Êàê íå ïðèìåíÿòü?Íå âàëèäèðîâàòü äàííûåÍå ïðîèçâîäèòü ïîáî÷íûõ ýôôåêòîâ
    • Ñïàñèáî!