0% found this document useful (0 votes)
38 views

JAVA Coding Standard: Krypto Team

This document outlines coding standards for the Krypto team Java project. It covers naming conventions for packages, classes, variables, methods and constants. It specifies formatting conventions for comments, indentation, whitespace and declarations. It also describes conventions for statements including simple, compound, return, if/else, for, while, do-while, switch, try-catch and exceptions. The goal is to make the code consistent, readable, understandable and maintainable.

Uploaded by

vupadhyay1983
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

JAVA Coding Standard: Krypto Team

This document outlines coding standards for the Krypto team Java project. It covers naming conventions for packages, classes, variables, methods and constants. It specifies formatting conventions for comments, indentation, whitespace and declarations. It also describes conventions for statements including simple, compound, return, if/else, for, while, do-while, switch, try-catch and exceptions. The goal is to make the code consistent, readable, understandable and maintainable.

Uploaded by

vupadhyay1983
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

Carnegie Mellon University

School of Computer Science Master of Software Engineering

CMU-MSE-KRYPTO-CS1.1 Summer 2001

Krypto Team JAVA Coding Standard


Version 1.1 June 11, 2001

Pisey Huy Grace Lewis Ming-hsun Liu

Document Revisions
Rev. 1.1 Date 06/11/2001 ut!or"s# Grace A. Lewis Comment Extended exception handling section.

CMU-MSE-KRYPT -CS-1.1

ii

