Java Naming Convection
은 코드의 식별자, 즉 클래스, 변수, 메서드 등의 이름을 짓는 규칙을 말합니다. 이 규칙은 코드의 가독성을 높이고 통일성을 유지하기 위해 사용됩니다. 이러한 Code Convection을 통해서 더욱 깔끔하게 코드를 공유하고 우선 순위를 둘 수 있습니다.
File Name | Use |
---|---|
GNUmakefile | The preferred name for makefiles. We use GNUmakefile to build our software. |
README | The preferred name for the file that summarizes the contents of a particular directory |
❗️ What is a Makefile?
메이크 파일은 명시적 룰, 암묵적인 룰, 변수 정의, 지시어 그리고 주석문 이렇게 다섯가지 종류의 것들이 들어 있습니다.
makefile
을 읽을 때 make
가 뭔가 특별한 작업를 하도록 만드는 명령어이다. ex) 다른 makefile 읽기, 일부 무시하고 코딩 등등makefile
은 주석문(#)으로 시작한다.GNUmakefile
은 범용적으로 쓰이지 못하고 특정 프로그램에만 사용이 될 수 있다는 점이 있기 때문에 makefile
이 더 범용적으로 사용이 된다.Makefile example
JFLAGS = -g
JAVAC = javac
RM = rm
TARGET = Helloworld.class
SOURCES = Helloworld.java
all: $(TARGET)
.SUFFIXES: .java .class
.java.class:
$(JAVAC) $(JFLAGS) $<
clean:
-$(RM) $(SOURCES:.java=.class)
이렇게 코드를 작성함으로써, 우리가 make라는 것을 Window terminal에 입력했을 때 결과를 똑같이 얻을 수 있는 것이다.
/*
* Classname
*
* Version information
*
* Date
*
* Copyright notice
*/
Java can have four styles of implementation comments; Block, sigle-line, trailing, and end-of-line.
: Block Comments
는 파일, 메서드, 자료 구조 그리고 알고리즘에 대한 설명에 대해서 작성할 때 사용이 됩니다.
/*
* Here is a block comment.
*/
/*-* Here is a block comment with some very special
* formatting that I want indent(1) to ignore.
*
* one
* two
* three
*/
if (condition) {/* Handle the condition. */
...
}
: Trailing Comment
는 같은 줄에 해당하는 코드가 어떤 식으로 동작하는지에 대해 나타내고 싶을 때 간략하게 탭을 한 후에 주석 처리하여 작성한다.
if (a == 2) {return TRUE; /* special case */
} else {
return isPrime(a); /* works only for odd a */
}
: 일반적으로 사용하는 주석의 형태이다.
if (foo > 1) {// Do a double-flip.
...
}
else {
return false; // Explain why here.
}
//if (bar > 1) {
//
// // Do a triple-flip.
// ...
//}
//else {
// return false;
//}
int level; // indentation level
int size; // size of table
/* Or
int level, size;
Do not put different type on the same line!!!!
*/
: 변수의 초기화는 선언한 메서드 혹은 함수 안에서 바로하는 것이 좋다.
: 변수의 선언은 코드 블럭의 시작 부분에 삽입한다.
void myMethod() {
int int1 = 0; // beginning of method block
if (condition) {
int int2 = 0; // beginning of "if" block
...
}
}
</blockquote>
Try to avoid this form!
int count;
...
myMethod() {
if (condition) {
int count = 0; // AVOID!
...
}
...
}
: 자바의 클래스 혹은 인터페이스를 만들때, 아래의 룰을 따라서 만든다.
class Sample extends Object {
int ivar1;
int ivar2;
Sample(int i, int j) {
ivar1 = i;
ivar2 = j;
}
int emptyMethod() {}
...
}
class CarModel;
class EmployeeInfo;
void calculateTutorial();
String getUserInfo();
int totalCount;
String firstName;
final int MAX_SIZE = 100;
final double PI_VALUE = 3.14;
💡
final
의 뜻은?
변수, 메서드, 클래스 들에 사용되며, 사용된 요소에 대한 변경을 금지하거나, 값이 한 번 할당되면 변경할 수 없음을 나타냅니다. 그리고 코드의 매직넘버(코드안에 하드코딩된 숫자를 의미합니다. 즉, 직접 사용된 숫자를 의미한다.)가 있으면 가독성이 떨어지고, 유지보수의 어려움이 있기 때문에MAX_SIZE
라는 이름으로 선언하여 사용을 한다.
package com.example.myproject;
interface Drawable;
interface Serailizable;
void printDetails(String employeeName, int employeeId);
class CustomerOrder;
String processHttpRequest();
🧸 Better statements
if(condition) { return x; } return y;
보다는 아래와 같이 쓰는게 훨씬 효율적이다.
return (condition ? x : y);
references
1. Gerneral Rules of Makefile
2. Code Convention