Vous êtes sur la page 1sur 88

CLEAN CODE

Two reasons


The Boy Scout Rule


Elegance





Simple, direct, prose





Literate


Care




Small, epressi!e, simple





"hat you epected





#eaning$ul Names
public List<int[]> getThem() {
List<int[]> list1 = new ArrayList<int[]>();
!r (int[] " # theList)
i ("[$] == %)
list1&a''(");
return list1;
(

#eaning$ul Names
public List<)ell> get*lagge')ells() {
List<)ell> lagge')ells = new ArrayList<)ell>();
!r ()ell cell # game+!ar')
i (cell&is*lagge'())
lagge')ells&a''(cell);
return lagge')ells;
(

#eaning$ul Names
int a = l;
i (, == l)
a = ,1;
else
l = $1;


public static -!i' c!py)hars(char a1[]. char a/[]) {
!r (int i = $; i < a1&length; i00) {
a/[i] = a1[i];
(
(
#eaning$ul Names
class 1ta2cr'1$/ {
pri-ate 1ate genym'hms;
pri-ate 1ate m!'ym'hms;
pri-ate inal 3tring ps45int = 61$/6;
78 &&& 87
(;

#eaning$ul Names
class )ust!mer {
pri-ate 1ate generati!nTimestamp;
pri-ate 1ate m!'iicati!nTimestamp;;
pri-ate inal 3tring rec!r'9' = 61$/6;
78 &&& 87
(;

#eaning$ul Names
!r (int : = $; : < ;%; :00) {
s 0= (t[:] 8 %) 7 <;
(

#eaning$ul Names
int real1ays=er9'eal1ay = %;
c!nst int >,2?@1AA3@=B2@>BB? = <;
int sum = $;
!r (int : = $; : < CDE+B2@,*@TA3?3; :00) {
int realTasF1ays = tasFBstimate[:] 8
real1ays=er9'eal1ay;
int realTasF>eeFs = (real'ays 7 >,2?@1AA3@=B2@>BB?);
sum 0= realTasF>eeFs;
(

#eaning$ul Names
public class =art {
pri-ate 3tring m@'sc; 77 The te"tual 'escripti!n
-!i' setCame(3tring name) {
m@'sc = name;
(
(


=h!neCumber ph!ne3tring;
77 name n!t change' when type change'G
#eaning$ul Names
public class =art {
3tring 'escripti!n;
-!i' set1escripti!n(3tring 'escripti!n) {
this&'escripti!n = 'escripti!n;
(
(


=h!neCumber ph!ne;
#eaning$ul Names
!r (a = $; a < 1$; a00)
!r (b = $; b < 1$; b00)


Eanager. =r!cess!r. 1ata. 9n!
#eaning$ul Names
!r (i = $; i < 1$; i00)
!r (: = $; : < 1$; :00)


)ust!mer. >iFi=age. Acc!unt. A''ress=arser
77 a class name sh!ul' n!t be a -erb
#eaning$ul Names
p!st=ayment. 'elete=age. sa-e
77 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"(/;&$);

#eaning$ul Names
etch. retrie-e. get 77 as e5ui-alent meth!'s
c!ntr!ller. manager. 'ri-er 77 c!nusing


77 a-!i' using the same w!r' !r tw! purp!ses
#eaning$ul Names
Acc!untHisit!r. I!bJueue
77 pe!ple wh! rea' y!ur c!'e will be pr!grammers


irstCame. lastCame. street. city. state. 4ipc!'e
77 a better s!luti!n
a''r*irstCame. a''rLastCame. a''r3tate
77 a better s!luti!n
)lass A''ress
#eaning$ul Names
A''ress
77 is a ine name !r a class
Acc!untA''ress. )ust!merA''ress
77 are ine names !r instances ! the class A''ress
77 but c!ul' be p!!r names !r classes
EA) a''resses. p!rt a''resses. >eb a''resses
77 a better s!luti!n
=!stalA''ress. EA). D29

%unctions
77 rules ! uncti!ns#
77 1& sh!ul' be small
77 /& sh!ul' be smaller than that
77 < 1<$ characters per line
77 < /$ lines


77 *DC)T9,C3 3K,DL1 1, ,CB TK9CL& TKBA 3K,DL1 1, 9T >BLL&
77 TKBA 3K,DL1 1, 9T ,CLA&
%unctions
77 high le-el ! abstracti!n
getKtml()
77 interme'iate le-el ! abstracti!n
3tring page=athCame = =ath=arser&ren'er(page=ath);
77 remarFably l!w le-el
&appen'(6Mn6)


77 the 3tep'!wn 2ule
%unctions
class Bmpl!yee&&&
int payAm!unt() {
switch (getType()) {
case Bmpl!yeeType&BCL9CBB2#
return @m!nthly3alary;
case Bmpl!yeeType&3ALB3EAC#
return @m!nthly3alary 0 @c!mmissi!n;
case Bmpl!yeeType&EACALB2#
return @m!nthly3alary 0 @b!nus;
'eault#
thr!w new B"cepti!n(69nc!rrect Bmpl!yee6);
(
(

%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();
(

%unctions
testableKtml => inclu'e3etupAn'Tear'!wn=ages
inclu'e3etupAn'Tear'!wn=ages. inclu'e3etup=ages.
inclu'e3uite3etup=age. inclu'e3etup=age
77 what happene' t!
inclu'eTear'!wn=ages. inclu'e3uiteTear'!wn=age.
inclu'eTear'!wn=age


77 the i'eal number ! arguments !r a uncti!n is 4er!
%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)


ren'er(true)
%unctions
77 asFing a 5uesti!n ab!ut that argument
b!!lean ileB"ists(NEy*ileO)
77 !perating !n that argument. trans!rming an' returning it
9nput3tream ile,pen(NEy*ileO)
77 e-ent. use the argument t! alter the state ! the system
-!i' passw!r'Attempt*aile'Ctimes(int attempts)


ren'er*!r3uite()
ren'er*!r3ingleTest()
%unctions
write*iel'(name)
77 is easier t! un'erstan' than
write*iel'(!utput3tream. name)
77 perectly reas!nable
=!int p = new =!int($.$)
77 pr!blematic
assertB5uals(e"pecte'. actual)

assertB5uals(message. e"pecte'. actual)


%unctions
)ircle maFe)ircle('!uble ". '!uble y. '!uble ra'ius);
)ircle maFe)ircle(=!int center. '!uble ra'ius);


write(name)
write*iel'(name)
assertB5uals(e"pecte'. actual)
assertB"pecte'B5ualsActual(e"pecte'. actual)
%unctions
77 '! s!mething !r answer s!mething. but n!t b!th
public b!!lean set(3tring attribute. 3tring -alue);
setAn')hecF9B"ists
i (attributeB"ists(6username6)) {
setAttribute(6username6. 6uncleb!b6);
&&&
(

%unctions
77 'uplicati!n may be the r!!t ! all e-il in s!tware


77 B'sger 1i:FstraPs rules
77 !ne entry
77 !ne e"it
77 uncti!ns small
77 !ccasi!nal multiple return. breaF. !r c!ntinue statement
77 can s!metimes e-en be m!re e"pressi-e 1i:FstraPs rules
Comments
77 '!nPt c!mment ba' c!'e. rewrite itG


77 )hecF t! see i the empl!yee is eligible !r ull
beneits
i ((empl!yee&lags Q K,D2LA@*LAL) QQ
(empl!yee&age > R<))
i (empl!yee&isBligible*!r*ull+eneits())
Comments &good'
77 )!pyright ()) /$11 by ,s!c!& All rights reser-e'&
77 2elease' un'er the terms ! the LCD Leneral =ublic
License 77 -ersi!n / !r later&


77 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 ''. yyyy
=attern timeEatcher = =attern&c!mpile(
6MM'8#MM'8#MM'8 MMw8. MMw8 MM'8. MM'86);
Comments &good'
77This is !ur best attempt t! get a race c!n'iti!n
77by 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();
(

assertTrue(a&c!mpareT!(b) == S1); 77 a < b


assertTrue(b&c!mpareT!(a) == 1); 77 b > a
Comments &good'
public static 3imple1ate*!rmat maFe3tan'ar'Kttp1ate*!rmat() {
773imple1ate*!rmat is n!t threa' sae.
77s! we nee' t! create each instance in'epen'ently&
3imple1ate*!rmat ' = new 3imple1ate*!rmat(6'' EE yyyy6);
'&setTimeT!ne(TimeT!ne&getTimeT!ne(6LET6));
return ';
(


77T,1,SE'E these are n!t nee'e'
77 >e e"pect this t! g! away when we '! the checF!ut m!'el
Comments &good'
3tring list9tem)!ntent = match&gr!up(;)&trim();
77 the trim is real imp!rtant& 9t rem!-es the starting
77 spaces that c!ul' cause the item t! be rec!gni4e'
77 as an!ther list&
new List9tem>i'get(this. list9tem)!ntent. this&le-el 0 1);
return buil'List(te"t&substring(match&en'()));


77 there is n!thing 5uite s! helpul an' satisying
77 as a wellS'escribe' public A=9
Comments &(ad'
try {
3tring pr!perties=ath = pr!pertiesL!cati!n 0 676 0
=2,=B2T9B3@*9LB;
*ile9nput3tream pr!perties3tream =
new *ile9nput3tream(pr!perties=ath);
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'
(

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*!r)l!se
(inal l!ng time!utEillis) thr!ws B"cepti!n
{
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);
(
(

Comments &(ad'
788
8 The pr!cess!r 'elay !r this c!mp!nent&
87
pr!tecte' int bacFgr!un'=r!cess!r1elay = S1;
788
8 The liecycle e-ent supp!rt !r this c!mp!nent&
87
pr!tecte' Liecycle3upp!rt liecycle =
new Liecycle3upp!rt(this);
788
8 The c!ntainer e-ent listeners !r this )!ntainer&
87
pr!tecte' ArrayList listeners = new ArrayList();

Comments &(ad'
788
8 Uparam title The title ! the )1
8 Uparam auth!r The auth!r ! the )1
8 Uparam tracFs The number ! tracFs !n the )1
8 Uparam 'urati!n9nEinutes The 'urati!n ! the )1 in minutes
87
public -!i' a'')1(3tring title. 3tring auth!r.
int tracFs. int 'urati!n9nEinutes) {
)1 c' = new )1();
c'&title = title;
c'&auth!r = auth!r;
c'&tracFs = tracFs;
c'&'urati!n = 'urati!n9nEinutes;
(

Comments &(ad'
8 )hanges (r!m 11S,ctS/$$1)
8 SSSSSSSSSSSSSSSSSSSSSSSSSS
8 11S,ctS/$$1 # 2eS!rganise' the class an' m!-e' it t! new
8 pacFage c!m&:reinery&'ate (1L);
8 $<SC!-S/$$1 # A''e' a get1escripti!n() meth!'. an'
8 eliminate' C!table1ate class (1L);
8 1/SC!-S/$$1 # 9+1 re5uires set1escripti!n() meth!'. n!w
8 that C!table1ate class is g!ne (1L); )hange'
8 get=re-i!us1ay,>eeF().
8 get*!ll!wing1ay,>eeF() an'
8 getCearest1ay,>eeF() t! c!rrect bugs (1L);
8 $<S1ecS/$$1 # *i"e' bug in 3prea'sheet1ate class (1L);
8 /VSEayS/$$/ # E!-e' the m!nth c!nstants int! a separate
8 interace (E!nth)!nstants) (1L);

Comments &(ad'
788
8 1eault c!nstruct!r&
87
pr!tecte' Annual1ate2ule() { (
788 The 'ay ! the m!nth& 87
pri-ate int 'ay,E!nth;
788
8 2eturns the 'ay ! the m!nth&
8 Ureturn the 'ay ! the m!nth&
87
public int get1ay,E!nth() {
return 'ay,E!nth;
(

Comments &(ad'
788 The name& 87
pri-ate 3tring name;
788 The -ersi!n& 87
pri-ate 3tring -ersi!n;
788 The licenceCame& 87
pri-ate 3tring licenceCame;
788 The -ersi!n& 87
pri-ate 3tring in!;

Comments &(ad'
77 '!es the m!'ule r!m the gl!bal list <m!'> 'epen' !n the
77 subsystem we are part !W
i (sm!'ule&get1epen'3ubsystems()
&c!ntains(sub3ysE!'&get3ub3ystem()))
77 this c!ul' be rephrase' with!ut the c!mment as
ArrayList m!'ule1epen'ees = sm!'ule&get1epen'3ubsystems();
3tring !ur3ub3ystem = sub3ysE!'&get3ub3ystem();
i (m!'ule1epen'ees&c!ntains(!ur3ub3ystem))

Comments &(ad'
77 Acti!ns 7777777777777777777777777777777777


while ((line = in&rea'Line()) G= null) {
line)!unt00;
char)!unt 0= line&length();
3tring w!r's[] = line&split(6MM>6);
w!r')!unt 0= w!r's&length;
( 77while
Comments &(ad'
78 A''e' by 2icF 87


9nput3tream2esp!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 &(ad'
788
8 TasF t! run it tests&
8 This tasF runs itnesse tests an' publishes the results&
8 <p7>
8 <pre>
8 Dsage#
8 Qlt;tasF'e name=Q5u!t;e"ecuteSitnesseStestsQ5u!t;
8 classname=Q5u!t;itnesse&ant&B"ecute*itnesseTestsTasFQ5u!t;
8 classpathre=Q5u!t;classpathQ5u!t; 7Qgt;
8 ,2
8 Qlt;tasF'e classpathre=Q5u!t;classpathQ5u!t;
8 res!urce=Q5u!t;tasFs&pr!pertiesQ5u!t; 7Qgt;
8 <p7>
8 Qlt;e"ecuteSitnesseStests

Comments &(ad'
788
8 =!rt !n which itnesse w!ul' run& 1eaults t! <b>X$X/<7b>&
8
8 Uparam itnesse=!rt
87
public -!i' set*itnesse=!rt(int itnesse=!rt)
{
this&itnesse=!rt = itnesse=!rt;
(

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 bits
as !utput strings ! % enc!'e' characters& =r!cee'ing r!m
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 in
the baseR% alphabet&
>hen enc!'ing a bit stream -ia the baseR% enc!'ing. the bit
stream must be presume' t! be !r'ere' with the m!stS
signiicantSbit irst&
87

Comments &(ad'
78
8 start with an array that is big en!ugh t! h!l' all the
8 pi"els (plus ilter bytes). an' an e"tra /$$ bytes !r
8 hea'er in!
87
this&png+ytes = new byte[((this&wi'th 0 1) 8 this&height 8 ;)
0 /$$];


77 sh!rt uncti!ns '!nPt nee' much 'escripti!n
Comments &(ad'
77 e"tra !rmality ! the :a-a'!c c!mments

%ormatting
77 c!mmunicati!n


77 highSle-el S> 'etails

77 each blanF line is a -isual cue
77 that i'entiies a new an' separate c!ncept
%ormatting
77 -ertical 'ensity implies cl!se ass!ciati!n
788
8 The class name ! the rep!rter listener
87
pri-ate 3tring m@classCame;
788
8 The pr!perties ! the rep!rter listener
87
pri-ate m@pr!perties = new ArrayList();

%ormatting
77 -ariables
77 sh!ul' be 'eclare' as cl!se t! their usage as p!ssible
77 instance -ariables
77 sh!ul' be 'eclare' at the t!p ! the class
77 'epen'ent uncti!ns
77 i !ne uncti!n calls an!ther. they sh!ul' be -ertically
77 cl!se. an' the caller sh!ul' be ab!-e the calle'
77 c!nceptual ainity
77 certain bits ! c!'e want t! be near !ther bits

%ormatting
pri-ate -!i' measureLine(3tring line) {
line)!unt00;
int line3i4e = line&length();
t!tal)hars 0= line3i4e;
line>i'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);
(

%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 re5uest=arsingTimeLimit;
pri-ate l!ng re5uest=r!gress;
pri-ate l!ng re5uest=arsing1ea'line;
pri-ate b!!lean hasBrr!r;
&&&
(

%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 re5uest=arsingTimeLimit;
pri-ate l!ng re5uest=r!gress;
pri-ate l!ng re5uest=arsing1ea'line;
pri-ate b!!lean hasBrr!r;
&&&
(

%ormatting
public class )!mment>i'get e"ten's Te"t>i'get {
public static inal 3tring 2BLBY= =
6Z[[ZMrMn]8(W#(W#MrMn)\Mn\Mr)W6;
public )!mment>i'get(3tring te"t) { super(te"t); (
public 3tring ren'er() thr!ws B"cepti!n { return 66; (
(

%ormatting
public class )!mment>i'get e"ten's Te"t>i'get {
public static inal 3tring 2BLBY= =
6Z[[ZMrMn]8(W#(W#MrMn)\Mn\Mr)W6;
public )!mment>i'get(3tring te"t) {
super(te"t);
(
public 3tring ren'er() thr!ws B"cepti!n {
return 66;
(
(

%ormatting
77 e-ery pr!grammer has his !wn a-!rite !rmatting rules
77 but i he w!rFs in a team
77 then the team rules

O()ects and Data Structures
)!ncrete =!int
public class =!int {
public '!uble ";
public '!uble y;
(

O()ects and Data Structures
Abstract =!int
public interace =!int {
'!uble getY();
'!uble getA();
-!i' set)artesian('!uble ". '!uble y);
'!uble get2();
'!uble getTheta();
-!i' set=!lar('!uble r. '!uble theta);
(

O()ects and Data Structures
)!ncrete Hehicle
public interace Hehicle {
'!uble get*uelTanF)apacity9nLall!ns();
'!uble getLall!ns,Las!line();
(

O()ects and Data Structures
Abstract Hehicle
public interace Hehicle {
'!uble get=ercent*uel2emaining();
(

O()ects and Data Structures
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

O()ects and Data Structures
inal 3tring !utput1ir = ct"t&get,pti!ns()
&get3cratch1ir()
&getAbs!lute=ath();


,pti!ns !pts = ct"t&get,pti!ns();
*ile scratch1ir = !pts&get3cratch1ir();
inal 3tring !utput1ir = scratch1ir&getAbs!lute=ath();
inal 3tring !utput1ir = ct"t&!pti!ns&scratch1ir&abs!lute=ath;
Error *andling
i ('elete=age(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;
(

Error *andling
try {
'elete=age(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());
(

public -!i' 'elete(=age page) {
try {
'elete=ageAn'All2eerences(page);
( catch (B"cepti!n e) {
l!gBrr!r(e);
(
(
pri-ate -!i' 'elete=ageAn'All2eerences(=age page) thr!ws B"cepti!n {
'elete=age(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());
(

Error *andling
77 uncti!ns sh!ul' '! !ne thing
77 err!r han'ing is !ne thing
77 i the Feyw!r' try e"ists in a uncti!n
77 it sh!ul' be the -ery irst w!r' in the uncti!n an' that
77 there sh!ul' be n!thing ater the catch7inally bl!cFs

Error *andling
try {
EealB"penses e"penses = e"pense2ep!rt1A,
&getEeals(empl!yee&get91());
m@t!tal 0= e"penses&getT!tal();
( catch(EealB"pensesC!t*!un' e) {
m@t!tal 0= getEeal=er1iem();
(

Error *andling
EealB"penses e"penses = e"pense2ep!rt1A,
&getEeals(empl!yee&get91());
m@t!tal 0= e"penses&getT!tal();

Error *andling
List<Bmpl!yee> empl!yees = getBmpl!yees();
i (empl!yees G= null) {
!r(Bmpl!yee e # empl!yees) {
t!tal=ay 0= e&get=ay();
(
(

Error *andling
List<Bmpl!yee> empl!yees = getBmpl!yees();
!r(Bmpl!yee e # empl!yees) {
t!tal=ay 0= e&get=ay();
(
public List<Bmpl!yee> getBmpl!yees() {
i( && there are n! empl!yees && )
return )!llecti!ns&emptyList();
(

Error *andling
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 Eetrics)alculat!r&"=r!:ecti!n6);
(
return (p/&" ] p1&") 8 1&<;
(

Error *andling
+nit Tests
77 irst law
77 y!u may n!t write pr!'ucti!n c!'e until
77 y!u ha-e written a ailing unit test
77 sec!n' law
77 y!u may n!t write m!re ! a unit test
77 than is suicient t! ail. an' n!t c!mpiling is ailing
77 thir' law
77 y!u may n!t write m!re pr!'ucti!n c!'e
77 than is suicient t! pass the currently ailing test

+nit Tests
77 test c!'e is :ust as imp!rtant as pr!'ucti!n c!'e


77 what maFes a clean testW three things
77 rea'ability. rea'ability. an' rea'ability
+nit Tests
77 tests c!me t! a single c!nclusi!n
77 that is 5uicF an' easy t! un'erstan'


77 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
77 *ast
77 9n'epen'ent
77 2epeatable
77 3elS-ali'ating
77 Timely

Classes
77 public static c!nstants
77 pri-ate static -ariables
77 pri-ate instance -ariables
77 public uncti!ns
77 pri-ate utilities calle' by a public uncti!n right ater


77 the irst rule is that they sh!ul' be small
77 the sec!n' rule is that they sh!ul' be smaller than that
Classes
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

77 maintaining c!hesi!n results in many small classes


Emergence




,uestions-

Vous aimerez peut-être aussi