Table of Contents
T $%E O& CO'TE'TS 1 ('TRODUCT(O' ((( 1

1.1 PURPOSE.......................................................................................................................................................1 1.2 SCOPE...........................................................................................................................................................1 1.) RE&ERE'CES................................................................................................................................................1 2 ' M('* CO'+E'T(O'S 2

2.1 P CK *ES 'D &(%ES.................................................................................................................................) 2.2 CO'ST 'TS..................................................................................................................................................) ) &ORM TT('* CO'+E'T(O'S ,

).1 C% SS &(%E STRUCTURE............................................................................................................................., ).2 COMME'TS.................................................................................................................................................., 3.2.1 COMME !"# CLA"" $EA%E&.....................................................................................................................' 3.2.2 COMME !"# ME!$O% $EA%E&.................................................................................................................' 3.2.3 COMME !"# ()LE $EA%E&.........................................................................................................................6 ).) ('DE'T T(O'...............................................................................................................................................)., ./(TE SP CE.............................................................................................................................................3.*.1 +LA , L) E".............................................................................................................................................6 3.*.2 +LA , "-ACE"........................................................................................................................................... , ST TEME'TS 0

,.1 S(MP%E ST TEME'TS..................................................................................................................................0 ,.2 COMPOU'D ST TEME'TS...........................................................................................................................0 ,.) RETUR' ST TEME'T..................................................................................................................................0 ,., (&1 (&-E%SE1 (& E%SE-(& E%SE ST TEME'TS.................................................................................................0 ,.2 &OR ST TEME'TS........................................................................................................................................3 ,.- ./(%E ST TEME'TS...................................................................................................................................3 ,.4 DO-./(%E ST TEME'TS.............................................................................................................................3 ,.0 S.(TC/ ST TEME'TS................................................................................................................................10 ,.3 TRY-C TC/ ST TEME'TS.........................................................................................................................10 ,.10 E5CEPT(O' ST TEME'TS.......................................................................................................................11 *.10.1 !$&O/) G A E0CE-!)O ....................................................................................................................11 *.10.2 CA!C$) G A E0CE-!)O .....................................................................................................................11 2 DEC% R T(O'S 12

2.1 'UM$ER PER %('E....................................................................................................................................12 2.2 ('(T( %(6 T(O'.........................................................................................................................................12 2.) P% CEME'T...............................................................................................................................................12 2., C% SS 'D ('TER& CE DEC% R T(O'S .................................................................................................1)

CMU-MSE-KRYPT -CS-1.1

iii

- CODE E5 MP%E

1,

CMU-MSE-KRYPT -CS-1.1

i1

1 Intro uction
!his doc23ent contains the 4a1a coding standard that the ,r5pto tea3 will 6ollow d2ring the de1elop3ent o6 etscape %ata7ase ,e5store 8ersion 2.0 9 %+" 2.0:. !his coding standard sho2ld 7e 2pdated a6ter each !"- c5cle; or as needed. Code inspection res2lts are expected to 7e a good so2rce o6 additional 3aterial 6or this doc23ent.

1.1

Purpose

Coding con1entions help ens2re that pro<ect code has a consistent str2ct2re and st5le. !he5 are intended to 3a=e the code easier to read; 2nderstand; re1iew; and 3aintain. !his doc23ent is intended 6or the 6ollowing 2ses# %es=>side re6erence 6or %+" 2.0 de1elopers d2ring coding "o2rce 6or code re1iew chec=lists &e6erence 6or de1elopers who 32st extend and 3aintain the %+" 2.0 pro<ect

1.2

Scope
a3ing con1entions 6or pro<ects; 6iles; o7<ects; 1aria7les; and other code constr2cts (or3atting con1entions 6or code 3od2les and their co33ents Error handling con1entions Coding practices and reco33endations

!his doc23ent descri7es the 6ollowing 6or the ,r5pto -ro<ect#

1.3

References
Coding Con1entions 6or the 4a1a -rogra33ing Lang2age; http#//<a1a.s2n.co3/docs/codecon1/ht3l/CodeCon1!OC.doc.ht3l Gr5phon Coding "tandard 1ersion 0.* 4a1a Coding "tandard 8ersion 2.0 o6 the co3p2ter science depart3ent at &)!; http#//www.cs.rit.ed2/?65</<a1a> coding>standard.ht3l 4a1a%oc !ool; http#//<a1a.s2n.co3/<2se/<a1adoc/index.ht3l

!he 6ollowing so2rces were 2sed in creation o6 the original 1ersion o6 this standard.

CMU-MSE-KRYPT -CS-1.1

! "aming Con#entions
Meaning62l na3es are i3portant 6or 3aintaining 2nderstanda7le; high>@2alit5 code. %e1elopers sho2ld ta=e the ti3e to choose na3es that are descripti1e and 2na37ig2o2s. Except 6or 2ser>de6ined constants; all identi6ier na3es sho2ld 7e written as a 3ix o6 2pper and lower case letters. ,r5pto 2ses the 6ollowing r2les 6or 6or3atting identi6ier na3es#

(7enti8ier t9:e 8aria7les

-ac=ages

Classes

'amin; ru<es 8aria7les are in 3ixed case with a lowercase 6irst letter. )nternal words start with capital letters. 8aria7le na3es sho2ld not start with 2nderscore ABC or dollar sign ADC characters; e1en tho2gh 7oth are allowed. 8aria7le na3es sho2ld 7e short 5et 3eaning62l. !he choice o6 a 1aria7le na3e sho2ld 7e 3ne3onic> that is; designed to indicate to the cas2al o7ser1er the intent o6 its 2se. One> character 1aria7le na3es sho2ld 7e a1oided except 6or te3porar5 Ethrowawa5E 1aria7les. Co33on na3es 6or te3porar5 1aria7les are i, j, k, m, and n 6or integersF c; d; and e 6or characters. !he pre6ix o6 a 2ni@2e pac=age na3e is alwa5s written in all>lowercase A"C)) letters and sho2ld 7e one o6 the top> le1el do3ain na3es; c2rrentl5 co3; ed2; go1; 3il; net; org; or one o6 the English two>letter codes identi65ing co2ntries as speci6ied in )"O "tandard 3166; 1GH1. "27se@2ent co3ponents o6 the pac=age na3e 1ar5 according to an organiIationJs own internal na3ing con1entions. "2ch con1entions 3ight speci65 that certain director5 na3e co3ponents 7e di1ision; depart3ent; pro<ect; 3achine; or login na3es. !he pre6ix 6or an %+" pac=age will 7e edu.cmu.sei.cbs.ndbs. Class na3es sho2ld 7e no2ns; in 3ixed case with the 6irst letter o6 each internal word capitaliIed. !r5 to =eep 5o2r class na3es si3ple and descripti1e. Kse whole words>a1oid acron53s and a77re1iations 92nless the a77re1iation is 32ch 3ore widel5 2sed than the long 6or3; s2ch as K&L or $!ML:.

E=am:<e
int char float i; c; myWidth;

edu.cmu.sei.cbs.ndbs

class Raster; class ImageSprite;

CMU-MSE-KRYPT -CS-1.1

Constants 92ser de6ined: Methods

CapitaliIe all letters in each word. Kse 2nderscores in place o6 spaces. Methods sho2ld 7e 1er7s; in 3ixed case with the 6irst letter lowercase; with the 6irst letter o6 each internal word capitaliIed. )nter6ace na3es sho2ld 7e capitaliIed li=e class na3es.

static final int MIN WI!"# $ %; run&'; run(ast&';

)nter6aces

interface Storing;

2.1

Packages and Files

!his section descri7es the na3ing con1entions 6or de1elop3ent 6iles created in the 4a1a pro<ect. !hese 6iles will 7e na3ed according to the 6ollowing r2les# &i<e t9:e "o2rce 6iles Co3pressed pac=age 6iles 'amin; ru<es !he sa3e as the class na3e in the 6ile. a3e sho2ld 7e capitaliIed li=e class na3es. E=am:<e #ash.ja)a #ash.jar

!he 6ollowing 6ile extensions will 7e 2sed 6or all ,r5pto 6iles. &i<e t9:e 4a1a so2rce 6ile 4a1a class 6ile 4a1a co3pressed pac=age 6ile E=tension ja)a class jar

2.2

Constants
Kse the static =e5word to declare constants. Kse constants to a1oid hard>coded 1al2es in the code. (or 2ser>de6ined constants; 2se 2ppercase letters and replace spaces with 2nderscores to 6or3at the na3e o6 the constant. static String !SN N*M+ $ ,-rypto cafe,;

String str!SN;

str!SN $ !SN N*M+;

CMU-MSE-KRYPT -CS-1.1

$ %ormatting Con#entions
!he length o6 4a1a so2rce lines; incl2ding co33ents; sho2ld not exceed H0 characters. Kse li7eral a3o2nts o6 white space to i3pro1e code reada7ilit5. /hen a co3plete state3ent or an expression will not 6it on a single line; 7rea= it according to these general principles# 1. 2. 3. *. '. +rea= a6ter a co33a. +rea= 7e6ore an operator. -re6er higher>le1el 7rea=s to lower>le1el 7rea=s. Align the new line with the 7eginning o6 the expression at the sa3e le1el on the pre1io2s line. )6 the a7o1e r2les lead to con62sing code or to code thatJs all 2p against the right 3argin; indent one additional le1el.

3.1

Class File Structure

!he ele3ents o6 a class or inter6ace 6ile sho2ld appear in the 6ollowing order# 1. 2. 3. *. Class 9static: 1aria7les )nstance 1aria7les Constr2ctors Methods gro2ped 75 62nctionalit5

3.2

Comments

!he 6or3atting 6or co33ents will 6ollow the re@2ire3ents o6 the 4a1a%oc !ool. A doc23entation co33ent is 3ade 2p o6 two parts >> a description 6ollowed 75 Iero or 3ore tags; with a 7lan= line 9containing a single asteris= ELE: 7etween these two sections# .// / "his is the description part of a doc comment / / 0tag 1omment for the tag /. !he 6irst line is indented to line 2p with the code 7elow the co33ent; and starts with the 7egin>co33ent s537ol 9/LL: 6ollowed 75 a ret2rn. "27se@2ent lines start with an asteris= L. !he5 are indented an additional space so the asteris=s line 2p. A space separates the asteris= 6ro3 the descripti1e text or tag that 6ollows it. )nsert a 7lan= co33ent line 7etween the description and the list o6 tags; as shown. )nsert additional 7lan= lines to create E7loc=sE o6 related tags. !he last line 7egins with the end>co33ent s537ol 9L/: indented so the asteris=s line 2p and 6ollowed 75 a ret2rn. ote that the end>co33ent s537ol contains onl5 a single asteris= 9L:.

CMU-MSE-KRYPT -CS-1.1

A 7lan= line sho2ld precede and 6ollow the co33ent 7loc=.

!.2.1 Co""ents# C$ass %eader


!he 6ollowing co33ents will appear 7e6ore the de6inition o6 e1er5 class. .// * A description of what the class does. * * @author Author name * @author Contributor 1 * @author Contributor 2 . . * @author Contributor n */

!.2.2 Co""ents# Met&od %eader


!he 6ollowing co33ents will appear 7e6ore e1er5 3ethod 9incl2ding 3ain:. .// / * description of 2hat the method does / / 0param name description / 0param name description . . / 0param name description / / 0return description / / 0e3ception name description / 0e3ception name description . . / 0e3ception name description / / 45R+6 / preconditions / 4.5R+6 / / 457S"6 / postconditions / 4.57S"6 / /. %e6inition o6 co33ents Mpara3 !he Mpara3 tag is 6ollowed 75 the na3e 9not t5pe: o6 the para3eter; 6ollowed 75 a description o6 the para3eter. Additional spaces sho2ld 7e inserted 7etween the na3e and description so that co33ents line 2p in a 7loc=. %ashes or other p2nct2ation sho2ld not 7e inserted 7e6ore the description. !he na3e alwa5s starts

CMU-MSE-KRYPT -CS-1.1

'

with a lowercase letter. !he description is 3ost 2s2all5 a phrase; starting with a lowercase letter and ending witho2t a period; 2nless it contains a co3plete sentence. Mret2rn !he Mret2rn tag is 6ollowed 75 a description o6 the ret2rn 1al2e. /hene1er possi7le; detailed in6or3ation 9s2ch as ret2rns >1 when an o2t>o6>7o2nds arg23ent is s2pplied: sho2ld 7e pro1ided. "paces sho2ld 7e 2sed to line the description 2p with the rest o6 the co33ents in the 3ethod. Mexception An Mexception tag sho2ld 7e incl2ded 6or at least an5 declared 9chec=ed: exceptions. )t can also doc23ent an5 non>declared exceptions that can 7e thrown 75 the 3ethod; 9nor3all5 those that appear directl5 in the i3ple3entation; rather than those that are indirectl5 thrown:. N-&EO N/-&EO 4a1a%oc does not ha1e a tag 6or preconditions; so an $!ML tag is 2sed. -reconditions sho2ld 7e expressed in prose or in a 6or3al lang2age i6 it is 3ore appropriate 6or the sit2ation. N-O"!O N/-O"!O 4a1a%oc does not ha1e a tag 6or postconditions; so an $!ML tag is 2sed. -ostconditions sho2ld 7e expressed in prose or in a 6or3al lang2age i6 it is 3ore appropriate.

!.2.! Co""ents# 'i$e %eader


Each 4a1a so2rce 6ile contains a single p27lic class or inter6ace. !he root na3e o6 the 6ile will 7e the na3e o6 the p27lic class that it contains and the s266ix will 7e .ja)a. An5 non>p27lic classes in the so2rce 6ile 32st pro1ide ser1ices that are 2sed directl5 75 the p27lic class 9s2ch non> p27lic classes 3a5 also 7e 2sed 75 other p27lic classes in the sa3e pac=age:. All 4a1a "o2rce 6iles 32st 7egin with a C>st5le co33ent that lists the 6ile na3e and 1ersion in6or3ation# ./ * filename.java * * Version: * !d * * "evisions: * #o$ */

3.3

Indentation

(o2r spaces sho2ld 7e 2sed as the 2nit o6 indentation

3.4

White Space

!.(.1 )$an* +ines


+lan= lines i3pro1e reada7ilit5 75 setting o66 sections o6 code that are logicall5 related. !wo 7lan= lines sho2ld alwa5s 7e 2sed in the 6ollowing circ23stances# +etween sections o6 a so2rce 6ile

CMU-MSE-KRYPT -CS-1.1

+etween class and inter6ace de6initions

One 7lan= line sho2ld alwa5s 7e 2sed in the 6ollowing circ23stances# +etween 3ethods +etween the local 1aria7les in a 3ethod and its 6irst state3ent +e6ore a 7loc= or single>line co33ent +etween logical sections inside a 3ethod to i3pro1e reada7ilit5

!.(.2 )$an* S,a-es


+lan= spaces sho2ld 7e 2sed in the 6ollowing circ23stances# A =e5word 6ollowed 75 a parenthesis sho2ld 7e separated 75 a space. Exa3ple# 2hile &true' 8 ... 9 ote that a 7lan= space sho2ld not 7e 2sed 7etween a 3ethod na3e and its opening parenthesis. !his helps to disting2ish =e5words 6ro3 3ethod calls. A 7lan= space sho2ld appear a6ter co33as in arg23ent lists. All 7inar5 operators sho2ld 7e separated 6ro3 their operands 75 spaces. +lan= spaces sho2ld ne1er separate 2nar5 operators s2ch as 2nar5 3in2s; incre3ent 9EPPE:; and decre3ent 9E>>E: 6ro3 their operands. Exa3ple# a :$ c : d; a $ &a : b' . &c / d'; 2hile &d:: $ s::' 8 n::; 9 printSi;e&,si;e is , : foo : ,<n,'; !he expressions in a 6or state3ent sho2ld 7e separated 75 7lan= spaces. Exa3ple# for &e3pr=; e3pr>; e3pr?' Casts sho2ld 7e 6ollowed 75 a 7lan= space. Exa3ples# myMethod&&byte' aNum, &7bject' 3'; myMethod&&int' &cp : @', &&int' &i : ?'' : =';

CMU-MSE-KRYPT -CS-1.1

& 'tatements
4.1 Simple Statements

Each line sho2ld contain at 3ost one state3ent. Exa3ple# arg)::; argcAA; arg)::; argcAA; .. 1orrect .. 1orrect .. IN17RR+1"B

4.2

Compound Statements

Co3po2nd state3ents are state3ents that contain lists o6 state3ents enclosed in 7races EQ state3ents RE. "ee the 6ollowing sections 6or exa3ples. !he enclosed state3ents sho2ld 7e indented one 3ore le1el than the co3po2nd state3ent. !he opening 7race sho2ld 7e at the end o6 the line that 7egins the co3po2nd state3entF the closing 7race sho2ld 7egin a line and 7e indented to the 7eginning o6 the co3po2nd state3ent. +races are 2sed aro2nd all state3ents; e1en single state3ents; when the5 are part o6 a control str2ct2re; s2ch as an ifAelse or for state3ent. !his 3a=es it easier to add state3ents witho2t accidentall5 introd2cing 72gs d2e to 6orgetting to add 7races.

4.3

Return Statement

A return state3ent with a 1al2e sho2ld not 2se parentheses 2nless it is an expression that re@2ires parentheses or the parentheses pro1ide clarit5. Exa3ple# return; return my!isk.si;e&'; return &si;e C si;e D defaultSi;e';

4.4

if if!else if else!if else Statements

!he ifAelse class o6 state3ents sho2ld ha1e the 6ollowing 6or3# if &condition' 8 statements; 9 if &condition' 8 statements; 9 else 8 statements; 9

CMU-MSE-KRYPT -CS-1.1

if &condition' 8 statements; 9 else if &condition' 8 statements; 9 else8 statements; 9 'ote> if state3ents alwa5s 2se 7races QR. A1oid the 6ollowing error>prone 6or3# if &condition' statement; ..IN17RR+1" BB

4."

For Statements

A for state3ent sho2ld ha1e the 6ollowing 6or3# for &initialization; condition; update' 8 statements; 9 An e3pt5 for state3ent 9one in which all the wor= is done in the initialiIation; condition; and 2pdate cla2ses: sho2ld ha1e the 6ollowing 6or3# for &initialization; condition; update'; /hen 2sing the co33a operator in the initialiIation or 2pdate cla2se o6 a for state3ent; a1oid the co3plexit5 o6 2sing 3ore than three 1aria7les. )6 needed; 2se separate state3ents 7e6ore the for loop 96or the initialiIation cla2se: or at the end o6 the loop 96or the 2pdate cla2se:.

4.#

While Statements

A 2hile state3ent sho2ld ha1e the 6ollowing 6or3# 2hile &condition' 8 statements; 9 An e3pt5 2hile state3ent sho2ld ha1e the 6ollowing 6or3# 2hile &condition';

4.$

do!%hile Statements

A doA2hile state3ent sho2ld ha1e the 6ollowing 6or3# do 8 statements; 9 2hile &condition';

CMU-MSE-KRYPT -CS-1.1

4.&

s%itch Statements

A s2itch state3ent sho2ld ha1e the 6ollowing 6or3# s2itch &condition' 8 case *E1D statements; ./ falls through /. case !+(D statements; break; case FGHD statements; break; defaultD statements; break; 9 E1er5 ti3e a case 6alls thro2gh 9doesnJt incl2de a 7rea= state3ent:; add a co33ent where the 7rea= state3ent wo2ld nor3all5 7e. !his is shown in the preceding code exa3ple with the /L 6alls thro2gh L/ co33ent. E1er5 s2itch state3ent sho2ld incl2de a de6a2lt case. !he 7rea= in the de6a2lt case is red2ndant; 72t it pre1ents a 6all>thro2gh error i6 later another case is added.

4.'

tr(!catch Statements

A tryAcatch state3ent sho2ld ha1e the 6ollowing 6or3at# try 8 statements; 9 catch &+3ception1lass e' 8 statements; 9 A tryAcatch state3ent 3a5 also 7e 6ollowed 75 finally; which exec2tes regardless o6 whether or not the try 7loc= has co3pleted s2ccess62ll5. try 8 statements; 9 catch &+3ception1lass e' 8 statements; 9 finally 8 statements; 9

CMU-MSE-KRYPT -CS-1.1

10

4.1) *+ception Statements (.10.1 T&ro.ing An E/-e,tion


/hen throwing an exception; the state3ent sho2ld ha1e the 6ollowing sa3ple 6or3at#
public )oid 7pen-eyStore&String path' thro2s ja)a.security.-eyStore+3ception 8 Eerkeley!EManager certI $ ne2 Eerkeley!EManager&'; try 8 certI.hash7pen&path : JcertI.dbK'; 9 catch &ja)a.io.I7+3ception e' 8 thro2 ne2 ja)a.security.-eyStore+3ception&,1ertificate file not found in gi)en path.,'; 9 9

A precise description o6 the exception sho2ld 7e incl2ded while throwing an exception. !he text sho2ld 7e as si3ple and direct as possi7le; and end with a period. Kppercase is 3andator5 in the 6irst word o6 the sentence; and sho2ld 7e 2sed inside the sentence onl5 when re@2ired. Exa3ples o6 correct exception text are# SCapa7ilit5 6or deleting certi6icates not i3ple3ented 5et.T S)ncorrect password 6or =e5store 6ile.T SCerti6icate 6ile not 6o2nd in gi1en path.T Exa3ples o6 incorrect exception text# S/eCre sorr5; 72t we ha1e not i3ple3ented the capa7ilit5 to delete entries at the 3o3ent.T S)ncorrect password.T SCerti6icate 6ile not there.T

(.10.2 Cat-&ing An E/-e,tion


/hen catching an exception; the state3ent sho2ld ha1e the 6ollowing sa3ple 6or3at# try 8 7pen-eyStore&JcD<ndbs<K'; 9 catch &ja)a.security.-eyStore+3ception e' 8 System.out.println&e.getMessage&''; 9 !he catch state3ent sho2ld onl5 print the 3essage s2pplied 75 the 3ethod throwing the exception.

CMU-MSE-KRYPT -CS-1.1

11

( )eclarations
".1 ,um-er Per .ine

Maxi323 one declaration per line is 3andator5. !he co33ent descri7ing each 1aria7le sho2ld 7e placed a7o1e the declaration. .. Indentation le)el int le)el; .. Si;e of table int si;e; !he 6ollowing is not allowed# int le)el, si;e; %o not p2t di66erent t5pes on the sa3e line. Exa3ple# int foo, foo*rrayLM; .. IN17RR+1"B

".2

Initiali/ation

Local 1aria7les sho2ld 7e initialiIed where the5 are declared. !he onl5 reason not to initialiIe a 1aria7le where it is declared is i6 the initial 1al2e depends on so3e co3p2tation occ2rring 6irst.

".3

Placement

%eclarations sho2ld 7e placed onl5 at the 7eginning o6 7loc=s. %o not wait to declare 1aria7les 2ntil their 6irst 2seF it can con62se the 2nwar5 progra33er and ha3per code porta7ilit5 within the scope. )oid myMethod&' 8 .. beginning of method block int int= $ N; if &condition' 8 .. beginning of ,if, block int int> $ N; ... 9 9 !he one exception to the r2le is indexes o6 for loops; which in 4a1a can 7e declared in the for state3ent# for &int i $ N; i 4 ma3Ooops; i::' 8 ... 9 A1oid local declarations that hide declarations at higher le1els. (or exa3ple; do not declare the sa3e 1aria7le na3e in an inner 7loc=#

CMU-MSE-KRYPT -CS-1.1

12

int count; ... myMethod&' 8 if &condition' 8 int count $ N; ... 9 ... 9

.. IN17RR+1"B

".4

Class and Interface 0eclarations

/hen coding 4a1a classes and inter6aces; the 6ollowing 6or3atting r2les sho2ld 7e 6ollowed# o space 7etween a 3ethod na3e and the parenthesis E9E starting its para3eter list; 6ollowed 75 the parenthesis S:T Open 7race EQE appears at the end o6 the sa3e line as the declaration state3ent. Closing 7race ERE starts a line 75 itsel6 indented to 3atch its corresponding opening state3ent; except when it is a n2ll state3ent the ERE sho2ld appear i33ediatel5 a6ter the EQE. class Sample e3tends 7bject 8 int i)ar=; int i)ar>; Sample&int i, int j' 8 i)ar= $ i; i)ar> $ j; 9 int emptyMethod&' 8 9 ... 9 Methods are separated 75 a 7lan= line .

CMU-MSE-KRYPT -CS-1.1

13

* Co e +,ample
!he 6ollowing exa3ple shows how to 6or3at a 4a1a so2rce 6ile containing a single p27lic class. ./ / Elah.ja)a / / PersionD * 1.%2 &&/'(/1% * * "evisions: * #o$ * / / 1opyright &c' =QQ%A=QQQ Sun Microsystems, Inc. / QN= San *ntonio Road, 5alo *lto, 1alifornia, Q%?N?, R.S.*. / *ll rights reser)ed. / / "his soft2are is the confidential and proprietary information of Sun / Microsystems, Inc. &,1onfidential Information,'. Gou shall not / disclose such 1onfidential Information and shall use it only in / accordance 2ith the terms of the license agreement you entered into / 2ith Sun. /. package ja)a.blah; import ja)a.blah.blahdy.ElahElah; .// / Class description goes here. / / 0author Firstname Lastname /. public class Elah e3tends Some1lass 8 /* A class implementation comment can go here. */ .// classVar1 documentation comment /. public static int classPar=; .// / classVar2 documentation comment that happens to be / more than one line long /. pri)ate static 7bject classPar>; .// instanceVar1 documentation comment /. public 7bject instancePar=; .// instanceVar2 documentation comment /. protected int instancePar>; .// instanceVar3 documentation comment /. pri)ate 7bjectLM instancePar?;

CMU-MSE-KRYPT -CS-1.1

1*

.// / ...constructor lah documentation comment... /. public Elah&' 8 // ...implementation goes here... 9 .// / ...method do!omething documentation comment... /. public )oid doSomething&' 8 // ...implementation goes here... 9 .// / ...method doSomething+lse documentation comment... / 0param some5aram description /. public )oid doSomething+lse&7bject some5aram' 8 // ...implementation goes here... 9 9

CMU-MSE-KRYPT -CS-1.1

1'

You might also like