Date post: | 02-Jun-2018 |
Category: |
Documents |
Upload: | milankrstic |
View: | 216 times |
Download: | 0 times |
of 88
8/11/2019 cleancode-110520031623-phpapp02
1/88
CLEAN CODE
Arturo Herrero
8/11/2019 cleancode-110520031623-phpapp02
2/88
Two reasons
2. You want to be a better programmer1. You are aprogrammer
8/11/2019 cleancode-110520031623-phpapp02
3/88
The Boy Scout Rule
Uncle BobRobert C. Martin
8/11/2019 cleancode-110520031623-phpapp02
4/88
Elegance
Bjarne Stroustrup
I lie m! co"e to beelegant an" e##icient
Clean co"e "oes onet$ing well
8/11/2019 cleancode-110520031623-phpapp02
5/88
Simple, direct, prose
Clean co"e is simplean" "irect
Clean co"e rea"s liewell%written prose
&ra"! Booc$
8/11/2019 cleancode-110520031623-phpapp02
6/88
Literate
Clean co"e can berea"
Clean co"e s$oul" beliterate
'a(e )$omas
8/11/2019 cleancode-110520031623-phpapp02
7/88
Care
Clean co"e alwa!s
loos lie it was writtenb! someone w$o cares
Mic$ael *eat$ers
8/11/2019 cleancode-110520031623-phpapp02
8/88
Small, epressi!e, simple
Re"uce" "uplication+
Ron ,e##ries
$ig$ e-pressi(eness+an" earl! buil"ing o#+simple abstractions
8/11/2019 cleancode-110520031623-phpapp02
9/88
"hat you epected
woring on clean co"e
ar" Cunning$am
w$en eac$ routine !ourea"s turns out to beprett! muc$ w$at !ou
You now !ou are
e-pecte"
8/11/2019 cleancode-110520031623-phpapp02
10/88
8/11/2019 cleancode-110520031623-phpapp02
11/88
#eaning$ul Names
public List getThem() { List list1 = new ArrayList(); !r (int[] " # theList) i ("[$] == %)
list1&a''("); return list1;
Use Intention%Re(ealing /ames
8/11/2019 cleancode-110520031623-phpapp02
12/88
#eaning$ul Names
public List get*lagge'ells() { List lagge'ells = new ArrayList(); !r (ell cell # game+!ar') i (cell&is*lagge'())
lagge'ells&a''(cell); return lagge'ells;
Use Intention%Re(ealing /ames
8/11/2019 cleancode-110520031623-phpapp02
13/88
8/11/2019 cleancode-110520031623-phpapp02
14/88
#eaning$ul Names
class ta2cr'1$/ { pri-ate ate genym'hms; pri-ate ate m!'ym'hms; pri-ate inal 3tring ps45int = 61$/6;
78 &&& 87;
Use 0ronounceable /ames
8/11/2019 cleancode-110520031623-phpapp02
15/88
#eaning$ul Names
class ust!mer { pri-ate ate generati!nTimestamp; pri-ate ate m!'iicati!nTimestamp;; pri-ate inal 3tring rec!r'9' = 61$/6;
78 &&& 87;
Use 0ronounceable /ames
8/11/2019 cleancode-110520031623-phpapp02
16/88
#eaning$ul Names
!r (int : = $; : < %; :00) {
s 0= (t[:] 8 %) 7 ;
Use Searc$able /ames
8/11/2019 cleancode-110520031623-phpapp02
17/88
#eaning$ul Names
int realayser9'ealay = %;c!nst int ,2?@A3@B2@BB? = ;int sum = $;!r (int : = $; : < CDE+B2@,*@TA3?3; :00) {
int realTasFays = tasFBstimate[:] 8realayser9'ealay; int realTasFeeFs = (real'ays 7 ,2?@A3@B2@BB?); sum 0= realTasFeeFs;
Use Searc$able /ames
8/11/2019 cleancode-110520031623-phpapp02
18/88
#eaning$ul Names
public class art { pri-ate 3tring m@'sc; 77 The te"tual 'escripti!n -!i' setCame(3tring name) { m@'sc = name;
Member 0re#i-es A(oi" enco"ings
Hungarian /otation A(oi" enco"ingsh!neCumber ph!ne3tring;77 name n!t change' when type change'G
8/11/2019 cleancode-110520031623-phpapp02
19/88
#eaning$ul Names
public class art { 3tring 'escripti!n; -!i' setescripti!n(3tring 'escripti!n) { this&'escripti!n = 'escripti!n;
Member 0re#i-es A(oi" enco"ings
Hungarian /otation A(oi" enco"ingsh!neCumber ph!ne;
8/11/2019 cleancode-110520031623-phpapp02
20/88
#eaning$ul Names
!r (a = $; a < 1$; a00) !r (b = $; b < 1$; b00)
A(oi" Mental Mapping
Class /amesEanager. r!cess!r. ata. 9n!
8/11/2019 cleancode-110520031623-phpapp02
21/88
#eaning$ul Names
!r (i = $; i < 1$; i00) !r (: = $; : < 1$; :00)
A(oi" Mental Mapping
Class /amesust!mer. iFiage. Acc!unt. A''ressarser77 a class name sh!ul' n!t be a -erb
8/11/2019 cleancode-110520031623-phpapp02
22/88
#eaning$ul Names
p!stayment. 'eleteage. sa-e77 meth!'s sh!ul' ha-e -erb !r -erb phrase names
string name = empl!yee&getCame();cust!mer&setCame(6miFe6);i (paychecF&is!ste'())&&&
!mple" ulcrum!int = !mple"&r!m2ealCumber(/&$);
77 is generally better than!mple" ulcrum!int = new !mple"(/&$);
Met$o" /ames
8/11/2019 cleancode-110520031623-phpapp02
23/88
#eaning$ul Names
etch. retrie-e. get 77 as e5ui-alent meth!'s
c!ntr!ller. manager. 'ri-er 77 c!nusing
0ic 3ne or" per Concept
'on4t 0un77 a-!i' using the same w!r' !r tw! purp!ses
8/11/2019 cleancode-110520031623-phpapp02
24/88
#eaning$ul Names
Acc!untHisit!r. I!bJueue77 pe!ple wh! rea' y!ur c!'e will be pr!grammers
Use Solution 'omain /ames
A"" Meaning#ul Conte-tirstCame. lastCame. street. city. state. 4ipc!'e77 a better s!luti!na''r*irstCame. a''rLastCame. a''r3tate
77 a better s!luti!nlass A''ress
8/11/2019 cleancode-110520031623-phpapp02
25/88
#eaning$ul Names
A''ress77 is a ine name !r a class
Acc!untA''ress. ust!merA''ress
77 are ine names !r instances ! the class A''ress77 but c!ul' be p!!r names !r classes
EA a''resses. p!rt a''resses. eb a''resses77 a better s!luti!n
!stalA''ress. EA. D29
'on4t A"" &ratuitous Conte-t
8/11/2019 cleancode-110520031623-phpapp02
26/88
%unctions
77 rules ! uncti!ns#77 1& sh!ul' be small77 /& sh!ul' be smaller than that
77 < 1$ characters per line77 < /$ lines
Small5
'o 3ne )$ing77 *DCT9,C3 3K,DL , ,CB TK9C& TKB 3K,DL , 9T BLL&77 TKB 3K,DL , 9T ,CL&
8/11/2019 cleancode-110520031623-phpapp02
27/88
%unctions
77 high le-el ! abstracti!n getKtml()
77 interme'iate le-el ! abstracti!n
3tring pageathCame = atharser&ren'er(pageath);
77 remarFably l!w le-el &appen'(6Mn6)
3ne 6e(el o# Abstraction per *unction
Rea"ing Co"e #rom )op to Bottom77 the 3tep'!wn 2ule
8/11/2019 cleancode-110520031623-phpapp02
28/88
%unctions
class Bmpl!yee&&& int payAm!unt() { switch (getType()) { case Bmpl!yeeType&BC9CBB2#
return @m!nthly3alary; case Bmpl!yeeType&3ALB3EAC# return @m!nthly3alary 0 @c!mmissi!n; case Bmpl!yeeType&EACAB2# return @m!nthly3alary 0 @b!nus; 'eault#
thr!w new B"cepti!n(69nc!rrect Bmpl!yee6);
Switc$ Statements
8/11/2019 cleancode-110520031623-phpapp02
29/88
%unctions
class Bmpl!yeeType&&& abstract int payAm!unt(Bmpl!yee emp);
class 3alesman&&&
int payAm!unt(Bmpl!yee emp) { return emp&getE!nthly3alary() 0 emp&get!mmissi!n(
class Eanager&&& int payAm!unt(Bmpl!yee emp) {
return emp&getE!nthly3alary() 0 emp&get+!nus();
Switc$ Statements
8/11/2019 cleancode-110520031623-phpapp02
30/88
%unctions
testableKtml => inclu'e3etupAn'Tear'!wnages
inclu'e3etupAn'Tear'!wnages. inclu'e3etupages.inclu'e3uite3etupage. inclu'e3etupage
77 what happene' t!inclu'eTear'!wnages. inclu'e3uiteTear'!wnage.inclu'eTear'!wnage
Use 'escripti(e /ames
*unction Arguments77 the i'eal number ! arguments !r a uncti!n is 4er!
8/11/2019 cleancode-110520031623-phpapp02
31/88
%unctions
77 i a uncti!n is g!ing t! trans!rm its input argument.77 the trans!rmati!n sh!ul' appear as the return -alue
3tring+uer trans!rm(3tring+uer in)
77 is better than-!i' trans!rm(3tring+uer !ut)
Common Mona"ic *orms
*lag Argumentsren'er(true)
8/11/2019 cleancode-110520031623-phpapp02
32/88
%unctions
77 asFing a 5uesti!n ab!ut that argumentb!!lean ileB"ists(NEy*ileO)
77 !perating !n that argument. trans!rming an' returning
9nput3tream ile,pen(NEy*ileO)
77 e-ent. use the argument t! alter the state ! the syste-!i' passw!r'Attempt*aile'Ctimes(int attempts)
Common Mona"ic *orms
*lag Argumentsren'er*!r3uite()ren'er*!r3ingleTest()
8/11/2019 cleancode-110520031623-phpapp02
33/88
%unctions
write*iel'(name)77 is easier t! un'erstan' thanwrite*iel'(!utput3tream. name)
77 perectly reas!nable!int p = new !int($.$)
77 pr!blematicassertB5uals(e"pecte'. actual)
'!a"ic *unctions
)ria"sassertB5uals(message. e"pecte'. actual)
8/11/2019 cleancode-110520031623-phpapp02
34/88
%unctions
ircle maFeircle('!uble ". '!uble y. '!uble ra'ius);ircle maFeircle(!int center. '!uble ra'ius);
Argument 3bjects
7erbs an" 8e!wor"swrite(name)write*iel'(name)
assertB5uals(e"pecte'. actual)
assertB"pecte'B5ualsActual(e"pecte'. actual)
8/11/2019 cleancode-110520031623-phpapp02
35/88
%unctions
77 '! s!mething !r answer s!mething. but n!t b!thpublic b!!lean set(3tring attribute. 3tring -alue);
setAn'hecF9B"ists
i (attributeB"ists(6username6)) { setAttribute(6username6. 6uncleb!b6); &&&
Ha(e /o Si"e 9##ects
8/11/2019 cleancode-110520031623-phpapp02
36/88
%unctions
77 'uplicati!n may be the r!!t ! all e-il in s!tware'on4t Repeat Yoursel# 'RY
Structure" 0rogramming77 B'sger i:FstraPs rules77 !ne entry77 !ne e"it
77 uncti!ns small
77 !ccasi!nal multiple return. breaF. !r c!ntinue statemen77 can s!metimes e-en be m!re e"pressi-e i:FstraPs rules
8/11/2019 cleancode-110520031623-phpapp02
37/88
Comments
77 '!nPt c!mment ba' c!'e. rewrite itGComments 'o /ot Mae Up #or Ba" Co"e
9-plain Yoursel# in Co"e77 hecF t! see i the empl!yee is eligible !r ullbeneitsi ((empl!yee&lags Q K,D2L@*LA) QQ (empl!yee&age > R))
i (empl!yee&isBligible*!r*ull+eneits())
8/11/2019 cleancode-110520031623-phpapp02
38/88
Comments &good'
77 !pyright () /$11 by ,s!c!& All rights reser-e'&77 2elease' un'er the terms ! the CD eneral ublicLicense 77 -ersi!n / !r later&
6egal Comments
In#ormati(e Comments77 2eturns an instance ! the 2esp!n'er being teste'&pr!tecte' abstract 2esp!n'er resp!n'er9nstance();77 renaming the uncti!n# resp!n'er+eingTeste'
77 !rmat matche' FF#mm#ss BBB. EEE ''. yyyyattern timeEatcher = attern&c!mpile(6MM'8#MM'8#MM'8 MMw8. MMw8 MM'8. MM'86);
8/11/2019 cleancode-110520031623-phpapp02
39/88
Comments &good'
77This is !ur best attempt t! get a race c!n'iti!n77by creating large number ! threa's&!r (int i = $; i < /$$$; i00) { i'get+uil'erThrea' wi'get+uil'erThrea' =
new i'get+uil'erThrea'(wi'get+uil'er. te"t. ail*lag) Threa' threa' = new Threa'(wi'get+uil'erThrea'); threa'&start();
9-planation o# Intent
Clari#icationassertTrue(a&c!mpareT!(b) == S1); 77 a < bassertTrue(b&c!mpareT!(a) == 1); 77 b > a
C t
8/11/2019 cleancode-110520031623-phpapp02
40/88
Comments &good'
public static 3impleate*!rmat maFe3tan'ar'Kttpate*!rmat( 773impleate*!rmat is n!t threa' sae. 77s! we nee' t! create each instance in'epen'ently& 3impleate*!rmat ' = new 3impleate*!rmat(6'' EE yyyy
'&setTime!ne(Time!ne&getTime!ne(6ET6)); return ';
arning o# Conse:uences
)3'3 Comments77T,,SE'E these are n!t nee'e'77 e e"pect this t! g! away when we '! the checF!ut m!'el
C t
8/11/2019 cleancode-110520031623-phpapp02
41/88
Comments &good'
3tring list9tem!ntent = match&gr!up()&trim();77 the trim is real imp!rtant& 9t rem!-es the starting77 spaces that c!ul' cause the item t! be rec!gni4e'77 as an!ther list&
new List9temi'get(this. list9tem!ntent. this&le-el 0 1);return buil'List(te"t&substring(match&en'()));
Ampli#ication
,a(a"ocs in 0ublic A0Is77 there is n!thing 5uite s! helpul an' satisying77 as a wellS'escribe' public A9
C t
8/11/2019 cleancode-110520031623-phpapp02
42/88
Comments &(ad'
try { 3tring pr!pertiesath = pr!pertiesL!cati!n 0 676 0 2,B2T9B3@*9LB; *ile9nput3tream pr!perties3tream =
new *ile9nput3tream(pr!pertiesath); l!a'e'r!perties&l!a'(pr!perties3tream);catch(9,B"cepti!n e) { 77 C! pr!perties iles means all 'eaults are l!a'e'
Mumbling
C t
8/11/2019 cleancode-110520031623-phpapp02
43/88
Comments &(ad'
77 Dtility meth!' that returns when this&cl!se' is true&77 Thr!ws an e"cepti!n i the time!ut is reache'&public synchr!ni4e' -!i' wait*!rl!se (inal l!ng time!utEillis) thr!ws B"cepti!
{ i(Gcl!se') { wait(time!utEillis); i(Gcl!se') thr!w new B"cepti!n(6E!cF2esp!nse3en'er c!ul' n!t be cl!se'6);
Re"un"ant Comments
C t
8/11/2019 cleancode-110520031623-phpapp02
44/88
Comments &(ad'
7888 The pr!cess!r 'elay !r this c!mp!nent&87pr!tecte' int bacFgr!un'r!cess!relay = S1;
7888 The liecycle e-ent supp!rt !r this c!mp!nent&87pr!tecte' Liecycle3upp!rt liecycle = new Liecycle3upp!rt(thi7888 The c!ntainer e-ent listeners !r this !ntainer&87pr!tecte' ArrayList listeners = new ArrayList();
Re"un"ant Comments
C t
8/11/2019 cleancode-110520031623-phpapp02
45/88
Comments &(ad'
7888 Uparam title The title ! the 8 Uparam auth!r The auth!r ! the 8 Uparam tracFs The number ! tracFs !n the
8 Uparam 'urati!n9nEinutes The 'urati!n ! the in minu87public -!i' a''(3tring title. 3tring auth!r. int tracFs. int 'urati!n9nEinutes) { c' = new (); c'&title = title; c'&auth!r = auth!r; c'&tracFs = tracFs; c'&'urati!n = 'urati!n9nEinutes;
Man"ate" Comments
C t
8/11/2019 cleancode-110520031623-phpapp02
46/88
Comments &(ad'
8 hanges (r!m 11S,ctS/$$1)8 SSSSSSSSSSSSSSSSSSSSSSSSSS8 11S,ctS/$$1 # 2eS!rganise' the class an' m!-e' it t! new8 pacFage c!m&:reinery&'ate ();
8 $SC!-S/$$1 # A''e' a getescripti!n() meth!'. an'8 eliminate' C!tableate class ();8 1/SC!-S/$$1 # 9+ re5uires setescripti!n() meth!'. n!w8 that C!tableate class is g!ne (); hang8 getre-i!usay,eeF().8 get*!ll!wingay,eeF() an'8 getCearestay,eeF() t! c!rrect bugs ()8 $SecS/$$1 # *i"e' bug in 3prea'sheetate class ();8 /VSEayS/$$/ # E!-e' the m!nth c!nstants int! a separate8 interace (E!nth!nstants) ();
,ournal Comments
Comments
8/11/2019 cleancode-110520031623-phpapp02
47/88
Comments &(ad'
7888 eault c!nstruct!r&87pr!tecte' Annualate2ule() {
788 The 'ay ! the m!nth& 87pri-ate int 'ay,E!nth;
7888 2eturns the 'ay ! the m!nth&8 Ureturn the 'ay ! the m!nth&87public int getay,E!nth() { return 'ay,E!nth;
/oise Comments
Comments
8/11/2019 cleancode-110520031623-phpapp02
48/88
Comments &(ad'
788 The name& 87pri-ate 3tring name;
788 The -ersi!n& 87
pri-ate 3tring -ersi!n;
788 The licenceCame& 87pri-ate 3tring licenceCame;
788 The -ersi!n& 87pri-ate 3tring in!;
Scar! /oise
Comments
8/11/2019 cleancode-110520031623-phpapp02
49/88
Comments &(ad'
77 '!es the m!'ule r!m the gl!bal list
8/11/2019 cleancode-110520031623-phpapp02
50/88
Comments &(ad'
77 Acti!ns 77777777777777777777777777777777770osition Marers
Closing Brace Commentswhile ((line = in&rea'Line()) G= null) { line!unt00; char!unt 0= line&length(); 3tring w!r's[] = line&split(6MM6); w!r'!unt 0= w!r's&length;
77while
Comments
8/11/2019 cleancode-110520031623-phpapp02
51/88
Comments &(ad'
78 A''e' by 2icF 87Attributions an" B!lines
Commente"%3ut Co"e9nput3tream2esp!nse resp!nse = new 9nput3tream2esp!nse();resp!nse&set+!'y(!rmatter&get2esult3tream().!rmatter&get+yte!unt());77 9nput3tream results3tream = !rmatter&get2esult3tream()77 3tream2ea'er rea'er = new 3tream2ea'er(results3tream);
77 resp!nse&set!ntent(rea'er&rea'(!rmatter&get+yte!unt(
Comments
8/11/2019 cleancode-110520031623-phpapp02
52/88
Comments &(ad'
7888 TasF t! run it tests&8 This tasF runs itnesse tests an' publishes the results8
8 8 Dsage#8 Qlt;tasF'e name=Q5u!t;e"ecuteSitnesseStestsQ5u!t;8 classname=Q5u!t;itnesse&ant&B"ecute*itnesseTestsTasFQ58 classpathre=Q5u!t;classpathQ5u!t; 7Qgt;8 ,28 Qlt;tasF'e classpathre=Q5u!t;classpathQ5u!t;8 res!urce=Q5u!t;tasFs&pr!pertiesQ5u!t; 7Qgt;8 8 Qlt;e"ecuteSitnesseStests
H)M6 Comments
Comments &( d'
8/11/2019 cleancode-110520031623-phpapp02
53/88
Comments &(ad'
7888 !rt !n which itnesse w!ul' run& eaults t! X$X/
8/11/2019 cleancode-110520031623-phpapp02
54/88
Comments &(ad'
78 2* /$% S Eultipurp!se 9nternet Eail B"tensi!ns (E9EB) art ,ne# *!rmat ! 9nternet Eessage +!'ies secti!n R&X& +aseR% !ntentSTranserSBnc!'ing
The enc!'ing pr!cess represents /%Sbit gr!ups ! input b as !utput strings ! % enc!'e' characters& r!cee'ing r let t! right. a /%Sbit input gr!up is !rme' by c!ncatenating XSbit input gr!ups& These /% bits are then treate' as % c!ncatenate' RSbit gr!ups. each ! which is translate' int! a single 'igit the baseR% alphabet& hen enc!'ing a bit stream -ia the baseR% enc!'ing. the stream must be presume' t! be !r'ere' with the m!stS signiicantSbit irst&87
)oo Muc$ In#ormation
Comments &( d'
8/11/2019 cleancode-110520031623-phpapp02
55/88
Comments &(ad'
788 start with an array that is big en!ugh t! h!l' all the8 pi"els (plus ilter bytes). an' an e"tra /$$ bytes !r8 hea'er in!
87this&png+ytes = new byte[((this&wi'th 0 1) 8 this&height 8 0 /$$];
Inob(ious Connection
*unction Hea"ers77 sh!rt uncti!ns '!nPt nee' much 'escripti!n
Comments &( d'
8/11/2019 cleancode-110520031623-phpapp02
56/88
Comments &(ad'
77 e"tra !rmality ! the :a-a'!c c!mments,a(a"ocs in /onpublic Co"e
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
57/88
%ormatting
77 c!mmunicati!n)$e 0urpose o# *ormatting
)$e /ewspaper Metap$or77 highSle-el S> 'etails
7ertical 3penness Between Concepts77 each blanF line is a -isual cue77 that i'entiies a new an' separate c!ncept
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
58/88
%ormatting
77 -ertical 'ensity implies cl!se ass!ciati!n
7888 The class name ! the rep!rter listener87pri-ate 3tring m@classCame;
7888 The pr!perties ! the rep!rter listener87pri-ate m@pr!perties = new ArrayList();
7ertical 'ensit!
8/11/2019 cleancode-110520031623-phpapp02
59/88
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
60/88
%ormatting
pri-ate -!i' measureLine(3tring line) { line!unt00; int line3i4e = line&length(); t!talhars 0= line3i4e; linei'thKist!gram&a''Line(line3i4e. line!unt); rec!r'i'estLine(line3i4e);
public static '!uble r!!t/(int a. int b. int c) { '!uble 'eterminant = 'eterminant(a. b. c); return (Sb S Eath&s5rt('eterminant)) 7 (/8a);
Hori;ontal 3penness an" 'ensit!
8/11/2019 cleancode-110520031623-phpapp02
61/88
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
62/88
%ormatting
public class *itCesseB"pe'iter implements 2esp!nse3en'er{ pri-ate 3!cFet s!cFet; pri-ate 9nput3tream input; pri-ate ,utput3tream !utput; pri-ate 2e5uest re5uest; pri-ate 2esp!nse resp!nse; pri-ate *itCesse!nte"t c!nte"t; pr!tecte' l!ng re5uestarsingTimeLimit; pri-ate l!ng re5uestr!gress; pri-ate l!ng re5uestarsingea'line;
pri-ate b!!lean hasBrr!r; &&&
Hori;ontal Alignment
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
63/88
%ormatting
public class !mmenti'get e"ten's Te"ti'get { public static inal 3tring 2BBY = 6Z[ZMrMn]8(W#(W#MrMn)\Mn\Mr)W6; public !mmenti'get(3tring te"t) { super(te"t); public 3tring ren'er() thr!ws B"cepti!n { return 66;
Breaing In"entation
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
64/88
%ormatting
public class !mmenti'get e"ten's Te"ti'get { public static inal 3tring 2BBY = 6Z[ZMrMn]8(W#(W#MrMn)\Mn\Mr)W6;
public !mmenti'get(3tring te"t) { super(te"t);
public 3tring ren'er() thr!ws B"cepti!n { return 66;
Breaing In"entation
%ormatting
8/11/2019 cleancode-110520031623-phpapp02
65/88
%ormatting
77 e-ery pr!grammer has his !wn a-!rite !rmatting rules77 but i he w!rFs in a team77 then the team rules
)eam Rules
O()ects and Data Structures
8/11/2019 cleancode-110520031623-phpapp02
66/88
) a a a
!ncrete !int
public class !int { public '!uble "; public '!uble y;
'ata Abstraction
O()ects and Data Structures
8/11/2019 cleancode-110520031623-phpapp02
67/88
)
Abstract !int
public interace !int { '!uble getY(); '!uble get(); -!i' setartesian('!uble ". '!uble y); '!uble get2(); '!uble getTheta(); -!i' set!lar('!uble r. '!uble theta);
'ata Abstraction
O()ects and Data Structures
8/11/2019 cleancode-110520031623-phpapp02
68/88
)
!ncrete Hehicle
public interace Hehicle { '!uble get*uelTanFapacity9nall!ns(); '!uble getall!ns,as!line();
'ata Abstraction
O()ects and Data Structures
8/11/2019 cleancode-110520031623-phpapp02
69/88
)
Abstract Hehicle
public interace Hehicle { '!uble getercent*uel2emaining();
'ata Abstraction
O()ects and Data Structures
8/11/2019 cleancode-110520031623-phpapp02
70/88
)
77 !b:ects hi'e their 'ata behin' abstracti!ns an'77 e"p!se uncti!ns that !perate !n that 'ata
77 'ata structure e"p!se their 'ata an'77 ha-e n! meaningul uncti!ns
'ata
8/11/2019 cleancode-110520031623-phpapp02
71/88
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
72/88
g
i ('eleteage(page) == B@,?) { i (registry&'elete2eerence(page&name) == B@,?) { i (c!nig?eys&'elete?ey(page&name&maFe?ey()) == B@,?) l!gger&l!g(6page 'elete'6); else {
l!gger&l!g(6c!nig?ey n!t 'elete'6); else { l!gger&l!g(6'elete2eerence r!m registry aile'6); else {
l!gger&l!g(6'elete aile'6); return B@B22,2;
0re#er 9-ceptions to Returning 9rror Co"es
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
73/88
g
try { 'eleteage(page); registry&'elete2eerence(page&name); c!nig?eys&'elete?ey(page&name&maFe?ey());
catch (B"cepti!n e) { l!gger&l!g(e&getEessage());
0re#er 9-ceptions to Returning 9rror Co"es
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
74/88
public -!i' 'elete(age page) { try { 'eleteageAn'All2eerences(page); catch (B"cepti!n e) { l!gBrr!r(e);
pri-ate -!i' 'eleteageAn'All2eerences(age page) thr!ws B"cepti 'eleteage(page); registry&'elete2eerence(page&name); c!nig?eys&'elete?ey(page&name&maFe?ey());
pri-ate -!i' l!gBrr!r(B"cepti!n e) { l!gger&l!g(e&getEessage());
9-tract )r!
8/11/2019 cleancode-110520031623-phpapp02
75/88
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
76/88
try { EealB"penses e"penses = e"pense2ep!rtA, &getEeals(empl!yee&get9()); m@t!tal 0= e"penses&getT!tal(); catch(EealB"pensesC!t*!un' e) {
m@t!tal 0= getEealeriem();
'e#ine t$e /ormal *low
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
77/88
EealB"penses e"penses = e"pense2ep!rtA, &getEeals(empl!yee&get9());m@t!tal 0= e"penses&getT!tal();
'e#ine t$e /ormal *low
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
78/88
List empl!yees = getBmpl!yees();i (empl!yees G= null) { !r(Bmpl!yee e # empl!yees) { t!talay 0= e&getay();
'on4t Return /ull
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
79/88
List empl!yees = getBmpl!yees(); !r(Bmpl!yee e # empl!yees) { t!talay 0= e&getay();
public List getBmpl!yees() { i( && there are n! empl!yees && ) return !llecti!ns&emptyList();
'on4t Return /ull
Error *andling
8/11/2019 cleancode-110520031623-phpapp02
80/88
public '!uble "r!:ecti!n(!int p1. !int p/) { return (p/&" p1&") 8 1&;
public '!uble "r!:ecti!n(!int p1. !int p/) { i (p1 == null \\ p/ == null) { thr!w 9n-ali'ArgumentB"cepti!n (69n-ali' argument !r Eetricsalculat!r&"r!:ecti!n return (p/&" p1&") 8 1&;
'on4t 0ass /ull
+nit Tests
8/11/2019 cleancode-110520031623-phpapp02
81/88
77 irst law77 y!u may n!t write pr!'ucti!n c!'e until77 y!u ha-e written a ailing unit test
77 sec!n' law
77 y!u may n!t write m!re ! a unit test77 than is suicient t! ail. an' n!t c!mpiling is ailin
77 thir' law77 y!u may n!t write m!re pr!'ucti!n c!'e77 than is suicient t! pass the currently ailing test
)$e )$ree 6aws o# )''
8/11/2019 cleancode-110520031623-phpapp02
82/88
+nit Tests
8/11/2019 cleancode-110520031623-phpapp02
83/88
77 tests c!me t! a single c!nclusi!n77 that is 5uicF an' easy t! un'erstan'3ne Assert per )est
Single Concept per )est77 the best rule is that y!u sh!ul'77 minimi4e the number ! asserts per c!ncept an'77 test :ust !ne c!ncept per test uncti!n
+nit Tests
8/11/2019 cleancode-110520031623-phpapp02
84/88
77 *ast77 9n'epen'ent77 2epeatable77 3elS-ali'ating77 Timely
*.I.R.S.).
8/11/2019 cleancode-110520031623-phpapp02
85/88
Classes
8/11/2019 cleancode-110520031623-phpapp02
86/88
77 a class !r m!'ule sh!ul' ha-e !ne. an' !nly !ne.77 reas!n t! change
77 32 is !ne ! the m!re imp!rtant c!ncept in ,, 'esign
)$e Single Responsibilit! 0rinciple SR0
Co$esion77 maintaining c!hesi!n results in many small classes
Emergence
8/11/2019 cleancode-110520031623-phpapp02
87/88
Simple 'esign Rule 1= Runs All t$e )estsSimple 'esign Rules 2= /o 'uplication
Simple 'esign Rules >= 9-pressi(eSimple 'esign Rules ?= Minimal Classes an" Met$o"s
8/11/2019 cleancode-110520031623-phpapp02
88/88