1 / 12

JAVA Coding Pattern

JAVA Coding Pattern. 코딩 규칙. 2010.08.03 박혜웅. Code Conventions-Basic. 이 문서는 권고안의 내용중 중요한 부분 과 개인경험 을 통해 정리하였다 . Sun 의 권고안 http://java.sun.com/docs/codeconv/ http://www.oracle.com/technetwork/java/codeconv-138413.html 권고안을 따르는 Eclipse default formatter 를 기준으로 한다. Code Conventions.

kacy
Télécharger la présentation

JAVA Coding Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA Coding Pattern 코딩 규칙 2010.08.03 박혜웅

  2. Code Conventions-Basic • 이 문서는 권고안의 내용중 중요한 부분과 개인경험을 통해 정리하였다. • Sun의 권고안 • http://java.sun.com/docs/codeconv/ • http://www.oracle.com/technetwork/java/codeconv-138413.html • 권고안을 따르는 Eclipse default formatter를 기준으로 한다.

  3. Code Conventions • 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 와 동일

  4. Code Conventions-Comments • Block Comments, Single-Line comments, Trailing Comments (/* */) • Do not use for commenting code. • Search명령의 검색결과가 소스인지 코멘트인지 불분명하므로. • End-Of-Line Comments (//) • Use for commenting code. //if (bar > 1 ) { // // // To do: // ... //} // else // return false;

  5. Code Conventions-Declarations • One declaration per line • Eclipse default formatter 와 동일 • Avoid declarations that hide declaratioins. • 동일한 변수명을 가능한 사용하지 말아라 • 단, Constructor, Setter, Getter는 예외다. int level; // Good int size; // Good int level, size; // Avoid int count; ... func() { if (...) { int countSome; ... int count; ... setCount(int count) { if (...) { this.count=count; ... int count; ... func() { if (condition) { int count;// Avoid ...

  6. Code Conventions-Declarations • Class and Interface Declarations • Eclipse default formatter와 동일 • Methods are separated by blank line class Sample extends Object { int ivar1; int ivar2; Sample(int i, int j){ ivar1=i; ivar2=j; } int emptyMethod() {} ... }

  7. Code Conventions-Statements • 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 (?) argv++; argc--; argv++; argc--; // Avoid System.out.println("error"); System.exit(1); System.out.println("error"), System.exit(1);// Avoid return; return: myDisk.size(); if( size > 0 ) { return size; } return 0; return (size ? size : defaultSize); // Avoid

  8. Code Conventions-Statements, Blank • 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 if ( condition ) { statement; } if ( condition ) // Avoid statement; for ( initialization; condition; update ); while ( condition ); for ( initialization; condition; update ) {} ; //Avoid while ( condition ) {}; //Avoid

  9. Code Conventions-Naming • 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!

  10. Code Conventions-Naming • 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

  11. Code Conventions-Naming • 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!

  12. Code Conventions-Programming • 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 statement. • 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

More Related