Naming Convention Guide

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

"

!"#$%& ()%*+%,$)% -.$/+



0.1+ 2 3 4+,,+5 !"#$%&6
varlable, funcLlon or class names should reveal ldeas abouL Lhelr
respecLlve funcLlons.

Lxample 1
Suppose we wanL Lo declare a varlable LhaL lndlcaLes elapsed Llme ln
Lerms of days". 1hen Lhe varlable should be declared as

lnL elapsed1lmelnuays,

And noL as

[W8CnC] lnL d, !"
lnL LlmeLlapsed, !"
lnL days,

Lxample 2
Can you guess whaL Lhls code ls dolng ?

publlc LlsL<lnL[]> geL1hem() [
LlsL<lnL[]> llsL1 = new ArrayLlsL<lnL[]>(),
for (lnL[] x : LheLlsL)[
lf (x[0] == 4)
llsL1.add(x),
}
reLurn llsL1,
}
1ake Llme Lo sLudy Lhe funcLlon carefully and once done, move forward.
#
now [usL rename Lhe meLhod and Lhe varlables and you can experlence
Lhe maglc !

Pere we are codlng for Lhe mlnesweeper game and we need Lo wrlLe a
funcLlon for geLLlng Lhe llsL of all Lhe flagged cells (cell ls an array of
lnLeger. 1he array of cells forms Lhe enLlre board). 1hen does Lhe
meLhod deflned below reveal any ldea abouL lLs behavlor?


publlc LlsL<lnL[]> geLllaggedCells() [
LlsL<lnL[]> flaggedCells = new ArrayLlsL<lnL[]>(),
for (lnL[] cell : game8oard)
lf (cell[S1A1uS_vALuL] == lLACCLu)
flaggedCells.add(cell),
reLurn flaggedCells,
}
Pere by only renamlng 'LheLlsL' as 'game8oard', 'llsL1' as 'flaggedCells'
and 'x' as 'cell' makes Lhe meLhod much easy Lo undersLand. 1he name
of Lhe meLhod 'geLllaggedCells' glves Lhe ldea abouL Lhe meLhod's
funcLlon. 'S1A1uS_vALuL' and 'lLACCLu' varlables speclfy Lhe
slgnlflcance of zeroLh lndex of Lhe cell[] and Lhe value for Lhe flagged cell
respecLlvely.


$
0.1+ 7 3 89$%&: ,) "*)$/ ;9$1+ %"#$%&6
<= uon'L use names LhaL can have mulLlple meanlngs.
lnL hp,
1hls varlable name 'hp' can mean a 'hypoLenuse varlable' or
'helghL of a plcLure' varlable. So lLs beLLer we use 'hypoLenuse' or
'plcLurePelghL' lnsLead of 'hp'.

4= uon'L use names LhaL vary ln small ways.
Avold declarlng varlables llke followlng
SLrlng x?ZConLrollerlorLfflclenLPandllngCfSLrlngs,
SLrlng x?ZConLrollerlorLfflclenLSLorageCfSLrlngs,

