A presentation at Confitura in in Warsaw, 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
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
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
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
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
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
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 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
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
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 : / / 2 0 1 7 . c o n f i t u r a . p l / ' ) 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 : / / 2 0 1 7 . c o n f i t u r a . p 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 . 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
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 ( )
C o n s i s t e n t
n a m e 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 !
H o w ?
A g r e e
o n
s t a n d a r d s
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
T e a m
l e a d e r ' s
r o l e
e c o n o m i c s /
T h a n k
y o u ! @ p a w e l _ l e w t a k
Porozmawiajmy o trudnych tematach w IT. Najczęściej przy tej okazji wspomina się o czyszczeniu pamięci podręcznej i nazywaniu rzeczy. W ramach tej prezentacji chciałbym się skupić na tej drugiej. Na podstawie własnego doświadczenia pokażę dobre i złe praktyki stosowane przez programistów. Na podstawie dobrych i złych nazw poszukamy wspólnego mianownika w obu przypadkach i zastanowimy się co sprawia, że nazwa funkcji/zmiennej/klasy/modułu jest dobra, zła lub brzydka. Postaram się odpowiedzieć na pytanie czy wystarczą wystarczająco dobre nazwy czy powinniśmy być w tej kwestii perfekcjonistami. Wyniesiona z prezentacji wiedza powinna pomóc rozpoznać dobrą/złą nazwę w czasie kolejnego code review.