A presentation at PyCon PL in in 96-200 Ossa, Poland by Paweł Lewtak
2 n d
h a r d e s t
t h i n g
i n c o m p u t e r
s c i e n c e @ p a w e l _ l e w t a k
D e f i n i t i o n ?
T h e r e
a r e
o n l y
t w o
h a r d
t h i n g s
i n
C o m p u t e r S c i e n c e :
c a c h e
i n v a l i d a t i o n
a n d
n a m i n g t h i n g s . P h i l
K a r l t o n S o u r c e :
h t t p s : / / m a r t i n f o w l e r . c o m / b l i k i / T w o H a r d T h i n g s . h t m l
T h e r e
a r e
2
h a r d
p r o b l e m s
i n
c o m p u t e r s c i e n c e :
c a c h e
i n v a l i d a t i o n ,
n a m i n g
t h i n g s , a n d
1
e r r o r s L e o n
B a m b r i c k S o u r c e :
h t t p s : / / t w i t t e r . c o m / s e c r e t g e e k / s t a t u s / 7 2 6 9 9 9 7 8 6 8
T h e r e
a r e
o n l y
t w o
h a r d
p r o b l e m s
i n d i s t r i b u t e d
s y s t e m s : 2 .
o n c e
d e l i v e r y 1 .
G u a r a n t e e d
o r d e r
o f
m e s s a g e s 2 .
o n c e
d e l i v e r y M a t h i a s
V e r r a e s S o u r c e :
h t t p s : / / t w i t t e r . c o m / m a t h i a s v e r r a e s / s t a t u s / 6 3 2 2 6 0 6 1 8 5 9 9 4 0 3 5 2 0
2
N a m i n g
t h i n g s *
t h i n g s v a r i a b l e s m e t h o d s c l a s s e s m o d u l e s c o m m e n t s i n l i n e
d o c s c o m m i t
m e s s a g e s
Y o u
d o n ' t
c o d e
f o r C P U
Y o u
d o n ' t
c o d e
f o r i n t e r p r e t e r
Y o u
d o n ' t
c o d e
f o r c o m p i l e r
Y o u
c o d e
f o r
p e o p l e
Y o u
c o d e
f o r
o t h e r d e v e l o p e r s
Y o u
c o d e
f o r
y o u r f u t u r e
s e l f
D o n ' t
c o d e , w r o t e
p r o s e S o u r c e :
C K O N K Z L m M w k
S o u r c e :
e c o n o m i c s /
A l w a y s
c o d e
a s
i f
t h e
g u y
w h o
e n d s
u p m a i n t a i n i n g
y o u r
c o d e
w i l l
b e
a
v i o l e n t p s y c h o p a t h
w h o
k n o w s
w h e r e
y o u
l i v e . C o d e
f o r
r e a d a b i l i t y . J o h n
F .
W o o d s S o u r c e :
h t t p s : / / g r o u p s . g o o g l e . c o m / f o r u m /
! m s g / c o m p . l a n g . c + + / r Y C O 5 y n 4 l X w / o I T t S k Z O t o U J
C o m p r e h e n s i o n
~ 7 0 %
d e f
a ( b ) :
c
=
s o r t e d ( b )
d
=
l e n ( b )
i f
d
%
2
1 :
r e t u r n
c [ ( d
1 )
/
2 ]
e l s e :
r e t u r n
( c [ d / 2
1 ]
c [ d / 2 ] )
/
2
d e f
m e d i a n ( p o o l ) :
c o p y
=
s o r t e d ( p o o l )
s i z e
=
l e n ( c o p y )
i f
s i z e
%
2
1 :
r e t u r n
c o p y [ ( s i z e
1 )
/
2 ]
e l s e :
r e t u r n
( c o p y [ s i z e / 2
1 ]
c o p y [ s i z e / 2 ] )
/
2
d o c u m e n t i n g c o d e
C o d e
w r i t t e n
b y s o m e b o d y
e l s e
P r o g r a m m i n g
i s
m a p p i n g f r o m
p r o b l e m
d o m a i n v i a
i n t e r m e d i a t e
d o m a i n i n t o
p r o g r a m m i n g
d o m a i n
D D D
F T W
W o r s t
v a r i a b l e
n a m e
d a t a S o u r c e :
h t t p : / / a r c h i v e . o r e i l l y . c o m / p u b / p o s t / t h e _ w o r l d s _ t w o _ w o r s t _ v a r i a b l e . h t m l
S e c o n d
w o r s t
n a m e ?
d a t a 2
t o t a l
=
p r i c e
q t y t o t a l 2
=
t o t a l
d i s c o u n t t o t a l 2
=
t o t a l
t a x r a t e t o t a l 3
=
p u r c h a s e _ o r d e r _ v a l u e
a v a i l a b l e _ c r e d i t i f
t o t a l 2
<
t o t a l 3 :
p r i n t
( " Y o u
c a n ' t
a f f o r d
t h i s
o r d e r . " )
o r d e r _ t o t a l
=
p r i c e
q t y p a y a b l e _ t o t a l
=
o r d e r _ t o t a l
d i s c o u n t p a y a b l e _ t o t a l
=
p a y a b l e _ t o t a l
t a x r a t e a v a i l a b l e _ f u n d s
=
p u r c h a s e _ o r d e r _ v a l u e
a v a i l b l e _ c r e d i t i f
p a y a b l e _ t o t a l
<
a v a i l a b l e _ f u n d s :
p r i n t
( " Y o u
c a n ' t
a f f o r d
t h i s
o r d e r . " )
o n e
s e t s
o u t
t o
w r i t e
l e g a c y
c o d e R a c h e l
W i l l m e r S o u r c e :
0 9
B r o k e n
w i n d o w t h e o r y
C o d e
w i l l
d e c a y
D e s i g n
p a t t e r n s
M i s a p p l i e d
J a v a
d e s i g n
p a t t e r n s a r e
t h e
r o o t
o f
a l l A b s t r a c t W o r d F a c t o r y F a c t o r y ( " e v i l " ) H N
c o m m e n t S o u r c e :
h t t p s : / / t w i t t e r . c o m / t m m x / s t a t u s / 8 6 5 3 0 8 6 7 8 9 0 3 2 6 7 3 2 8
N a m i n g
c o n v e n t i o n s
T L ; D R C a m e l C a s e C l a s s m e t h o d N a m e s o m e V a r i a b l e C A P I T A L _ C O N S T A N T
s y n t a x
<
s e m a n t h i c s
C o m m o n
i s s u e s
P s e u d o
g e t t e r
g e t _ d a t a ( ) w i t h
e x t r a
o p e r a t i o n s
i n s i d e
g e t _ c r e a t e _ o b j e c t ( )
f e t c h f i n d l o o k u p c r e a t e c a l c u l a t e
N o t
r e a l l y
a
b o o l e a n
i s _ a c t i v e ( ) d e f
i s _ a c t i v e ( ) :
i f
c o n d :
r e t u r n
' f a l s e '
r e t u r n
' t r u e '
i s _ v a l i d ( ) d e f
i s _ v a l i d ( ) :
i f
i n p u t _ i s _ v a l i d :
r e t u r n
T r u e
P l u r a l
/
s i n g u l a r n a m e s
d e f
g e t _ p e r s o n ( ) :
r e t u r n
[ ' J o h n
D o e ' ,
' J a n e
D o e ' ] d e f
g e t _ e m p l o y e r s ( ) :
r e t u r n
' J o h n
D o e '
M i s l e a d i n g
d o c s
d e f
g e t _ l o w e s t _ p r i c e ( u s e r ) :
p a s s
d e f
g e t _ l o w e s t _ p r i c e ( u s e r ) :
" " " A c t u a l l y
i t
r e t u r n s
t h e
h i g h e s t
p r i c e . " " "
p a s s
M o r e
t h a n
o n e r e s p o n s i b i l i t y
A b b r e v i a t i o n s p o s m o d a b s a u t h
S y n o n y m s
< T h a t T h i n g
M a n a g e r U s e r M a n a g e r S t r i n g M a n a g e r P r o d u c t M a n a g e r e t c .
A l t e r n a t i v e s B u i l d e r W r i t e r A d a p t e r F a c t o r y H a n d l e r P r o v i d e r C o n v e r t e r
M a g i c
n u m b e r s
i m p o r t
r e q u e s t s r e s p o n s e
=
r e q u e s t s . g e t ( ' h t t p s : / / p l . p y c o n . o r g / ' ) i f
r e s p o n s e . s t a t u s _ c o d e
2 0 0 :
p r i n t
( " I t
w o r k s ! " ) e l i f
r e s p o n s e . s t a t u s _ c o d e
4 1 8 :
p r i n t
( " U n e x p e c t e d
t e a p o t ! " )
i m p o r t
r e q u e s t s r e s p o n s e
=
r e q u e s t s . g e t ( ' h t t p s : / / p l . p y c o n . o r g / ' ) i f
r e s p o n s e . s t a t u s _ c o d e
r e q u e s t s . c o d e s . o k :
p r i n t
( " I t
w o r k s ! " ) e l i f
r e s p o n s e . s t a t u s _ c o d e
r e q u e s t s . c o d e s . t e a p o t :
p r i n t
( " U n e x p e c t e d
t e a p o t ! " )
U s e l e s s
c o m m e n t s
d e f
g e t _ d a t a ( ) :
" " "
R e t u r n s
t h e
d a t a .
" " "
p a s s d e f
g e t _ m a x _ i d _ f r o m _ d b ( ) :
" " "
R e t u r n
m a x i m u m
I D
v a l u e
f r o m
t h e
d a t a b a s e . " " "
p a s s
E x p l a i n
w h y , n o t
w h a t
o r
h o w
C o m m i t
m e s s a g e s
S o u r c e :
h t t p s : / / t w i t t e r . c o m / A r i e l B H / s t a t u s / 8 9 5 6 0 1 4 2 9 2 5 5 2 4 5 8 2 4
D o n ' t
d o
i t
l i k e
t h i s h t t p : / / w h a t t h e c o m m i t . c o m h t t p : / / w w w . c o m m i t l o g s f r o m l a s t n i g h t . c o m /
B a d
n a m e : D o e s
m o r e
t h a t
w h a t
i s
s a y s S a y s
m o r e
t h a n
w h a t
i t
d o e s D o e s
t h e
o p p o s i t e C o n t a i n s
m o r e
t h a n
w h a t
i t
s a y s S a y s
m o r e
t h a n
w h a t
i t
c o n t a i n s C o n t a i n s
t h e
o p p o s i t e
G o o d
p r a c t i c e s
S p e c i f i c
n a m e s N o
g e n e r i c s
S h o r t
n a m e s
D o
n o t
u s e
n e g a t i o n
i s _ n o t _ e n a b l e d ( )
i s _ d i s a b l e d ( )
C o n s i s t e n t
n a m e s
C o d e
&
d o c s
S i n g l e
r e s p o n s i b i l i t y
D o m a i n
t e r m s
T h i n k
a b o u t
i t
A S C I I
o n l y
H u n g a r i a n
n o t a t i o n h o s t L i s t ,
h o s t S e t
=
h o s t s ,
v a l i d H o s t s v a l u e S t r i n g
=
f i r s t N a m e ,
l o w e r c a s e d S K U i n t N u m b e r
=
a c c o u n t N u m b e r
T e s t s !
C o m m i t
m e s s a g e
G o o d
c o m m i t
m e s s a g e S p e e d s
u p
r e v i e w
p r o c e s s H e l p s
w r i t e
r e l e a s e
n o t e s H e l p s
f u t u r e
m a i n t a i n e r s
S h o r t
( 5 0
c h a r s
o r
l e s s )
s u m m a r y
o f
c h a n g e s M o r e
d e t a i l e d
e x p l a n a t o r y
t e x t ,
i f
n e c e s s a r y .
W r a p
i t
t o
a b o u t
7 2 c h a r a c t e r s
o r
s o .
I n
s o m e
c o n t e x t s ,
t h e
f i r s t
l i n e
i s
t r e a t e d
a s
t h e s u b j e c t
o f
a n
e m a i l
a n d
t h e
r e s t
o f
t h e
t e x t
a s
t h e
b o d y .
T h e
b l a n k l i n e
s e p a r a t i n g
t h e
s u m m a r y
f r o m
t h e
b o d y
i s
c r i t i c a l
( u n l e s s
y o u
o m i t t h e
b o d y
e n t i r e l y ) ;
t o o l s
l i k e
r e b a s e
c a n
g e t
c o n f u s e d
i f
y o u
r u n
t h e t w o
t o g e t h e r . F u r t h e r
p a r a g r a p h s
c o m e
a f t e r
b l a n k
l i n e s .
B u l l e t
p o i n t s
a r e
o k a y ,
t o o
T y p i c a l l y
a
h y p h e n
o r
a s t e r i s k
i s
u s e d
f o r
t h e
b u l l e t ,
p r e c e d e d
b y
a
s i n g l e
s p a c e ,
w i t h
b l a n k
l i n e s
i n
b e t w e e n ,
b u t
c o n v e n t i o n s
v a r y
h e r e S o u r c e :
S o u r c e :
2 . h t m l
H o w ?
A g r e e
o n
s t a n d a r d s
B o y
S c o u t
R u l e
P r a c t i c e
I m p r o v e
v o c a b u l a r y
R e f a c t o r
C o d e
r e v i e w s S h o r t ,
b i t e
s i z e ,
s i n g l e
l o g i c a l
c h a n g e
C o d e
o w n e r s h i p
C o m m i t
m e s s a g e s
R e s e a r c h
L A s . p d f
T h a n k
y o u ! @ p a w e l _ l e w t a k
Q u e s t i o n s ? @ p a w e l _ l e w t a k