(= Avold uslng slngle-leLLer varlable names (slngle-leLLer names can CnL?
be used as local varlables lnslde shorL meLhods CnL? ll nLCLSSA8?).
lnL a, l, o,
varlables should %+*+5 be named as menLloned above.

>= uon'L name a varlable arblLrarlly [usL because you can'L use Lhe same
name Lo refer Lo Lwo dlfferenL Lhlngs ln Lhe same scope.
Conslder, for example, Lhe pracLlce of creaLlng a varlable named
'klass' [usL because Lhe name 'class' was used for someLhlng else.


%
?= uon'L use Lhe names whlch are confuslng, and you can'L undersLand
clearly whaL lL does laLer on
geLAcLlveAccounL(),
geLAcLlveAccounLs(),
geLAcLlveAccounLlnfo(),
Check Lhe above menLloned meLhods. Pow can we declde whlch
meLhod ls Lo be called and when lL ls Lo be called?

@= uon'L use number-serles (a1, a2..) and nolse words (a, an, Lhe..).
publlc sLaLlc vold copyChars(char a1[], char a2[]) [
for (lnL l = 0, l < a1.lengLh, l++) [
a2[l] = a1[l],
}
}
1hls funcLlon reads much beLLer when 'source' and 'desLlnaLlon' are
used ln Lhe place of a1 and a2 respecLlvely for Lhe argumenL names.

-= nolse words are anoLher meanlngless dlsLlncLlon, whlch doesn'L serve
any purpose. lmaglne LhaL you have a roducL class. lf you have anoLher
class called roducLlnfo or roducLuaLa, you have made Lhe names
dlfferenL wlLhouL maklng Lhem mean anyLhlng dlfferenL. lnfo and uaLa
are lndlsLlncL nolse words llke a, an, and Lhe.
noLe LhaL Lhere ls noLhlng wrong wlLh uslng preflxes convenLlons llke 'a'
and 'Lhe' as long as Lhey make a meanlngful dlsLlncLlon. lor example you
mlghL use 'a' for all local varlables and 'Lhe' for all funcLlon argumenLs.
1he problem comes ln when you declde Lo call a varlable LheZork
because you already have anoLher varlable named zork.

&
A= Avold Lype encodlng
SLrlng phonenumberSLrlng,
Pere Lhe problem occurs when Lhe Lype of Lhe varlable ls changed
and Lhe varlable name ls noL changed. L.g. honenumber
becomes long double lnsLead of SLrlng

B= Avold menLal mapplng. lor us $%&"'() '* +',-.
Conslder Lhe followlng declaraLlon:
lnL r, // lowercase verslon of Lhe url wlLh hosL & scheme removed
Pere, every Llme Lhe programmer comes across 'r' ln Lhe code
furLher, he has Lo remember LhaL 'r' ls Lhe lowercased verslon of
Lhe url wlLh Lhe hosL and scheme removed.

C= Avold uslng Lhe same word for Lwo purposes.
lf you follow Lhe one word per concepL" rule, you could end up
wlLh many classes LhaL have, for example, an &// meLhod.
now suppose we have many classes where &// wlll creaLe a new
value by addlng or concaLenaLlng Lwo exlsLlng values.
Suppose now we are wrlLlng a new class LhaL has a meLhod LhaL
puLs lLs slngle parameLer lnLo a collecLlon, should we call Lhls
meLhod &//? lL mlghL seem conslsLenL because we have so many
oLher &// meLhods, buL ln Lhls case, Lhe semanLlcs are dlfferenL,
so we should use a name llke ',*0"( or &110,/ lnsLead.

k. Avold Clever namlng
lf names are Loo clever, Lhey wlll be memorable only Lo people
who share Lhe auLhor's sense of humor, and only as long as Lhese
people remember Lhe [oke.
e.g. don'L use Lhe name llke whack() Lo mean klll()
'
0.1+ D6 E9", ,9$%&: ,) $%F1./+ ;9$1+ %"#$%&
<= Always use pronounceable names.
G:+
prlvaLe uaLe modlflcaLlon1lmesLamp,
$%:,+"/ )H
[W8CnC] prlvaLe uaLe modyLs,

4= use searchable names
Cne mlghL easlly be able Lo flnd MAx_CLASSLS_L8_S1uuLn1, buL
flndlng Lhe number 7 could be more Lroublesome. Searches may Lurn up
Lhe dlglL as parL of flle names, oLher consLanL deflnl- Llons, and ln varlous
expresslons where Lhe value ls used wlLh dlfferenL lnLenL.
Compare
for (lnL [=0, [<34, [++) [
s += (L[[]*4)/3,
}
1o
lnL realuayserldealuay = 4,
consL lnL WC8k_uA?S_L8_WLLk = 3,
lnL sum = 0,
for (lnL [=0, [ < nuM8L8_Cl_1ASkS, [++) [
lnL real1askuays = LaskLsLlmaLe[[] * realuayserldealuay,
lnL real1askWeeks = (realdays / WC8k_uA?S_L8_WLLk),
sum += real1askWeeks,
}
(
noLe LhaL 'sum', above, ls noL a parLlcularly useful name buL aL leasL ls
searchable, buL conslder how much easler lL wlll be Lo flnd
WC8k_uA?S_L8_WLLk Lhan Lo flnd all Lhe places where 3 was used
and fllLer Lhe llsL down Lo [usL Lhe lnsLances wlLh Lhe lnLended meanlng.

(= Classes and ob[ecLs should have noun or noun phrase
names llke CusLomer, Wlklage, AccounL, and Addressarser.
Avold words llke Manager, rocessor, uaLa, or lnfo ln Lhe name of
a class. A class name should noL be a verb.

>= MeLhods should have verb or verb phrase
names llke posLaymenL, deleLeage, or save. Accessors,
muLaLors, and predlcaLes should be named for Lhelr value and
preflxed wlLh geL, seL, and ls accordlng Lo Lhe [avabean sLandard.
SLrlng name = employee.geLname(),
cusLomer.seLname("mlke"),
lf (paycheck.lsosLed())...

?= lck one word per concepL.
lck one word for one absLracL concepL and sLlck wlLh lL. lor
lnsLance, lL's confuslng Lo have 20($34 "0("'0504 and -0( as
equlvalenL meLhods of dlfferenL classes. Pow do you remember
whlch meLhod name goes wlLh whlch class? lnsLead use any one
of 'feLch', 'reLrleve' or 'geL' for all Lhe class.


)
@= 1ry Lo add meanlngful conLexL.
lmaglne LhaL you have varlables named 2'"*(6&70, %&*(6&70,
*("00(, 3!8*068790", $'(), *(&(0, and :'1$!/0. 1aken LogeLher lL's
preLLy clear LhaL Lhey form an address. 8uL whaL lf you [usL saw
Lhe *(&(0 varlable belng used alone ln a meLhod? Would you
auLomaLlcally lnfer LhaL lL was parL of an address?
?ou can add conLexL by uslng preflxes: &//";'"*(6&70,
&//"<&*(6&70, &//"=(&(0, and so on. AL leasL readers wlll
undersLand LhaL Lhese varlables are parL of a larger sLrucLure. A
beLLer soluLlon ls Lo creaLe a class named Address.
Conslder Lhe followlng example for beLLer undersLandlng of Lhls
case:
>&"'&9%0* ?'(3 8,$%0&" $!,(0@(
prlvaLe vold prlnLCuessSLaLlsLlcs(char candldaLe, lnL counL) [
SLrlng number,
SLrlng verb,
SLrlng pluralModlfler,
lf (counL == 0) [
number = "no",
verb = "are",
pluralModlfler = "s",
} else lf (counL == 1) [
number = "1",
verb = "ls",
pluralModlfler = "",
} else [
number = lnLeger.LoSLrlng(counL),
*
verb = "are",
pluralModlfler = "s",
}
SLrlng guessMessage = SLrlng.formaL( "1here s s
ss", verb, number, candldaLe,
pluralModlfler ),
prlnL(guessMessage),
}
!"#$"%&'( *$+, -&'"# -./+'0+
publlc class CuessSLaLlsLlcsMessage [
prlvaLe SLrlng number,
prlvaLe SLrlng verb,
prlvaLe SLrlng pluralModlfler,
publlc SLrlng make(char candldaLe, lnL counL) [
creaLeluraluependenLMessagearLs(counL),
reLurn SLrlng.formaL("1here s s ss", verb,
number, candldaLe, pluralModlfler ),
}
prlvaLe vold creaLeluraluependenLMessagearLs(lnL
counL) [
lf (counL == 0) [
LhereArenoLeLLers(),
} else lf (counL == 1) [
LherelsCneLeLLer(),
} else [
LhereAreManyLeLLers(counL),
"+
}
}
prlvaLe vold LhereAreManyLeLLers(lnL counL) [
number = lnLeger.LoSLrlng(counL),
verb = "are",
pluralModlfler = "s",
}
prlvaLe vold LherelsCneLeLLer() [
number = "1",
verb = "ls",
pluralModlfler = "",
}
prlvaLe vold LhereArenoLeLLers() [
number = "no",
verb = "are",
pluralModlfler = "s",
}
}

You might also like