02.Coding Conventions

download 02.Coding Conventions

of 163

Transcript of 02.Coding Conventions

  • 7/25/2019 02.Coding Conventions

    1/163

    Version : 0.3

    Create Date : 15/03/2006Revise Date : 28/06/2006

    On behalf of On behalf of

    T Y TA Motor Asia Pacific Co.,Ltd. Fujitsu Systems usiness !Thailand" Ltd.

    A##ro$ed by% A##ro$ed by%

    Project Ad$isor Mr. Shibutani &a'uhiro Project Leader Mr. Yoshia(i Asa(a

    )ate )ate

    Project Ad$isor Mr. *iriya Tumnao Project Mana+er Ms. Patchara#ohn Srira,e,on+sa

    )ate )ate

    Project Leader Ms. Chinda A(sornu(ul Pre#ared y Mr. Yusuf

    )ate )ate Mr. )anilo -uestro

    Project Mana+er Mr. Chatchai Si tthicho(esiri

    )ate

    Fujitsu Systems Business (Thailand) Ltd.

    Toyota Motor Asia Pacifc Co., Ltd.Coding Conventions

    Toyota Motor Asia Pacifc Co., Ltd.Coding Conventions

  • 7/25/2019 02.Coding Conventions

    2/163

    Revision History

    Date Updated by Revision Update Point

    15 March 2006 Yusuf 0.1 newly created

    30 May 2006 Dani 0.2 Modified based on FSB !eetin"2# $un 2006 Dani 0.3 Modify the na!es fro! M%& to usin" M%&

  • 7/25/2019 02.Coding Conventions

    3/163

    3'163Phase Project Name TMAP Application Standard Page

    Block Name TMAP Developer Guide Function Name Created !!"#!$#!% B& 'usu(

    Document Name Naming Conventions Title Ta)le o( Contents *pdated !!"#!"#+, B& Dani

    Table of Contents

    %. $a(a )odin" )on(ention

    %.1. )odin" Styles

    %.1.1 )o!!ent

    %.1.2 *+date ,istory

    %.1.3 )o+yri"ht

    %.2. -a!in" ules%.2.1. /eneral

    %.2.2. &aca"e

    %.2.3. )lass%.2.. est )lass

    %.2.5. Method

    %.2.6. %r"u!ent

    %.2.. aribale in /eneral

    %.2.#. 4ocal ariable

    %.3. )odin" ules

    %.3.1. /eneral%.3.2. Metrics

    %.3.3. For!at

    %.3.. )o!!ent

    %.3.5. !+ort

    %.3.6. )lass

    %.3.. )onstructor

    %.3.#. Method

    %.3.. )lass Method

    %.3.10. ariable in /eneral

    %.3.11. nstance ariable

    %.3.12. )lass ariable

    %.3.13. 4ocal ariable

    %.3.1. nheritance

    %.3.15. nstance

  • 7/25/2019 02.Coding Conventions

    4/163

    '163Phase Project Name TMAP Application Standard Page

    Block Name TMAP Developer Guide Function Name Created !!"#!$#!% B& 'usu(

    Document Name Naming Conventions Title Ta)le o( Contents *pdated !!"#!"#+, B& Dani

    %.3.16. )ontrol Structure

    %.3.1. hread%.3.1#. Strin" Mani+ulation

    %.3.1. -u!ber

    %.3.20. Date

    %.3.21. )ollection

    %.3.22. Strea!

    %.3.23. 78ce+tion

    %.3.2. /arba"e )ollection

    %.3.25. 9thers

    B. Batch )odin" )on(entionB.1. )odin" Styles

    B.1.1. ,eader Section

    B.1.2. nclude Section

    B.1.3. /lobal ariables Section

    B.1.. 78tern ariables Section

    B.1.5. Main Function Section

    ). S:4 )odin" )on(ention).1. )odin" Styles

    ).2. )odin" ules

    ).2.1. ules on n;uiry &rocessin"

    ).2.2. ules on *+date &rocessin"

    ).2.3. ransaction ules

    D. Shell Scri+t )odin" )on(entionD.1. )odin" Styles

    D.1.1. ,eader )o!!ent

    %++endi8 %%++ % < S;li+s

  • 7/25/2019 02.Coding Conventions

    5/163

    5'163Phase Project Name TMAP Application Standard Page

    Block Name TMAP Developer Guide Function Name Created !!"#!$#!% B& 'usu(

    Document Name Naming Conventions Title Ta)le o( Contents *pdated !!"#!"#+, B& Dani

    %++endi8 B%++ B < S:4 Standards

  • 7/25/2019 02.Coding Conventions

    6/163

    6'163&hase &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 2006 but for the continuity of !aintainin" the a++lication> a readable and understandable source code

    is considerably (ery i!+ortant. herefore> this docu!ent +ro(ide a )odin" )on(ention as a /uideline for any Syste! De(elo+!ent endor to de(elo+

    a new syste! a++lication.

    1.2 Sco+e

    his docu!ent co(er a /uideline for )odin" )on(ention for $a(a accordin" to standard $a(a )odin" )on(ention +ro(ided by Sun Microsyste!>

    )odin" )on(ention for Batch +rocessin"> )odin" )on(ention for S:4> and )odin" )on(ention for Shell scri+tin".

    For rules which are not described here> can be refer to -a!in" )on(ention as a /eneral ules.

    1.3 %dditional

    For M%& standard> we can use the )hecStyle De(elo+er tool to (alidate and chec if the codes written adheres to the codin" standard as set by SunMicrosyste!s.

    S:4 desi"n standards and correct a++roach are included in this docu!ent as an a++endi8.

    http://checkstyle.sourceforge.net/

    http://checkstyle.sourceforge.net/http://checkstyle.sourceforge.net/
  • 7/25/2019 02.Coding Conventions

    7/163

    '163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 2006

    and the usual co!!ent is s+ecified.

    a. ,eader )o!!ent

    % title is attached to a +er

  • 7/25/2019 02.Coding Conventions

    8/163

    #'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 2006

  • 7/25/2019 02.Coding Conventions

    9/163

    '163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 2006 etc. Hsee s!s.est

    @E'+re

    @

    @ Hauthor %n i!+le!entor na!e is described Cdescribe with a half

  • 7/25/2019 02.Coding Conventions

    10/163

    10'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 2006 and lo" any e8e+tion

    @'

    Strin" fileFull-a!e J "etnFileDirC"etnFile-a!eCK

    File f J new FileCfileFull-a!eK

    tryN

    fw J new FileGriterCfileFull-a!e>trueK

    OcatchC978ce+tion eN

    7rror4o".writeCP.setFilecreate file writer errorPK

    rc J falseK

    O

  • 7/25/2019 02.Coding Conventions

    11/163

    11'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 2006

  • 7/25/2019 02.Coding Conventions

    12/163

    12'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2. -a!in" ules

    %.2.1. /eneral

    -R/-001 *se 7n"lish

    *se 7n"lish for na!in".

    By this standardiation> the code will be !ore readable by others.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic boolean hasTaio C N +ublic boolean hasStoc C N

    O O

    -R/-002 Do not distin"uish na!e ' (ariable only by s!all letter or ca+ital letter

    n the =a(a s+ecification> ca+ital letter and s!all letter is treated differently> but do not create na!e

    (ariable that only be distin"uished by s!all'ca+ital letter.

    By this standardiation> the code will be !ore readable by others

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ri(ate int nu!berK +ri(ate int car-u!berK

    +ri(ate int -u!berK +ri(ate int train-u!ber

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    13/163

    13'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated 2006 the code will be !ore readable by others

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    th.co.toyota.fra!ewor.banin".a00001 th.co.toyota.fra!ewor.banin".account

    -R&?/003 DonIt shorten +aca"e na!e

    *se understandable +aca"e na!e> donIt shorten it e(en if it beco!e lon"er

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    th.co.toyota.fw.b".acc th.co.toyota.fra!ewor.banin".account

    -R&?/00 t is +ossible to ha(e du+lication in sub +aca"e na!e

    t is +ossible to ha(e the sa!e sub +aca"e na!e if +arent +aca"e is different

    CF:D- is uni;ue

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    th.co.toyota.fra!ewor.banin".account

    th.co.toyota.fra!ewor.tradin".account

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    14/163

    1'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated 2006

  • 7/25/2019 02.Coding Conventions

    15/163

    15'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2.3. )lass

    -R)4S00 *se class na!e which re+resent the role of that class

    DonUt use na!e which is not !eanin"ful for class na!e> such as function D or se;uential

    nu!ber. By usin" !eanin"ful one> class contents can be associated fro! class na!e> and

    the code will be !ore readable.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class S0001 N +ublic class Sa!+le N

    O O

    -R)4S001 *se ca+ital for the first letter of class na!e

    *se ca+ital letter for the first letter of class na!e. f class na!e is !ade u+ fro! !ulti+le word>

    use ca+ital letter for the first letter of each word. his is the co!!on rule of $a(a

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class sa!+leclass N +ublic class Sa!+le)lass N

    O O

    -R)4S002 &ut P78ce+tionP at the end of e8ce+tion class na!e

    &ut P78ce+tionP at the end of e8ce+tion class na!e. By this standardiation> the code will be !ore

    readable

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class Sa!+le e8tends 78ce+tion N +ublic class Sa!+le78ce+tion e8tends 78ce+tion N

    O O

    -R)4S003 nterface na!in" follows class na!in"

    -a!in" rule for interface na!e basically follow the na!in" rule of class na!e.

    By this standardiation> the code will be !ore readable by others

    78ce+tion A f the distinction with class na!e is necessary> +ut PP at the head of interface na!e.

    Sa!+le

    +ublic class Sa!+le i!+le!ents Sa!+le N

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    16/163

    16'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    O

    -R)4S006 %bstract class na!in" follows class na!in"

    -a!in" rule for abstract class na!e basically follows the na!in" rule of class na!e.

    By this standardiation> the code will be !ore readable by others

    78ce+tion A f there is no suitable na!e> +ut P%bstractP at first> then use na!e which can be

    associated with sub class na!e

    Sa!+le

    abstract class %bstractSa!+le N

    O

    -R)4S005 !+le!entation class na!in" follows class na!in"

    -a!in" rule for i!+le!entation class na!e basically follows the na!in" rule of class na!e.

    By this standardiation> the code will be !ore readable by others

    78ce+tion A f the distinction with interface is necessary> +ut P!+lP at the end

    Sa!+le

    +ublic class Sa!+le!+l N

    O

    -R)4S00 &ut PableP at the end for interface that add so!e ability to class

    here are so!e interface that add so!e ability to a class such as unnable> )loneable> etc.

    Ghen we define this ty+e of interface> use ad=ecti(e CVable which re+resent that ability as

    class na!e. By this standardiation> the code will be !ore readable by others.Sa!+le

    +ublic class Sa!+le i!+le!ents &lu""able N

    O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    17/163

    1'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    18/163

    1#'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2.. est )lass

    -R)4001 est )lass na!e should be Wtest tar"et class na!e estX

    *se Wtest tar"et class na!e estX for test class na!e.

    By this standardiation> the code will be !ore readable by others.

    Sa!+le below consider the use of $*-

    Sa!+le

    +ublic class Sa!+le)lassest e8tends est)ase N

    O

    -R)4002 he na!e of class which +erfor!s all test and +aca"e test should be W%llestX or W+aca"e na!e estX

    *se W%llestX or W+aca"e na!e estX for class na!e of class which +erfor!s all test and

    +aca"e test. By this standardiation> the code will be !ore readable by others.

    Sa!+le

    +ublic class %llest N

    O

  • 7/25/2019 02.Coding Conventions

    19/163

    1'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    20/163

    20'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2.5. Method

    -RMD001 DonIt !ae a !ethod which has the sa!e na!e with constructor

    Do not !ae the !ethod of the sa!e na!e as the constructor.

    By this standardiation> the code will be !ore readable by others

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ublic BadSa!+leC N +ublic Fi8edSa!+leC N

    O O

    +ublic (oid BadSa!+le Cint nu!ber N +ublic (oid set-u!ber Cint nu!ber N

    O O

    O O

    -RMD002 *se ca+ital letter in !ethod na!e only as a se+arator

    f !ethod na!e consists only one word> use s!all letter. f !ethod na!e is !ade u+ fro! !ulti+le

    words> for the second word and after use ca+ital letter for the first letter of the each word.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic (oid Sa!+le!ethod C N +ublic (oid sa!+leMethod C N

    O O

    -RMD003 *se WPcreateP ob=ect na!eX for !ethod na!e which create ob=ect

    he na!e for !ethod which create ob=ect Cfactory !ethod should start with PcreateP and

    followed by the na!e of ob=ect created by this !ethodBy this standardiation> the code will be !ore readable by others.

    Sa!+le

    +ublic Sa!+le createSa!+le C N

    O

    -RMD00 *se WPtoP ob=ect na!eX for con(erter !ethod na!e

    he na!e for !ethod which con(ert ob=ect to another ob=ect Ccon(erter !ethod should start

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation '

    Moti(e

    78+lanation '

  • 7/25/2019 02.Coding Conventions

    21/163

    21'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    with PtoP> then followed by the na!e of ob=ect after con(ersion

    Sa!+le

    +ublic %nother to%nother C N

    O

    -RMD005 *se WP"etP attribute na!eX for "etter !ethod na!e

    he na!e for !ethod which "et an attribute C"etter !ethod should start with P"etP followed

    by attribute na!e. By this standardiation> the code will be !ore readable by others.

    his is the con(ention of $a(aBeans

    Sa!+le

    +ri(ate Strin" sa!+le-a!eK

    +ublic Strin" "etSa!+le-a!e C N

    return sa!+le-a!eK

    O

    -RMD006 *se WPsetP attribute na!eX for setter !ethod na!e

    he na!e for !ethod which set an attribute Csetter !ethod should start with PsetP followed

    by attribute na!e. By this standardiation> the code will be !ore readable by others.

    his is the con(ention of $a(aBeans

    Sa!+le

    +ri(ate Strin" sa!+le-a!eK

    +ublic (oid setSa!+le-a!e CStrin" na!e N

    sa!+le-a!e J na!eKO

    -RMD00 -a!e for !ethod which returns boolean should re+resent the state of true'false

    *se the na!e by which the state of true'false can be understood for !ethod which returns

    boolean. *se ;uestion for!at which re+resent Yes or -o C78 A is -oun

    By this standardiation> the code will be !ore readable by others.

    Sa!+le

    Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    22/163

    22'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    +ublic boolean is%slee+ C NO

    +ublic boolean canS+eaC NO

    +ublic boolean has78+ired C NO

    +ublic boolean e8ists C NO

    +ublic boolean hasalue C NO

    -RMD00# )onsider antony! in en"lish

    )onsider the sy!!etrical +ro+erty of en"lish word for the na!e of !ethod which its function or

    role +aired. By this standardiation> the code will be !ore readable by others.

    For e8a!+le send and recei(e> to+ and botto!> width and hei"ht

    Sa!+le

    +ublic (oid send CStrin" !essa"e NO

    +ublic Strin" recei(e C NO

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    23/163

    23'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    24/163

    2'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2.6. %r"u!ent

    -R&M001 DonIt use the sa!e na!e for !ethod +ara!eter and instance (ariable

    DonIt use na!e for !ethod ar"u!ent which conflict with class !e!ber. f sa!e na!e is used>

    +ut PRP at the start or use PthisP. By this standardiation> the code will be !ore readable by others.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ri(ate Strin" na!eK +ri(ate Strin" na!eK

    +ublic (oid set-a!e CStrin" na!e N +ublic (oid set-a!e CStrin" Rna!e N

    O O

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    25/163

    25'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    26/163

    26'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2.. ariable in /eneral

    -R%4001 For boolean (ariable> !aes the state of true'false understood

    *se the na!e by which the state of true'false can be understood for boolean (ariable.

    *se ;uestion for!at which re+resent Yes or -o C78 A is -oun

    .By this standardiation> the code will be !ore readable by others.

    7(en if abo(e rule is followed> t does not !ae any sense if the situation does not +ro+erly

    re+resented

    78a!+le A ariable which is used as a fla" whether stoc is e8ist or not

    9 hasStoc

    isStoc

    Sa!+le

    +ri(ate boolean is%slee+K

    +ri(ate boolean canS+eaK

    +ri(ate boolean has78+iredK

    +ri(ate boolean e8istsK

    +ri(ate boolean hasalueK

    -R%4002 )onstant should be static final> use ca+ital letter and use PRP as a se+arator

    )onstant should be declared as static final> and should use ca+ital letter for (ariable na!e. f

    constant na!e is !ade u+ fro! !ulti+le words> use PRP as a se+arator for each word.

    By this standardiation> the code will be !ore readable by others.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le+ublic static final int Sa!+lealue J 10K +ublic static final int S%M&47R%4*7 J 10K

    78+lanation '

    Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    27/163

    2'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    -R%4003 *se na!e which reflect the role of (ariable

    *se na!e which reflect the role of (ariable.

    By this standardiation> the code will be !ore readable by others

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ri(ate Strin" str1K +ri(ate Strin" ser(er-a!eK

    +ri(ate Strin" str2K +ri(ate Strin" client-a!eK

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    28/163

    2#'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.2.#. 4ocal ariable

    -R44001 f the sco+e is s!all> local (ariable na!e can be shortened

    9ri"inally> (ariable na!e should reflect the role of the (ariable. But if the sco+e is s!all> short

    na!e can be used.

    PSco+e is s!allP !eans Pcontains only few line and does not ha(e nested blocP

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    i!+ort =a(a.io.@K i!+ort =a(a.io.@K

    +ublic class Sa!+le N +ublic class Sa!+le N

    +ublic static (oid !ain CStrin"WX ar"s N +ubl ic static (oid !ain CStrin"WX ar"s N

    Strin" str1 J P1 4inePK Strin" inde8 J P1 4inePK

    try N try N

    Strin" str2 J br.read4ineCK Bufferedeader br J new Bufferedeaded

    Syste!.out.+rintln Cstr1K Cnew Fileeader CPtest.t8tPK

    Syste!.out.+rintln Cstr2K Strin" str J br.read4ineCK

    br.closeCK br.closeCK

    O catch C978ce+tion e N O catch C978ce+tion e N

    e.+rintStacrace CK e.+rintStacrace CK

    O O

    O O

    O O

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    29/163

    2'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    -R44002 *se PiP> P=P> PP> L for loo+ counter at each nest in F9 state!ent

    %s loo+ counter in F9 state!ent> use PiP> P=P> PP Cal+habetically sorted for each hierarchy.

    By this standardiation> the code will be !ore readable by others

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    for Cint = J 0K = E 10K = N for Cint i J 0K i E 10K i N

    L L

    for Cint n J 0K n E 10K n N for Cint = J 0K = E 10K = N

    L L

    for Cint t J 0K t E 10 E t N for Cint J 0K E 10 E N

    O O

    O O

    O O

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    30/163

    30'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    31/163

    31'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3. )odin" ules

    %.3.1. /eneral

    )R/-000 *se interface for ob=ect reference

    t is +ossible to declare usin" i!+le!entation class when refer to an ob=ect. ,owe(er> declare

    usin" interface if an a++ro+riate interface of i!+le!entation class e8ists. he ad(anta"e of usin"

    interface is the i!+ro(e!ent of code fle8ibili ty. Ghen declarin" usin" i!+le!entation class> if the

    i!+le!entation class is chan"ed afterwards it is necessary to chan"e all the reference locations.

    9n the other hand> the i!+le!entation class can be chan"ed only by chan"in" the instance "eneration

    +art if we declare by usin" interface

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    i!+ort =a(a.util.@K i!+ort =a(a.util.@K

    +ublic class BadSa!+le N +ublic class Fi8eddSa!+le N

    +ublic static (oid !ain CStrin"WX ar"s N +ublic static (oid !ain CStrin"WX ar"s N

    ector sa!+le4ist J new %rray4istCK 4ist sa!+le4ist J new %rray4istCK

    BadSa!+le badSa!+le J new BadSa!+leCK Fi8edSa!+le f i8edSa!+le J new Fi8edSa!+leCK

    badSa!+le.badMethodCsa!+le4istK fi8edSa!+le.fi8edMethodCsa!+le4istK

    O O

    +ublic (oid badMethod C%rray4ist in+ut N +ublic (oid fi8edMethod C4ist in+ut N

    O O

    O O

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    32/163

    32'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    -)R/-001 Do not use de+recated %&

    Do not use de+recated !ethod or class. f those functions are needed> +lease refer to the

    alternate solution written in $a(aDoc

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    i!+ort =a(a.util.DateK i!+ort =a(a.util.)alendarK

    +ublic class BadSa!+le N +ublic class Fi8eddSa!+le N

    +ublic static (oid !ain CStrin"WX ar"s N +ublic static (oid !ain CStrin"WX ar"s N

    Date sa!+leDate J new DateCK )alendar sa!+le)alendar J )alendar."etnstanceCK Syste!.out.+rintln Csa!+leDate."etYearCK Syste!.out.+rintln Csa!+le)alendar."etC

    )alendar.Y7%K

    O O

    O O

    )R/-002 Do not write unused code

    DonIt you write unused +ri(ate !ethod ' (ariable> or local (ariable f these e8ist> code readability

    will decrease> and waste so!e resources. Delete the one that no lon"er needed.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class BadSa!+le N

    +ublic static (oid !ain CStrin"WX ar"s N +ublic static (oid !ain CStrin"WX ar"s N

    usedMethod CK usedMethod CK

    O O

    +ublic (oid usedMethod C N +ublic (oid usedMethod C N

    O O

    +ublic (oid unusedMethod C N O

    O

    O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    33/163

    33'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )R/-003 Declare with +ro+er !odifier

    *nderstand the !eanin" of access !odifier such as +ri(ate and +ublic> and then declare class>

    !ethod> and (ariable with +ro+er !odifier

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic (oid internalMethod C N +ri(ate (oid internalMethod C N

    O O

    )R/-00 *se PfinalP +ro+erlyDeclare final for so!ethin" that do not chan"e or do not want to chan"e such as class that is

    not inherited> !ethod that is not o(erridden> or (ariable that its (alue does not chan"e Cconstant.

    t will increase code readability.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ublic static int D7F%*4R-7%4 J 60K +ublic static final int D7F%*4R-7%4 J 60K

    O O

    )R/-005 *nderstand the difference between +ri!iti(e ty+e and reference ty+e Cdefinition

    he characteristic of +ri!iti(e ty+e and reference ty+e> !ainly differs in the followin" 3 +oints

    < 78ecution s+eed and resource consu!+tion

    < Default (alue CGhen declarin" instance (ariable

    < Data structure

    he followin" is the e8+lanation of each difference

    < 78ecution s+eed and resource consu!+tion

    Different fro! reference ty+e> +ri!iti(e ty+e does not create new ob=ect when we use it.

    %s a result> ti!e and resource can be sa(ed.

    < Default (alue Cwhen declarin" instance (ariable

    &ri!iti(e ty+e A de+ends on the ty+e

    eference ty+e A null

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    34/163

    3'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    Default (alue for +ri!iti(e ty+e

    byte A Cbyte 0

    short A Cshort 0

    int A 0

    lon" A 04

    float A 0.0f

    double A 0.0dchar A Zu0000I

    boolean A false

    < Data Structure

    Data structure of +ri!iti(e ty+e and reference ty+e is as follows

    &ri!iti(e ty+e A has the (alue itself

    eference ty+e A has the reference of the (alue

    herefore> there is a difference between these ty+es when substitution is +erfor!ed

    &ri!iti(e ty+e A the (alue itself is substituted

    78a!+le

    int i J 5K

    int = J iK

    i and = is different (ariables with the (alue 5.

    he chan"e of does not affect = Cand (ice (ersa

    eference ty+e A he reference of a (alue is substituted

    78a!+le

    nte"er J new nte"er C5

    nte"er l J K

    n the e8a!+le abo(e> l and +oint the sa!e (alue

    he chan"e of will also chan"e l res+ecti(ely C(ice (ersa

    !a"e in the !e!ory

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    35/163

    35'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    Stac ,ea+

    5 '' iIs (alue 5

    &ri!iti(e ty+e 5 '' =Is (alue

    eference ty+e l

    )R/-005 *nderstand the difference between +ri!iti(e ty+e and reference ty+e Car"u!ent

    s the difference of beha(ior between +ri!iti(e ty+e and reference ty+e when those is +assed

    as a +ara!eter of a !ethod understood he (alue is +assed when the ar"u!ent is +ri!iti(e

    ty+e> and the reference to the instance is +assed as a (alue for the reference ty+e.

    herefore> if the state of reference ty+e ar"u!ent is chan"ed in a !ethod> it will cause

    inconsistency when the instance is referred fro! !ulti+le locations.

    Do not !ani+ulate +assed ar"u!ent directly in a !ethod which has reference ty+e or ar"u!ent

    Sa!+le

    i!+ort =a(a.awt.&ointK

    +ublic class Sa!+le N

    +ublic static (oid !ain CStrin"WX ar"s N

    int intalue J 0K

    &oint +oint J new &oint C0> 0K

    Sa!+le sa!+le J new Sa!+le CK

    sa!+le.+rintalueCintalue> +ointK

    sa!+le.!odifyalueCintalue> +ointK

    sa!+le.+rintalueCintalue> +ointK

    O

    +ri(ate (oid !odifyalueCint (alue> &oint +oint N

    (alue J 10K

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    36/163

    36'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    +oint.set4ocationC55> K

    O

    +ri(ate (oid +rintalueCint (alue> &oint +ointN

    Strin"Buffer buffer J new Strin"BufferCK

    buffer.a++endC(alueK

    buffer.a++endCP A PK

    buffer.a++endC+ointK

    Syste!.out.+rintlnCbufferKO

    O

    78ecution esult

    0 A =a(a.awt.&ointW8J0>yJ0X

    0 A =a(a.awt.&ointW8J55>yJX

    (alue is +assed as an ar"u!ent. herefore !ani+ulation within the !ethod will affect the (alue to which

    78a!+le abo(e handles +ri!iti(e ty+e (ariable intValue

    and reference ty+e (ariablepoint

    %s seen fro! the result> before and after the e8ecution of modifyValue()!ethod +ri!iti(e ty+e intValuehas

    not been chan"ed> but reference ty+epointhas been chan"ed

    For +ri!iti(e ty+eintValue> co+y of the (alue is +assed as an ar"u!ent. he !ani+ulation is +erfor!ed to the

    co+y> therefore does not affect intValueitself. 9n the other hand> for reference ty+epoint> reference of the

    pointrefers

  • 7/25/2019 02.Coding Conventions

    37/163

    3'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.2. Metrics

    )RM001 -u!ber of line in a !ethod should be less than 20

    he ideal nu!ber of line in a !ethod > includin" co!!ent> is 20 lines> at !ost 150 lines.

    f the nu!ber of line e8ceeds this (alue> re(iew the desi"n> di(ide !ethod into !ulti+le !ethods

    )RM002 -u!ber of line in a class should be less than 600 lines

    he ideal nu!ber of line in a class> includin" co!!ent> is 600 lines> at !ost 1000 lines.f the nu!ber of line e8ceeds this (alue> re(iew the desi"n> di(ide class into !ulti+le classes

    )RM003 -u!ber of +ublic !ethod in a class should be less than 30.

    -u!ber of +ublic !ethod in a class should be less than 30.

    f the nu!ber of !ethod e8ceeds this (alue> re(iew the desi"n> di(ide class into !ulti+le classes

    )RM00 -u!ber of class in a +aca"e should be less than 10

    he ideal nu!ber of class in a +aca"e is 10> at !ost 20.

    f the nu!ber of class e8ceeds this (alue> re(iew the desi"n> di(ide +aca"e into !ulti+le +aca"es

    )RM005 Do not !ae circulati(e co!+le8ity bi""er.

    he code will be !ore co!+le8 if nu!ber of nested bloc> class'!ethod in(ocation is "reat. f the

    code "ets co!+le8> readability'!aintainability will decrease> and the +ossibility of bu" occurrence

    "ets bi""er.

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    38/163

    3#'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.3. For!at

    )RFM001 *se blan s+ace instead of tab

    Do you use tab for code indent f we use tab and blan s+ace for indent> de+ends on editor

    settin"> the indent can be wron". t will cause the readability of the code decrease.

    *se blan s+ace instead of tab for indent. f you feel troubleso!e to +ut blan s+ace each ti!e>

    do batch con(ersion at the end by usin" the re+lace function of editor

    -ot eco!!ended Sa!+le eco!!ended Sa!+le+ublic class Fi8edSa!+le N +ublic class Fi8edSa!+le N

    +ri(ate int botto! J 5K +ri(ate int botto! J 5K

    +ri(ate int hei"ht J 10K +ri(ate int hei"ht J 10K

    +ri(ate int constant J 2K +ri(ate int constant J 2K

    +ublic int "et%reaC N +ublic int "et%reaC N

    int area J Cbotto! @ hei"ht ' constantK int area J Cbotto! @ hei"ht ' constantK

    return area return area

    O O

    O O

    )RFM002 *se characters of blan s+ace for indent

    *nless there is a s+ecific a"ree!ent> use character for indent.

    By this standardiation> the code will be !ore readable by others

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class Fi8edSa!+le N +ublic class Fi8edSa!+le N

    +ri(ate int botto! J 5K +ri(ate int botto! J 5K

    +ri(ate int hei"ht J 10K +ri(ate int hei"ht J 10K

    +ri(ate int constant J 2K +ri(ate int constant J 2K

    +ublic int "et%reaC N +ublic int "et%reaC N

    int area J Cbotto! @ hei"ht ' constantK int area J Cbotto! @ hei"ht ' constantK

    return area return area

    O O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    39/163

    3'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    O O

    )RFM003 %(oid too lon" line

    4i!it the nu!ber of character in one line to #0 characters. t beco!es hard to see in !onitor>

    causes so!e difficulties in +rintin" and e!ail sendin"> and causes the for!at colla+ses.

    ,ere is the +lace to start new line

    1. Start new line after co!!a

    2. Start new line before o+erator which has low +riority3. Di(ide +rocess into se(eral line by substitutin" (ariable etc

    &ut the startin" +oint of new line in +osition lie eco!!ended Sa!+le 2.

    Sa!+le

    W-ot eco!!ended Sa!+leX

    +ublic (oid lon"4ineMethodC boolean booleanalue> Strin" strin"alue> int intalueN

    O

    Weco!!ended Sa!+leX

    1. Start new line after co!!a

    +ublic (oid fi8edMethodC boolean booleanalue>

    Strin" strin"alue>

    int intalueN

    O

    2. Start new line before o+erator

    double len"th J Math.s;rtCMath.rando!C

    Math.+owCMath.rando!C> 2.0K

    3. Substitute (ariable

    double 8S;uared J Math.+owCMath.rando!C> 2.0K

    double len"th J Math.s;rtCMath.rando!C 8S;uaredK

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    40/163

    0'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RFM00 Do not write any state!ent after PNP

    Do not +ut any state!ent after PNP. t will cause code readability decrease. &ut in new line

    e(en if it is a short state!ent.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ri(ate (oid badSa!+leMethod C N int i J 0K +ri(ate (oid fi8edSa!+leMethod C N

    O int i J 0K

    O

    )RFM005 Grite only one state!ent in one line

    Do not write !ore than one state!ent in a line. )ode readability will decrease. &ut in a new line

    e(en for short state!ent

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic int badSa!+leMethod Cint to+> int botto! N +ublic int fi8edSa!+leMethod Cint to+> int botto! N

    int result J to+ < botto!K return resultK int result J to+ < botto!K

    return resultK

    O O

    )RFM006 &ut blan s+ace after co!!a

    &ut blan s+ace after co!!a. t will i!+ro(e code readability

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic (oid badSa!+leMethodCint score>int nu!berN +ublic (oid fi8edSa!+leMethodCint score> int nu!berN

    O O

    )RFM00 &ut blan s+ace before and after assi"n!ent o+erator CJ> J> ...

    &ut blan s+ace before and after assi"n!ent o+erator CJ> J> .... t will i!+ro(e code

    readability

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    aJ1K a J 1K

    b

  • 7/25/2019 02.Coding Conventions

    41/163

    1'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RFM00# &ut blan s+ace after se!icolon in F9 state!ent

    &ut blan s+ace after se!icolon in F9 state!ent. t will i!+ro(e code readability

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    for Cint i J 0Ki E 1000KiN for Cint i J 0K i E 1000K iN

    O O

    )RFM00 Do not +ut blan s+ace between P

  • 7/25/2019 02.Coding Conventions

    42/163

    2'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RFM011 &ut blan s+ace before and after binary o+erator CP]]P> P^^P

    &ut blan s+ace before and after binary o+erator. t will i!+ro(e code readability

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    if Ca]] bN if Ca ]] bN

    O O

    if Ca ^^bN if Ca ^^ bN

    O Oif Ca^^bN if Ca ^^ bN

    O O

    )RFM012 &ut blan s+ace before and after relational o+erator CPEP> PP> PJP> PEJP> PJJP> PQJP

    &ut blan s+ace before and after relational o+erator. t will i!+ro(e code readability

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    if CaEJ bN if Ca EJ bN

    O O

    if Ca JJbN if Ca JJ bN

    O O

    if CaQJbN if Ca QJ bN

    O O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    3'163&h & = t - M%& % li ti St d d &

  • 7/25/2019 02.Coding Conventions

    43/163

    3'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RFM016 &ut blan s+ace before and after arith!etic o+erator CPP> P

    P@P> P'P> P`P

    &ut blan s+ace before and after arith!etic o+erator. t will i!+ro(e code readability

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    if Ca bN if Ca bN

    O O

    if Ca `bN if Ca `b N

    O Oif Ca'bN if Ca ' bN

    O O

    )RFM013 Do not use +arenthesis in return state!ent

    Do not use unnecessary +arenthesis in return state!ent. n the =a(a s+ecification> return ob=ect

    is only one. t will confuse return state!ent with !ethod if you use a +arenthesis in return

    state!ent> and code readability will decrease. f there is any o+eration that needs +arenthesis>

    finish the o+eration beforehand.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    return Ca bK return a bK

    return Cint CaK return Cint aK

    )RFM01 boolean (ariable is already co!+ared

    DonIt you co!+are boolean (ariable with true Boolean (ariable does not need to be co!+ared

    because (ariable itself shows the result of condition. herefore> co!+arin" with true is

    redundant> and can decrease code readability.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    whileChasStoc JJ true N whileChasStoc N

    O O

    ifChasStoc JJ trueN ifChasStocN

    O O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    78+lanation '

    Moti(e

    '163&has &ro=ect -a!e M%& %++lication Standard &a"e

  • 7/25/2019 02.Coding Conventions

    44/163

    &has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RFM015 ne;uality si"n should be left code readability will i!+ro(e. 78ce+t there is any

    s+ecial +ur+ose> the direction should be left +ointin".

    78ce+tion A

    Ghen co!+arin" with constant> +ut the constant in the ri"ht side

    &ut in the left side for (ariable which is the center of o+eration and a++ears se(eral ti!es in

    the code

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    ifC a E i N ifC a E i N

    Oelse ifC a iN Oelse ifC i E a N

    O O

    78+lanation 'Moti(e

    5'163&has &ro=ect -a!e M%& %++lication Standard &a"e

  • 7/25/2019 02.Coding Conventions

    45/163

    &has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.. )o!!ent

    )R)M001 Grite at least author and (ersion Cclass> +ara!> return and e8ce+tion C!ethod in $a(adoc co!!ent.

    Grite $a(adoc co!!ent for class> !ethod> field as shown below> es+ecially for those which

    declared as +ublic or default. %lso write another ta" if necessary.

    W$a(adoc co!!ent for!atX

    Start with '@@

    Second line and below> start with @> then write the state!ent in $a(adoc ta"

    7nd with @'

    W)lassX

    Hauthor WauthorX

    Describe class creator and those who u+date. Describe each at a ta" if e8ist !ore than one

    H(ersion W(ersionX

    Describe class (ersion

    WMethodX

    H+ara! Wna!eX We8+lanationX

    Describe +ara!eter na!e and its e8+lanation by declaration order

    Hreturn We8+lanationX

    f e8ists> e8+lain the return (alue

    He8ce+tion Wna!eX We8+lanationX

    Grite the e8ce+tion which can be thrown when the !ethod is called and its e8+lanation

    WFieldX

    78+lanation about field

    Describe (ariable na!e as !uch as +ossible. Ghen it is not +ossible to show> write

    e8+lanation as $a(adoc co!!ent

    Sa!+le

    78+lanation 'Moti(e

    6'163&has &ro=ect -a!e M%& %++lication Standard &a"e

  • 7/25/2019 02.Coding Conventions

    46/163

    = ++ "

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    '@@

    @ Sa!+le class for e8+lainin" how to write $a(adoc

    @ H(ersion 1.0

    @ Hauthor suyoshi Gaahara

    @'

    +ublic class $a(adocSa!+le N

    '@@

    @ 78+lanation of sa!+leMethod

    @ H+ara! (alue ar"u!entIs e8+lanation

    @ Hreturn return (alueIs e8+lanation

    @ He8ce+tion Sa!+le78ce+tion e8ce+tionIs e8+lanation

    @'

    +ublic Strin" sa!leMethodCStrin" (alue throws Sa!+le78ce+tionN

    Strin" returnalue J [S%M&47\K

    return returnalueK

    O

    O

    '163&has &ro=ect -a!e M%& %++lication Standard &a"e

  • 7/25/2019 02.Coding Conventions

    47/163

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )R)M002 %s for the co!!ent> write only necessary one concisely

    t beco!es difficult to corect if we write co!!ent !ore than the necessity. herefore we write

    P%s for co!!ent> write only necessary one conciselyP. he followin" is the tric for writin"

    readable co!!ent.

    1. Declare (ariables with undestandable ' !eanin"ful na!e> so we can sa(e trouble for

    e8+lainin" those with co!!ent

    2. Do not write u+dated +erson and u+dated date

    3. f the co!!ent is una(oidably beco!es lon"> write the su!!ary in one line.

    t is not necessary to e8+lain e(ery lo"ic lie the followin" sa!+le.

    )o!!ent is necessary if the lo"ic is co!+le8. )o!+le8 lo"ic has !ulti+e +oints> therefore by di(idin"

    it into se(eral !ethods and addin" co!!ent as !ethodIs =a(adoc> we can !ae a better code

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class Bad)o!!entSa!+le N +ublic class Fi8ed)o!!entSa!+le N

    '@@ '@@

    @ Method to find the su! @ Method to find the su!

    @ @

    @ H+ara! ele!ents %rray of int to find the su! @ H+ara! ele!ents %rray of int to find the su!

    @ Hreturn Su! of array @ Hreturn Su! of array

    @' @'

    +ublic int su!CintWX ele!entsN +ublic int su!CintWX ele!entsN

    '' ariable to store the su! int su! J 0K

    int su! J 0K forCint i J 0K i E ele!ents.len"thK i N

    ''4oo+ to calculate the su! of array su! J ele!entsWiXK

    forCint i J 0K i E ele!ents.len"thK i N O

    '' ae out one ele!ent> calculate the su! return su!K

    su! J ele!entsWiXK O

    O

    '' eturn the result

    return su!K

    O

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    48/163

  • 7/25/2019 02.Coding Conventions

    49/163

    50'163&has &ro=ect -a!e M%& %++lication Standard &a"e

  • 7/25/2019 02.Coding Conventions

    50/163

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.6. )lass

    )R)4S001 Declare as final for class which will not be inherited

    Declare final for class which is not inherited. Below are its ad(anta"es

    1. hat the class inheritance and !ethod o(erridin" donIt e8ist will be notified to the co!+iler>

    so the code is o+ti!ied !ore a++ro+riately

    2. )ode readability will i!+ro(e> because it is

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic final class Fi8edSa!+le N

    O O

    )R)4S002 he order of declarin" the field is the order of +ublic> +rotected> default> and +ri(ate.

    Ghen fields are declared> is the order taen into consideration o ee+ the readability of the

    code> declare the field accordin" the access !odifier

    -ot eco!!ended Sa!+le eco!!ended Sa!+le+ublic class BadSa!+le N +ublic class Fi8eddSa!+le N

    int ranK +ublic int nu!berK

    +ri(ate int a"eK int ranK

    +ublic int nu!berK +ri(ate int a"eK

    O O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    51'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bl - M%& D l t St d d F ti - ) t d 3''2006 B h

  • 7/25/2019 02.Coding Conventions

    51/163

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.. )onstructor

    )R)001 Do not declare +ublic constructor in a class which is not declared as +ublic

    Do not create +ublic constructor for non it beco!es +ossible to do the followin"

    )reate instance dyna!ically by usin" )lass.newnstanceCSet the default (alue of instance (ariable> and when instance is "enerated it will be initialied

    by that (alue.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ri(ate int nu!berK +ri(ate int nu!berK

    +ublic BadSa!+leCint nu!N +ublic Fi8edSa!+leCN

    nu!ber J nu!K thisC10K

    O O

    O +ublic Fi8edSa!+leCint nu!N

    nu!ber J nu!K

    O

    O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    52/163

    53'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    53/163

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RMD003 Desi"n +ublic !ethod such that it will not brea consistency of class.

    Desi"n +ublic !ethod carefully such that it will not brea class consistency

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    )lass below ee+ stoci"htS+eaer and stoc4eft %fter correction> stoci"htS+eaer and stoc4eft

    S+eaer (alues as attributes. f s+eaer is always S+eaer is not accessed se+arately. Both (alues are

    sold in a set of ri"ht'left s+eaer> there is a desi"n chan"ed usin" one !ethod. he assu!+tion that

    +roble! in code below. s+eaer is sold in +air can be e+t.

    +ublic class BadSa!+le N +ublic class BadSa!+le N

    +ri(ate int stoci"htS+eaerK +ri(ate int stoci"htS+eaerK

    +ri(ate int stoc4eftS+eaerK +ri(ate int stoc4eftS+eaerK

    +ublic (oid buyi"htS+eaerCint ;uantityi"ht N +ublic (oid buyi"htS+eaerCint ;uantity N

    stoci"htS+eaer J stoci"htS+eaer J

    stoci"htS+eaer the assu!+tion of s+eaer is sold

    in a +air of left ' ri"ht s+eaer will brea.

    8+ ana onMoti(e

    5'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    54/163

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )RMD00 % !ethod should only ha(e one role

    %renIt !ulti+le +rocess +erfor!ed in a !ethod f different ty+e of !ulti+le +rocess is +erfor!ed in

    a !ethod> t will affect code readability> !aintainability> e8tensibility and reusability.

    Di(ide !ethod based on its function.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    i!+ort =a(a.awt.&ointK

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ublic static (oid !ainCStrin"WX ar"sN +ublic static (oid !ainCStrin"WX ar"sN

    &oint +oint J new &ointC55> K &oint +oint J new &ointC55> K

    BadSa!+le s!+l J new BadSa!+leCK Fi8edSa!+le s!+l J new Fi8edSa!+leCK

    s!+l.switchandYC+ointK s!+l.+rintalueC+ointK

    O s!+l.switchandYC+ointK

    '' Swa+ and dis+lay the (alue s!+l.+rintalueC+ointK

    +ri(ate (oid switchandYC&oint +ointN O

    Strin"Buffer sb Jnew Strin"BufferCK '' Swa+ the (aluesb.a++endCP is PK +ri(ate (oid switchandYC&oint +ointN

    sb.a++endC+oint."etCK double 8 J +oint."etCK

    sb.a++endCPand Y is PK double y J +oint."etYCK

    sb.a++endC+oint."etYCK +oint.set4ocationCy> 8K

    Syste!.out.+rintlnCsbK O

    '' Dis+lay

    double 8 J +oint."etCK +ri(ate (oid +rintalueC&oint +ointN

    double y J +oint."etYCK sb.a++endCP is PK

    +oint.set4ocationCy> 8K sb.a++endC+oint."etCK

    sb.a++endCPand Y is PK

    sb.set4en"thC0K sb.a++endC+oint."etYCK

    sb.a++endCP is PK Syste!.out.+rintlnCsbK

    sb.a++endC+oint."etCK O

    sb.a++endCPand Y is PK O

    78+lanation 'Moti(e

    55'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    55/163

    + y "

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    sb.a++endC+oint."etYCK

    Syste!.out.+rintlnCsbK

    O

    O

    )RMD005 Do not use PthisP as return (alue of !ethod

    &lease reconsider why we should use PthisP as a return (alue of !ethod. s there any s+ecific

    reason here is no ad(anta"e of usin" PthisP as a return (alue. *se (oid or another (alue as

    a return (alue of !ethod

    Ghen a !ethod is in(oed> the in(oer has a reference to that ob=ect. here is no +oint of returnin" PthisP.

    For con(enience there are so!e cases of returnin" PthisP lie the followin" sa!+le. Method in(oin" at +oint 1 can

    be written lie +oint 2.

    1 Sa!+le sa!+le J new Sa!+leCK

    sa!+le.sa!+leMethod1CKsa!+le.sa!+leMethod2CK

    sa!+le.sa!+leMethod3CK

    2. sa!+le.sa!+leMethod1C.sa!+leMethod2C.sa!+leMethod3CK

    But a(oid it because it can cause code readability +roble!

    )RMD006 Do not o(erload !ethod which has the sa!e nu!ber of ar"u!ent

    n !ethodI o(erload> if the nu!ber of ar"u!ents are the sa!e> it not easy to understand which

    !ethod is e8ecuted without checin" the ty+e of ar"u!ent in source code. Source code

    readability will decrease as e8+lanation written below

    78+lanation

    n the followin" sa!+le> three ind of classifyC !ethods are created to distin"uish ob=ect. n !ainC !ethod> we

    create an instance and call classifyC !ethods.

    +ublic class BadSa!+leN

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    56'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    56/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    +ublic Strin" classifyC9b=ect sa!+le9b=ectN

    return P*nnown 9b=ectPK

    O

    +ublic Strin" classifyCStrin" sa!+leStrin"N

    return PStrin"PK

    O

    +ublic Strin" classifyC78ce+tion sa!+le78ce+tionN

    return P78ce+tionPK

    O

    +ublic static (oid !ainCStrin" ar"sWXN

    BadSa!+le badSa!+le J new BadSa!+leCK

    9b=ect sa!+le1 J new 9b=ectCK '' 1

    Strin" sa!+le2 J new Strin"CK ''2

    78ce+tion sa!+le3 J new 78ce+tionCK '' 3

    Syste!.out.+rintlnCbadSa!+le.classifyCsa!+le1K '' 1ISyste!.out.+rintlnCbadSa!+le.classifyCsa!+le2K '' 2I

    Syste!.out.+rintlnCbadSa!+le.classifyCsa!+le3K '' 3I

    O

    O

    he result is

    *nnown 9b=ect

    Strin"

    78ce+tion

    Ge do not now which !ethod is e8ecuted by only seein" line 1I 2I and 3I.

    herefore we need to see line 1> 2> and 3 to chec the instance ty+e.

    5'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    57/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.. )lass Method

    )R)MD001 *se class na!e when in(oe class !ethod

    Do not we use ob=ect na!e when in(oin" class !ethod. *se class na!e when callin" class !ethod

    By followin" this rule> code readability will i!+ro(e.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ublic static (oid sa!+le)lassMethodCNO +ublic static (oid sa!+le)lassMethodCNO

    +ublic (oid sa!+leMethodCN +ublic (oid sa!+leMethodCN

    BadSa!+le ob=ect J new BadSa!+leCK Fi8edSa!+le ob=ect J new Fi8edSa!+leCK

    ob=ect.sa!+le)lassMethodCK Fi8edSa!+le.sa!+le)lassMethodCK

    O O

    O O

    78+lanation 'Moti(e

    5#'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    58/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.10. ariable in /eneral

    )R%4001 Declare only one (ariable in a state!ent

    Do not declare !ulti+le (ariables in a state!ent. t will decrease code readability and !aintain 0 and 1> etc. Ghen it is used as counter in a loo+

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ri(ate intWX sa!+le%rray J new int W10XK +ri(ate static final int %%YRST7 J 10K

    O +ri(ate intWX sa!+le%rray J new int W%%YRST7XK

    O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

    5'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

  • 7/25/2019 02.Coding Conventions

    59/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )R%4003 Declare array as P(ariable ty+eWXP

    &lease standardie the array declaration for! in the code to ee+ the consistency of the code.

    he declaration for! lie the followin" P-ot eco!!ended Sa!+leP see!s to re!ain as

    an influences of ) lan"ua"e.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ri(ate int sa!+le%rrayWX J new intW10XK +ri(ate intWX sa!+le%rray J new intW10XK

    O O

    )R%400 *se local (ariable as far as +ossible

    Ghen (ariable is accessed fre;uently> is the +erfor!ance taen into consideration

    t taes !ore ti!e to access the class (ariable and the instance (ariable co!+ared with the

    access to the local (ariable. &lease use the local (ariable if you fre;uently access the it as far as

    +ossible.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N+ri(ate int resultK +ri(ate int resultK

    +ublic (oid add-u!berCintWX nu!bers N +ublic (oid add-u!berCintWX nu!bers N

    for Cint i J 0K i E nu!bers.len"thK iN int te!+Su! J resultK

    result J nu!bersWiXK for Cint i J 0K i E nu!bers.len"thK i N

    O te!+Su! J nu!bersWiXK

    O O

    result J te!+Su!K

    O

    O O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    60/163

    61'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle % $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    61/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    62'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle % $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    62/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.11. nstance ariable

    )R4001nitialie instance (ariable in constructor> or when declarin" instance (ariable> or in first ti!e "etter !ethod is accessed C4ay initialiation

    here are three !ethods of initialiin" instance (ariable > in constructor> or in instance (ariable

    declaration> or in first ti!e "etter !ethod is accessed.

    7ach !ethodIs ad(anta"es and disad(anta"es are written below.

    W)onstructorX

    %d(anta"es

    1. hread safe

    2. t is +ossible to initialie different (alue for each instance

    Disad(anta"e

    1. here is a +ossibility of for"ettin" initialiation> because (ariable declaration and initialiation

    are +erfor!ed in se+arated location.

    Wnstance ariable DeclarationX

    %d(anta"es

    1. hread safe

    2. nstance "eneration is fast

    Disad(anta"e

    1. nitialiation can not be +erfor!ed for each instance

    WFirst ti!e "etter !ethod is accessedX

    %d(anta"es

    1. nitialiation o+eration can be o(erridden in by o(erridin" "etter !ethod in sub class.

    2. t is not necessary to +erfor! null checin" in a +lace where "et !ethod is called.

    C Because null checin" is +erfor!ed inside "et !ethod.

    3. !+le!entation of delayin" (ariable initialiation with instance "eneration can be achie(ed.

    Disad(anta"es

    1. nconsistency will occur if "et !ethod is called fro! !ulti+le threads at the sa!e ti!e

    2. t taes "reater load because each ti!e "etter !ethod is called> the chec whether (ariable

    78+lanation 'Moti(e

    63'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    63/163

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    is already initialied is +erfor!ed.

    Sa!+le

    Wnitialiation in constructorX

    +ublic class Sa!+le1N

    +ri(ate Strin" na!eK

    +ublic Sa!+le1 N

    na!e J PsuuiP K

    O

    +ublic Strin" "et-a!eC N

    return na!eK

    O

    O

    Wnitialiation on instance (ariable declarationX

    +ublic class Sa!+le2N

    +ri(ate Strin" na!e J PsuuiPK

    +ublic Sa!+le2 N

    O

    +ublic Strin" "et-a!eC N

    return na!eK

    O

    O

    Wnitialiation in the first ti!e "etter !ethod is accessedX

    +ublic class Sa!+le3N

    +ri(ate Strin" na!e K

    +ublic Sa!+le3 N

    O

    +ublic Strin" "et-a!eC N

  • 7/25/2019 02.Coding Conventions

    64/163

    65'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    65/163

    66'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    66/163

    %.3.12. )lass ariable

    )R)4001 Do not use array which is declared as P+ublic static finalP

    DonIt you thin that the ele!ent of array which is declared as final is not !odifiable Ghen

    an array is declared as final> the sie is unchan"eable> but the ele!ent is chan"eable. f you

    want that the ele!ent is unchan"eable> "enerate read code readability will i!+ro(e.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ubl ic static final int S%)R%4*7 J 10K +ublic static final int S%)R%4*7 J 10K

    +ublic (oid sa!+leMethodCN +ublic (oid sa!+leMethodCN

    BadSa!+le ob=ect J new BadSa!+leCK Fi8edSa!+le ob=ect J new Fi8edSa!+leCK

    int localalue Job=ect.S%)R%4*7K int localalue JFi8edSa!+le.S%)R%4*7K

    O O

    O O

    78+lanation 'Moti(e

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    67/163

  • 7/25/2019 02.Coding Conventions

    68/163

  • 7/25/2019 02.Coding Conventions

    69/163

    0'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    70/163

  • 7/25/2019 02.Coding Conventions

    71/163

  • 7/25/2019 02.Coding Conventions

    72/163

    3'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    73/163

    +arentnstance.sa!+leMethod CKO

    O

    )lass which inherit su+er class

    +ublic class )hild e8tends &arent N

    +ublic (oid sa!+leMethodCN

    Syste!.out.+rintlnCP)hildA !ethodPK

    O

    O

    f !ethod o(erridin" is +erfor!ed correctly> when the !ethod which its ar"u!ent defined as su+er class ty+e is

    in(oed> auto!atically the !ethod in subclass is called.

    herefore> is abo(e +ro"ra! is e8ecuted> )hild class !ethod sa!+leMethod is in(oed because &arent)lass

    !ethod sa!+leMethod is o(erridden in )hild class. he result is lie the followin".

    )hildA !ethod

    9n the other hand> !ethod which is declared as +ri(ate in su+er class can not be o(erridden. t will not cause

    any error> but is treated as different !ethod.

    f sa!+leMethod in &arent)lass is declared as +ri(ate> the result is A

    &arentA !ethod

    Because !ethod is not o(erriden> sa!+leMethod of &arent)lass is called. t will be the cause of bu" if we chan"e

    it carelessly

  • 7/25/2019 02.Coding Conventions

    74/163

    5'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    75/163

    See!in"ly> if !a+."etCnew D-u!berC123 is e8ecuted for this ,ashMa+> it see!s to be able to"et P,anaoP.,owe(er > the returnin" (alue is null.

    Gith the default hash)odeC !ethod> e(en if id (alue is the sa!e> when new hash is created another hash

    code is "i(en to that instance. n the e8a!+le abo(e> new instance is created when "ettin" the content

    of ,ashMa+. herefore> different hash code is "i(en e(en if id (alue is the sa!e> and i t is reco"nied as

    another instance of D-u!ber . o a(oid this +roble!> o(erride hash)odeC !ethod.

    9(erride hasd)odeC !ethod carefully. he sa!e instance !ust return sa!e hash code. Below is the e8a!+le of

    hash code !ethod.

    +ublic int hash)odeC N

    int result J 13K '' +ri!e nu!ber

    result J 11 @ result idK

    return resultK

    O

    Ge "a(e an e8a!+le to i!+le!ent hash)odeC !ethod. here are !any ways of i!+le!entin" this !ethod>

    By o(erridin" hash)odeC !ethod and i!+le!entin" it to return correct (alue> we can "et correct result when usin"

    collection class based on hash.

    )R,005 !+le!ent toStrin"C !ethod as far as +ossible

    ,a(e you checed that you obtain the (alue e8+ected usin" toStrin"C !ethod he default of

    9b=ect.toStrin"C !ethod returns the (alue obtained by hash)odeC. t is necessary to o(erride

    toStrin"C !ethod to "et !ore easy

  • 7/25/2019 02.Coding Conventions

    76/163

    '163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    77/163

    %.3.15. nstance

    )RS001 *se e;ualsC !ethod to co!+are ob=ects

    DonIt you use PJJP to co!+are ob=ects f we use PJJP> we are not co!+arin" if the ob=ects (alue

    is sa!e> but co!+arin" if they are the sa!e instances or not. *se e;ualsC !ethod to co!+are

    ob=ects (alue.

    he default of e;ualsC !ethod is si!+ly PJJP co!+arison. hen the suitable co!+arison of each

    ob=ect is i!+le!ented by o(erridin" this !ethod. For instance> to co!+are whether the character

    strin" (alue stored in Strin" is e;ual> the e;ualsC !ethod of the Strin" class is i!+le!ented.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ublic static (oid co!+areC Strin" left> Strin" ri"htN +ublic static (oid co!+areC Strin" left> Strin" ri"htN

    ifC left JJ ri"ht N ifC left.e;ualsCri"ht N

    Syste!.out.+rintlnC Phey are e;ual.PK Syste!.out.+rintlnC Phey are e;ual.PK

    O else N O else N

    Syste!.out.+rintlnC Phey are -9 e;ual.PK Syste!.out.+rintlnC Phey are -9 e;ual.PK

    O O

    O O

    O O

    78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    78/163

  • 7/25/2019 02.Coding Conventions

    79/163

  • 7/25/2019 02.Coding Conventions

    80/163

    #1'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    int i J 0K int sie J arrayStr.len"thK

  • 7/25/2019 02.Coding Conventions

    81/163

    for CK i E 8K N forCint i J 0K i E sieK iN

    iK Syste!.out.+rintlnCarrayStrWiXK

    O O

    O O

    O

    f there is no s+ecial reason> use for state!ent for

    incre!entin" nu!ber. Ghen incre!ent is not necessary> use while state!ent.

    +ublic class %notherBadSa!+leN i!+ort =a(a.util.Strin"oenierK

    (oid !ethodCint 8N +ublic class Ghile4oo+Sa!+leN

    int i J 0K (oid sa!+leMethodCStrin" tar"etN

    while Ci E 8 N '' For state!ent is suitable Strin"oenier stroen J

    iK new Strin"oenierCtar"etK

    O whileCstroen.hasMoreoensCN

    O Syste!.out.+rintlnC stroen.ne8toenCK

    O O

    O

    *se while state!ent lie Peco!!ended Sa!+leP O

    if incre!ent is not necessary.

    (oid sa!+leMethodCStrin" tar"etN

    Strin"oenier stroen J

    Strin"oenierCtar"etK

    forCint iJ0Kstroen.hasMoreoensCKiN

    Syste!.out.+rintlnC stroen.ne8toenCK

    O

    O

    #2'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    )R)005 Do not chan"e the (alue of loo+ (ariable in for state!ent.

  • 7/25/2019 02.Coding Conventions

    82/163

    snIt the counter of for state!ent chan"ed in the loo+ he counter of for state!ent should not be

    chan"ed outside the conditional e8+ression. t beco!es easy to !ae !istae and difficult to find

    it because control structure is difficult to understand.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+leN +ublic class Fi8edSa!+leN

    int badSa!+leMethodCN int fi8edSa!+leMethodCN

    int result J 0K int result J 0K

    for Cint i J 0K i E 100K iN for Cint i J 0K i E 50K iN

    i J 1K result J C2 @ i 1K

    result J iK O

    O return resultK

    return resultK O

    O O

    O

    )R)006 he counter of the for state!ent is started fro! 0.

    &lease "i(e 0 as an initial (alue of the counter used in the for state!ent unless there is a s+ecial

    reason to use another initial (alue. For instance> code readability will dro+ if startin" (alue other

    than 0 is used in for state!ent when accessin" !e!bers of array.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+leN +ublic class Fi8edSa!+le N

    +ublic static (oid !ainCStrin"WX ar"sN +ublic static (oid !ainCStrin"WX ar"sN

    intWX test%rray J new intW10XK intWX test%rray J new intW10XK

    for Cint i J 1K i EJ 10K iN for Cint i J 1K i EJ 10K iN

    test%rrayWi

  • 7/25/2019 02.Coding Conventions

    83/163

    i!+ro(e code readability.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+leN +ublic class Fi8edSa!+leN

    +ublic static (oid !ainCStrin"WX ar"sN +ublic static (oid !ainCStrin"WX ar"s N

    BadSa!+le sa!+le J new BadSa!+leCK Fi8edSa!+le sa!+le J new Fi8edSa!+leCK

    sa!+le.use)ontinueCK sa!+le.un*se)ontinueCK

    sa!+le.useBreaCK sa!+le.un*seBreaCK

    O O

    +ri(ate (oid use)ontinueCN +ri(ate (oid un*se)ontinueCN

    int si+-u!ber J 5K int si+-u!ber J 5K

    for Cint i J 0K i E 10K iN for Cint i J 0K i E 10K iN

    if CCi 1 JJ si+-u!berN if CCi 1 QJ si+-u!berN

    continueK Syste!.out.+rintlnCi 1 K

    O O

    Syste!.out.+rintlnCi 1K O

    O O

    O

    +ri(ate (oid useBreaCN +ri(ate (oid un*seBreaCN

    int li!it-u!ber J 5K int li!it-u!ber J 5K

    for Cint i J 0K i E 10K iN for Cint i J 0K Ci 1 EJ li!it-u!berKiN

    if Cli!it-u!ber E Ci 1 N Syste!.out.+rintlnCi 1K

    breaK O

    O O

    Syste!.out.+rintlnCi 1 K O

    O

    O

    78+lanation 'Moti(e

    if breakand continueare used to control +rocessin" in the loo+> control structure will "et co!+le8

    and code readability will dro+. Mostly of such lo"ic can be written without usin" breakor continue

    &lease use neither breaknor continueas !uch as +ossible to si!+lify control structure and

  • 7/25/2019 02.Coding Conventions

    84/163

  • 7/25/2019 02.Coding Conventions

    85/163

  • 7/25/2019 02.Coding Conventions

    86/163

    #'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    O O

  • 7/25/2019 02.Coding Conventions

    87/163

    W78ce+tionX

    f we want to e8ecute +rocess until the end of the loo+ e(en if e8ce+tion is occurred.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+leN +ublic class Fi8edSa!+leN+ublic (oid !ethodCStrin"WX strN +ublic (oid !ethodCStrin"WX strN

    int sie J str.len"thK int sie J str.len"thK

    forCint i J 0K i E sieK iN try N

    try N forCint i J 0K i E sieK iN

    int nu! J nte"er.+arsentCstrWiXK int nu! J nte"er.+arsentCstrWiXK

    so!e9therMethodCnu!K so!e9therMethodCnu!K

    O O

    catchC-u!berFor!at78ce+tion e N catchC-u!berFor!at78ce+tion e N

    e.+rintStacraceCK e.+rintStacraceCK

    O O

    O O

    O +ri(ate (oid so!e9therMethodCint iN

    +ri(ate (oid so!e9therMethodCint iN O

    O O

    O

    )R)011 Do not use trybloc in re+etiti(e +rocess.

    78+lanation 'Moti(e

    Do not +ut try/catchbloc in the loo+. t will lead to the decrease of +erfor!ance> and +rocess in

    the loo+ will be difficult to see. Do try/catchoutside loo+> e8ce+t for any s+ecial reason.

  • 7/25/2019 02.Coding Conventions

    88/163

    #'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.1. hread

  • 7/25/2019 02.Coding Conventions

    89/163

    here are two !ethods of i!+le!entin" thread

    %do+t the !ethod of i!+le!entin" unnable interface to create thread. For instance> we ha(e

    +ro"ra! to !o(e adio )ontrolled )ar. )ontroller ob=ect and )ar ob=ect run in different thread>

    and these ob=ect !ust be declared as )ontroller class with the function of controller> and)ar class with the function of car. hese classes !ay ha(e thread function> but should not be

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    '' )lass below is subclass of hread '' )lass below is subclass of )ontroller class which

    +ublic class BadSa!+le)ontrollere8tends hreadN has thread function

    +ri(ate )ar carK +ublic class Fi8edSa!+le)ontroller e8tends )ontroller

    +ublic BadSa!+le)ontroller C)ar RcarN i!+le!ents unnableN

    car J RcarK +ri(ate )ar carK

    O +ublic Fi8edSa!+le)ontroller C)ar RcarN

    +ublic (oid runCN car J RcarK

    car.start7n"ineCK O

    ''L +ublic (oid runCN

    O car.start7n"ineCK

    O ''L

    O

    O

    )RD001 !+le!ent Runnablefor thread

    78+lanation '

    Moti(e

    1. )reate subclass of Thread

    2. !+le!ent Runnableinterface

    declared as subclass of Thread.

    $a(a does not su++ort !ulti+le inheritances. f those classes inherit Threadclass> they cannot

    inherit another class. Do not inherit Thread class e8ce+t we want to e8tend basic function of

    ThreadCother than run() !ethod

  • 7/25/2019 02.Coding Conventions

    90/163

    1'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    +ublic class hreadBadSa!+le N +ublic class hreadFi8edSa!+le N

    +ri(ate 4oc9b=ect locK +ri(ate 4oc9b=ect locK

  • 7/25/2019 02.Coding Conventions

    91/163

    +ublic (oid lon"&rocessCN +ublic (oid lon"&rocessCN

    synchroniedClocN synchroniedClocN

    hread.yieldCK try N

    O

    O O catch Cnterru+ted78ce+tion e N

    O e.+rintStacraceCK

    OO

    O

    O

    Do not call !ethod which has synchronied bloc fro! synchronied bloc. You should consider

    that there is a +ossibility of deadloc occurs. here is a +ossibil ity of deadloc occurs if

    the followin" conditions are !et A

    1. here are !ulti+le loc ob=ects

    2. /o to loc another ob=ect without releasin" current loc ob=ect

    3. he loc order of ob=ect is undecided.

    he +rocess of callin" a !ethod which has synchronied bloc fro! synchronied bloc !eans

    "o to loc another ob=ect while ha(in" another loc ob=ect without releasin" it. t has already !et

    1 and 2 conditions. n addition of these> if loc order of ob=ect is undecided> deadloc can easily

    occur. f the order of loc resources is not strictly set> do not call a !ethod which has

    synchronied bloc fro! synchronied bloc.

    Deadloc is a state where two threads e8ist and each other is waitin" for the other thread to release the ob=ect

    they loc.

    For e8a!+le> assu!e that there are two coos. o wor> both need nife and cuttin" board> but there are only

    one nife and one cuttin" board. )oo % too the nife and coo B too the cuttin" board. But they cannot +erfor!

    loc.waitCK

    )RD00 Do not call a !ethod which has synchronied bloc fro! synchronied bloc

    78+lanation 'Moti(e

    78+lanation

  • 7/25/2019 02.Coding Conventions

    92/163

  • 7/25/2019 02.Coding Conventions

    93/163

  • 7/25/2019 02.Coding Conventions

    94/163

    5'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    +ublic class obot i!+le!ents unnableN +ublic class obot i!+le!ents unnableN

    +ri(ate static byteWX co!!andsK +ri(ate static byteWX co!!andsK

    +ri(ate obot)ontroller controllerK +ri(ate obot)ontroller controllerK

  • 7/25/2019 02.Coding Conventions

    95/163

    +ri(ate obot)ontroller controllerK +ri(ate obot)ontroller controllerK

    +ublic (oid runCN +ublic (oid runCN

    synchroniedCcontrollerN synchroniedCcontrollerN

    while Cco!!ands JJ nullN while Cco!!ands JJ nullN

    tryN tryN

    hread.slee+C10000K controller.waitCKOcatchCnterru+ted78ce+tion eN OcatchCnterru+ted78ce+tion eN

    e.+rintStacraceCK e.+rintStacraceCK

    O O

    O O

    int sie J co!!ands.len"thK int sie J co!!ands.len"thK

    forCint i J 0K i E sieK iN forCint i J 0K i E sieK iN

    this.+rocess)o!!andCco!!andsWiXK this.+rocess)o!!andCco!!andsWiXK

    O O

    co!!ands J nullK co!!ands J nullK

    O O

    O O

    +ublic (oid store)o!!andsCbyteWX co!!andsN +ublic (oid store)o!!andsCbyteWX co!!andsN

    this.co!!ands J co!!andsK this.co!!ands J co!!andsK

    O O

    +ri(ate (oid +rocess)o!!andCbyte co!!andN +ri(ate (oid +rocess)o!!andCbyte co!!andN

    '' L '' L

    O O

    O O

    6'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    +ublic class obot)ontrollerN +ublic class obot)ontrollerN

    O O

  • 7/25/2019 02.Coding Conventions

    96/163

    n while state!ent> if +rocess below is e8ecuted> the n this sa!+le> slee+C is !odified to waitC. f the con

    condition to !o(e to the ne8t +rocess can be satisfied. dition of while state!ent is not satisfied> waitC will be

    obot.store)o!!andsCco!!andsK e8ecuted. f notify%llC is called after condition is

    ,owe(er> until this +rocess is +erfor!ed> e(ery 10000 satisfied> waitin" controller will be resu!ed.

    !s C10 s the condition in while state!ent will be obot.store)o!!andsCco!!andsK

    e(aluated and slee+C each ti!e. 7(en if the condition controller.notify%llCK

    is satisfied> it can not !o(e to the ne8t +rocess until

    slee+C is o(er.

    )RD00 %++ly synchroniation Csynchronied only in necessary +art

    here are two !ethods to +erfor! synchroniation> the first is to declare synchronied !ethod>

    and the second is to use synchronied bloc.

    W1. Synchronied !ethodX W2. Synchronied blocX

    synchronied (oid !ethodC N (oid !ethodC N

    L synchronied CEinstance N

    O L

    O

    O

    he instance to tae the loc is written in Einstance +art of 2. Synchronied bloc.

    hen> of what 1.synchronied !ethod taes the loc he !ethod 1 has the e;ual !eanin"

    with the followin"

    (oid !ethodC N

    synchronied Cthis N

    L

    O

    O78+lanation 'Moti(e

  • 7/25/2019 02.Coding Conventions

    97/163

    #'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

  • 7/25/2019 02.Coding Conventions

    98/163

    '163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )reated 3''2006 By e"uh

    Docu!ent -a!e )odin" )on(entions itle %. $a(a )odin" )on(ention *+dated By

    %.3.1#. Strin" Mani+ulation

    )RS001 *se equals( !ethod to co!+are strin"s

  • 7/25/2019 02.Coding Conventions

    99/163

    Do you use PJJP or PQJP o+erator for co!+arin" strin"s f those o+erators are used> it does not

    co!+are if strin"s are the sa!e character strin" or not> but co!+are if strin"s are the sa!e

    will be sa!e because co!+iler will o+ti!ie those to the sa!e instance. ,owe(er> follow this rule

    in code.

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class BadSa!+le N

    +ublic boolean co!+areCStrin" na!e> +ublic boolean co!+areCStrin" na!e>

    Strin" another-a!eN Strin" another-a!eN

    return na!e JJ another-a!eK return na!e.e;ualsCanother-a!eK

    O O

    O O

    $a(a M in each case instance will be "enerated> and it will consu!e e8tra !e!ory. t is not

    -ot eco!!ended Sa!+le eco!!ended Sa!+le

    +ublic class BadSa!+le N +ublic class Fi8edSa!+le N

    +ri(ate Strin" sa!+leStrin" Jnew Strin" C[4iteral\K +ri(ate Strin" sa!+leStrin" J [4iteral\K

    +ublic (oid +rintC N +ublic (oid +rintC N

    Syste!.out.+rintlnCsa!+leStrin"K Syste!.out.+rintlnCsa!+leStrin"K

    O O

    O O

    78+lanation 'Moti(e

    instance or not. herefore> use equals()!ethod of Strinclass to co!+are strin"s.

    n case of literal> if the content is the sa!e> the result of co!+arison usin" PJJP or usin" equals()

    )RS002 Do not use newfor literal

    78+lanation 'Moti(e

    s newused when creatin" Strinob=ect to store strin" constant f newis used> de+ends on

    necessary to use new for strin" constant.

  • 7/25/2019 02.Coding Conventions

    100/163

    101'163&has &ro=ect -a!e M%& %++lication Standard &a"e

    Bloc -a!e M%& De(elo+!ent Standard Function -a!e )r