A presentation at PyCon PL in in 96-200 Ossa, Poland by Paweł Lewtak
O b j e c t
C a l i s t h e n i c s 9
s t e p s
t o
b e t t e r
O O
c o d e
A g e n d a L e a r n
h o w
t o
m a k e
o u r
c o d e
m o r e : r e a d a b l e r e u s a b l e t e s t a b l e m a i n t a i n a b l e
R a i s e
y o u
h a n d
i f
y o u
k n o w o n e
o f
t h e
f o l l o w i n g : D R Y K I S S S O L I D Y A G N I Z e n
o f
P y t h o n
C a l i s t h e n i c s
C a l
•
i s
•
t h e n
•
i c s
/ ˌ k a l ə s ˈ T H e n i k s /
" C a l i s t h e n i c s
a r e
e x e r c i s e s
c o n s i s t i n g
o f
a v a r i e t y
o f
g r o s s
m o t o r
m o v e m e n t s ;
o f t e n r h y t h m i c a l
a n d
g e n e r a l l y
w i t h o u t
e q u i p m e n t o r
a p p a r a t u s . " W i k i p e d i a
O b j e c t
C a l i s t h e n i c s J e f f
B a y
W r i t t e n
f o r
J a v a
W h y
b o t h e r ?
C o d e
i s
r e a d
m o r e t h a n
i t ' s
w r i t t e n
R u l e
1 O n l y
o n e
l e v e l
o f i n d e n t a t i o n
p e r
m e t h o d
c l a s s
B o a r d ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ,
d a t a ) :
L e v e l
0
s e l f . b u f
=
" "
f o r
i
i n
r a n g e ( 1 0 ) :
L e v e l
1
f o r
j
i n
r a n g e ( 1 0 ) :
L e v e l
2
s e l f . b u f
=
d a t a [ i ] [ j ]
c l a s s
B o a r d ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ,
d a t a ) :
s e l f . b u f
=
" "
s e l f . c o l l e c t _ r o w s ( d a t a )
d e f
c o l l e c t _ r o w s ( s e l f ,
d a t a ) :
f o r
i
i n
r a n g e ( 1 0 ) :
s e l f . c o l l e c t _ r o w ( d a t a [ i ] )
d e f
c o l l e c t _ r o w ( s e l f ,
r o w ) :
f o r
j
i n
r a n g e ( 1 0 ) :
s e l f . b u f
=
r o w [ j ]
B e n e f i t s S i n g l e
r e s p o n s i b i l i t y B e t t e r
n a m i n g S h o r t e r
m e t h o d s R e u s a b l e
m e t h o d s
R u l e
2 D o
n o t
u s e
e l s e
k e y w o r d
i f
o p t i o n s . g e t C a t e g o r i e s ( )
i s
N o n e :
. . . e l i f
l e n ( o p t i o n s . g e t C a t e g o r i e s ( ) )
1 :
. . . e l i f
S P E C I A L _ C A T E G O R Y
i n
o p t i o n s . g e t C a t e g o r i e s ( ) :
. . . e l i f
o p t i o n s . g e t C a t e g o r i e s ( )
a n d
o p t i o n s . g e t Q u e r y ( ) :
. . . e l i f
o p t i o n s . g e t C o n t e n t T y p e ( ) :
. . .
d e f
l o g i n
( s e l f ,
r e q u e s t ) :
i f
r e q u e s t . u s e r . i s _ a u t h e n t i c a t e d ( ) :
r e t u r n
r e d i r e c t ( " h o m e p a g e " )
e l s e :
m e s s a g e s . a d d _ m e s s a g e ( r e q u e s t ,
m e s s a g e s . I N F O ,
' B a d
c r e d e n t i a l s ' )
r e t u r n
r e d i r e c t ( " l o g i n " )
d e f
l o g i n
( s e l f ,
r e q u e s t ) :
i f
r e q u e s t . u s e r . i s _ a u t h e n t i c a t e d ( ) :
r e t u r n
r e d i r e c t ( " h o m e p a g e " )
m e s s a g e s . a d d _ m e s s a g e ( r e q u e s t ,
m e s s a g e s . I N F O ,
' B a d
c r e d e n t i a l s ' )
r e t u r n
r e d i r e c t ( " l o g i n " )
E x t r a c t
c o d e
D e f a u l t
v a l u e
P o l y m o r p h i s m
S t r a t e g y
p a t t e r n
S t a t e
p a t t e r n
B e n e f i t s A v o i d s
c o d e
d u p l i c a t i o n L o w e r
c o m p l e x i t y R e a d a b i l i t y
R u l e
3 W r a p
p r i m i t i v e
t y p e s
i f
i t h a s
b e h a v i o u r
V a l u e
O b j e c t
i n
D D D
c l a s s
V a l i d a t o r ( o b j e c t ) :
d e f
c h e c k _ d a t e ( s e l f ,
y e a r ,
m o n t h ,
d a y ) :
p a s s
1 0 t h
o f
D e c e m b e r
o r
1 2 t h
o f
O c t o b e r ? v a l i d a t o r
=
V a l i d a t o r ( ) v a l i d a t o r . c h e c k _ d a t e ( 2 0 1 6 ,
1 0 ,
1 2 )
c l a s s
V a l i d a t o r ( o b j e c t ) :
d e f
c h e c k _ d a t e ( y e a r :
Y e a r ,
m o n t h :
M o n t h ,
d a y :
D a y )
b o o l :
p a s s
F u n c t i o n
c a l l
l e a v e s
n o
d o u b t . v a l i d a t o r . c h e c k _ d a t e ( Y e a r ( 2 0 1 6 ) ,
M o n t h ( 1 0 ) ,
D a y ( 1 2 ) )
B e n e f i t s E n c a p s u l a t i o n T y p e
h i n t i n g A t t r a c t s
s i m i l a r
b e h a v i o u r
R u l e
4 O n l y
o n e
d o t
p e r
l i n e
O K :
F l u e n t
i n t e r f a c e
c l a s s
P o e m ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ,
c o n t e n t ) :
s e l f . c o n t e n t
=
c o n t e n t
d e f
i n d e n t ( s e l f ,
s p a c e s ) :
s e l f . c o n t e n t
=
"
"
s p a c e s
s e l f . c o n t e n t
r e t u r n
s e l f
d e f
s u f f i x ( s e l f ,
c o n t e n t ) :
s e l f . c o n t e n t
=
s e l f . c o n t e n t
"
"
c o n t e n t
r e t u r n
s e l f P o e m ( " R o a d
N o t
T r a v e l l e d " ) . i n d e n t ( 4 ) \
. s u f f i x ( " R o b e r t
F r o s t " ) . c o n t e n t
N o t
O K :
g e t t e r
c h a i n
c l a s s
C a r t S e r v i c e ( o b j e c t ) :
d e f
g e t _ t o k e n ( s e l f ) :
t o k e n
=
s e l f . g e t _ s e r v i c e ( ' a u t h ' ) \
. a u t h _ u s e r ( ' u s e r ' ,
' p a s s w o r d ' ) \
. g e t _ r e s u l t ( ) \
. g e t _ t o k e n ( )
r e t u r n
t o k e n
1 .
W h a t
i f
N o n e
i s
r e t u r n e d
i n s t e a d
o f
o b j e c t ?
2 .
H o w
a b o u t
e x c e p t i o n s
h a n d l i n g ?
c l a s s
L o c a t i o n ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . c u r r e n t
=
P i e c e ( ) c l a s s
P i e c e ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . r e p r e s e n t a t i o n
=
"
" c l a s s
B o a r d ( o b j e c t ) :
d e f
b o a r d _ r e p r e s e n t a t i o n ( s e l f ,
b o a r d ) :
b u f
=
' '
f o r
f i e l d
i n
b o a r d :
b u f
=
f i e l d . c u r r e n t . r e p r e s e n t a t i o n
r e t u r n
b u f
c l a s s
L o c a t i o n ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . c u r r e n t
=
P i e c e ( )
d e f
a d d _ t o ( s e l f ,
b u f f e r ) :
r e t u r n
s e l f . c u r r e n t . a d d _ t o ( b u f f e r ) c l a s s
P i e c e ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . r e p r e s e n t a t i o n
=
"
"
d e f
a d d _ t o ( s e l f ,
b u f f e r ) :
r e t u r n
b u f f e r
s e l f . r e p r e s e n t a t i o n c l a s s
B o a r d ( o b j e c t ) :
d e f
b o a r d _ r e p r e s e n t a t i o n ( s e l f ,
b o a r d ) :
b u f
=
' '
f o r
f i e l d
i n
b o a r d :
b u f
=
f i e l d . a d d _ t o ( b u f )
r e t u r n
b u f
B e n e f i t s E n c a p s u l a t i o n D e m e t e r ' s
l a w O p e n / C l o s e d
P r i n c i p l e
R u l e
5 D o
n o t
a b b r e v i a t e
W h y
a b b r e v i a t e ?
T o o
m a n y
r e s p o n s i b i l i t i e s
N a m e
t o o
l o n g ?
S p l i t
&
e x t r a c t
D u p l i c a t e d
c o d e ?
R e f a c t o r !
B e n e f i t s C l e a r
i n t e n t i o n s I n d i c a t e
u n d e r l y i n g
p r o b l e m s
R u l e
6 K e e p
y o u r
c l a s s e s
s m a l l
W h a t
i s
s m a l l
2 0
l i n e s
p e r
m e t h o d 5 0
l i n e s
p e r
c l a s s 1 0
c l a s s e s
p e r
m o d u l e
B e n e f i t s S i n g l e
R e s p o n s i b i l i t y S m a l l e r
m o d u l e s
R u l e
7 N o
m o r e
t h a n
2
i n s t a n c e
v a r i a b l e
p e r
c l a s s
C l a s s
s h o u l d
h a n d l e
s i n g l e v a r i a b l e
s t a t e
I n
s o m e
c a s e s
i t
m i g h t
b e t w o
v a r i a b l e s
c l a s s
C a r t S e r v i c e ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . l o g g e r
=
L o g g e r ( )
s e l f . c a r t
=
C a r t C o l l e c t i o n ( )
s e l f . t r a n s l a t i o n S e r v i c e
=
T r a n s l a t i o n S e r v i c e ( )
s e l f . a u t h S e r v i c e
=
A u t h S e r v i c e ( )
s e l f . u s e r S e r v i c e
=
U s e r S e r v i c e ( )
B e n e f i t s H i g h
c o h e s i o n E n c a p s u l a t i o n F e w e r
d e p e n d e n c i e s
R u l e
8 F i r s t
c l a s s
c o l l e c t i o n s
c o l l e c t i o n s
m o d u l e
B e n e f i t s S i n g l e
R e s p o n s i b i l i t y
R u l e
9 D o
n o t
u s e
s e t t e r s / g e t t e r s
A c c e s s o r s
a r e
f i n e
D o n ' t
m a k e
d e c i s i o n s o u t s i d e
o f
c l a s s
L e t
c l a s s
d o
i t ' s
j o b
T e l l ,
d o n ' t
a s k
c l a s s
G a m e ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . s c o r e
=
0
d e f
s e t _ s c o r e ( s e l f ,
s c o r e ) :
s e l f . s c o r e
=
s c o r e
d e f
g e t _ s c o r e ( s e l f ) :
r e t u r n
s e l f . s c o r e
U s a g e E N E M Y _ D E S T R O Y E D _ S C O R E
=
1 0 g a m e
=
G a m e ( ) g a m e . s e t _ s c o r e ( g a m e . g e t _ s c o r e ( )
E N E M Y _ D E S T R O Y E D _ S C O R E )
c l a s s
G a m e ( o b j e c t ) :
d e f
_ _ i n i t _ _ ( s e l f ) :
s e l f . s c o r e
=
0
d e f
a d d _ s c o r e ( s e l f ,
s c o r e ) :
s e l f . s c o r e
=
s c o r e
U s a g e E N E M Y _ D E S T R O Y E D _ S C O R E
=
1 0 g a m e
=
G a m e ( ) g a m e . a d d _ s c o r e ( E N E M Y _ D E S T R O Y E D _ S C O R E )
B e n e f i t s O p e n / C l o s e d
P r i n c i p l e
C a t c h
' e m
a l l !
C a t c h
' e m
a l l !
1 .
O n l y
o n e
l e v e l
o f
i n d e n t a t i o n
p e r
m e t h o d , 2 .
D o
n o t
u s e
e l s e
k e y w o r d , 3 .
W r a p
p r i m i t i v e
t y p e s
i f
i t
h a s
b e h a v i o r , 4 .
O n l y
o n e
d o t
p e r
l i n e , 5 .
D o n ’ t
a b b r e v i a t e , 6 .
K e e p
y o u r
e n t i t i e s
s m a l l , 7 .
N o
m o r e
t h a n
t w o
i n s t a n c e
v a r i a b l e
p e r
c l a s s , 8 .
F i r s t
C l a s s
C o l l e c t i o n s , 9 .
D o
n o t
u s e
a c c e s s o r s
C a t c h
' e m
a l l !
1 .
O n l y
o n e
l e v e l
o f
i n d e n t a t i o n
p e r
m e t h o d , 2 .
D o
n o t
u s e
e l s e
k e y w o r d , 3 .
W r a p
p r i m i t i v e
t y p e s
i f
i t
h a s
b e h a v i o r , 4 .
O n l y
o n e
d o t
p e r
l i n e , 5 .
D o n ’ t
a b b r e v i a t e , 6 .
K e e p
y o u r
e n t i t i e s
s m a l l , 7 .
N o
m o r e
t h a n
t w o
i n s t a n c e
v a r i a b l e
p e r
c l a s s , 8 .
F i r s t
C l a s s
C o l l e c t i o n s , 9 .
D o
n o t
u s e
a c c e s s o r s 1 0 .
? ? ? 1 1 .
P R O F I T !
H o m e w o r k
C r e a t e
n e w
p r o j e c t
u p
t o 1 0 0 0
l i n e s
l o n g
A p p l y
p r e s e n t e d
r u l e s
a s s t r i c t l y
a s
p o s s i b l e
D r a w
y o u r
o w n
c o n c u l s i o n s
C u s t o m i z e
t h e s e
r u l e s
F i n a l
t h o u g h t s
T h e s e
a r e
n o t
b e s t
p r a c t i c e s
T h e s e
a r e
j u s t
g u i d e l i n e s
U s e
w i t h
c a u t i o n !
Q u e s t i o n s ?
T h a n k
y o u !
Prezentacja jest o tym jak przy zastosowaniu kilku zasad pisać krótszy, bardziej czytelny i dużo prostszy w testowaniu kod. Pokażę czym jest Object Calisthenics, jak może pomóc w programowaniu na co dzień i czemu to bardziej wskazówki niż sztywne reguły.