1 written by BAGE
날짜
JAVA Coding Pattern
코딩 규칙
2010.08.03 박혜웅
2 written by BAGE
• 이 문서는 권고안의 내용중 중요한 부분과 개인경험을 통해 정리하였다 .• Sun 의 권고안
– http://java.sun.com/docs/codeconv/– http://www.oracle.com/technetwork/java/codeconv-138413.html
• 권고안을 따르는 Eclipse default formatter 를 기준으로 한다 .
Code Conventions-Basic
3 written by BAGE
• File Type– .java (UTF-8)– .class– README
• 설명을 위한 텍스트 파일 (UTF-8)
• Beginning Comments– ??
• Class and Interface Declarations– Variables, Method
• public, protected, private 순서로 나열• Indentation
– tabs only (4 spaces), Eclipse default formatter 와 동일• Line Length
– 80 ~ 150 ( 모니터에 맞게 )• Wrapping Lines ( 줄 바꿈 )
– Eclipse default formatter 와 동일
Code Conventions
4 written by BAGE
• Block Comments, Single-Line comments, Trailing Comments (/* */)– Do not use for commenting code.
• Search 명령의 검색결과가 소스인지 코멘트인지 불분명하므로 .
• End-Of-Line Comments (//)– Use for commenting code.
Code Conventions-Comments
//if (bar > 1 ) {//// // To do:// ...//}// else// return false;
5 written by BAGE
• One declaration per line– Eclipse default formatter 와 동일
• Avoid declarations that hide declaratioins.– 동일한 변수명을 가능한 사용하지 말아라– 단 , Constructor, Setter, Getter 는 예외다 .
Code Conventions-Declarations
int level; // Goodint size; // Good
int level, size; // Avoid
int count;...func() {
if (condition) {int count; //
Avoid...
int count;...func() {
if (...) {int countSome;...
int count;...setCount(int count) {
if (...) {this.count=count;
...
6 written by BAGE
• Class and Interface Declarations– Eclipse default formatter 와 동일– Methods are separated by blank line
Code Conventions-Declarations
class Sample extends Object {int ivar1;int ivar2;
Sample(int i, int j){ivar1=i;ivar2=j;
}
int emptyMethod() {}
...}
7 written by BAGE
• Simple Statements– Each line should contain at most one statement
– Do not use the comma operator
• return Statements– Use simple expression
– Do not use ternary expression (?)
Code Conventions-Statements
argv++;argc--;
argv++; argc--; // Avoid
System.out.println("error");System.exit(1);
System.out.println("error"), System.exit(1);// Avoid
return: myDisk.size();
return (size ? size : defaultSize); // Avoid
if( size > 0 ) {return size;
}return 0;
return;
8 written by BAGE
• if-else Statements– if statements always use braces {}.
• for, while Statements– An empty for, whlie statement should have the following form
• Blank Lines, Blank Spaces– Eclipse default formatter
Code Conventions-Statements, Blank
if ( condition ) {statement;
}
if ( condition ) // Avoidstatement;
for ( initialization; condition; update );while ( condition );
for ( initialization; condition; update ) {} ; //Avoidwhile ( condition ) {}; //Avoid
9 written by BAGE
• Java Project(?), Classes, Interfaces Naming– Should be noun with the first letter capitalized.– Avoid acronyms( 머리글자어 ) and abbreviatioins( 약어 ) except URL,
HTML...– Example
• class URLEncoder;• interface Storing;
– Use Whole words.– Example
• class MatrixCalculation; // OK• class MatCalc; // AVOID!
• Methods– Should be verbs with the first letter lowercase.– Omit class or interface name.– Example
• MatrixCalculation calculation = new MatrixCaculation();• calculation.getData(); // OK• calculation.getMatrixCalculationData(); // AVOID!
Code Conventions-Naming
10 written by BAGE
• Variables– Should be noun with the first letter lowercase. – If instance variable(public, protected, private, default), always use
keyword “this”.– Example
• private long instanceVariable;• this.instanceVariable = 10L; // OK• instanceVariable = 10L; // AVOID!
– Avoid acronyms( 머리글자어 ) and abbreviatioins( 약어 )– Example
• BufferedReader bufferedReader = new BufferedReader(); // OK• BufferedReader reader = new BufferedReader(); // OK• BufferedReader br = new BufferedReader(); //AVOID!
– if name is so long, can be shortten.– Example
• int difference; // OK• int diff; // OK• int d; // AVOID
Code Conventions-Naming
11 written by BAGE
• Constants (static final)– Should be all uppercase with words separated by underscores(_).– Ends with noun.– Example
• static final int MIN_WIDTH = 4; // OK• static final int minWidth = 4; // AVOID!
Code Conventions-Naming
12 written by BAGE
• Referring to Class Variables and Methods– Avoid using an object to access a static variable or method.– Example
• AClass.classMethod(); // OK• anObject.classMethod(); // AVOID!
• Variable Assignments– Avoid assigning several variables to the same value in a single state-
ment.– Example
• fooBar.fChar = barFoo.lchar = ‘c’; //AVOID!• d = (a = b + c) + r; //AVOID!
• Parenthese( 소괄호 )– Use parentheses liberally.– Example
• if(a == b && c == d) //AVOID!• if((a ==b) && (c == d)) // OK
Code Conventions-Programming