Code Convention - prgrms-web-devcourse-final-project/WEB4_5_ServerSOS_BE GitHub Wiki
βοΈ μ½λ 컨벀μ
μ£Όμμ μ€λͺ νλ €λ ꡬ문μ λ§μΆ° λ€μ¬μ°κΈ° ν©λλ€.
// Good
function someFunction() {
...
// statementμ κ΄ν μ£Όμ
statements
}
βοΈ μ½λ 컨벀μ μ΄ νμν μ΄μ
- νμλΌλ¦¬ μ½λλ₯Ό 곡μ νκΈ° λλ¬Έμ μΌκ΄μ±μλ μ½λλ₯Ό μμ±νλ©΄ μλ‘ μ΄ν΄νκΈ° μ½λ€.
- λμ€μ μ μ¬ μ§μ μ νλ‘μ νΈλ₯Ό νλ©° μ½λ 컨벀μ μ λ§λ€μ΄ μ§ννλ€κ³ νλ©΄ νμ λ©΄μμ μ 리νκ² μμ©ν μ μλ€.
μ°Έκ³
π Spring boot Java Coding Standards
β¨ 1. κΈ°λ³Έ μμΉ
- κ°λ μ± μ΅μ°μ
- νΉλ³ν μ΄μ κ° μλ κ²½μ° IntelliJ IDEA μλ μμ μ€μ
- νΉλ³ν μ΄μ κ° μλ κ²½μ° IntelliJ IDEA κ²½κ³ μ κ±°
π· 2. λ€μ΄λ° κ·μΉ
πΉ ν΄λμ€ (PascalCase)
- ν΄λμ€λͺ μ νμ€μΉΌ μΌμ΄μ€(PascalCase) μ¬μ©
// β
μ¬λ°λ₯Έ μμ
public class UserAccount { }
public class OrderService { }
πΉ λ³μ λ° λ©μλ (camelCase)
- λ³μ λ° λ©μλλ μΉ΄λ© μΌμ΄μ€(camelCase) μ¬μ©
@Value
λ₯Ό μ¬μ©ν νλ‘νΌν° κ°λ μΉ΄λ© μΌμ΄μ€ μ μ©
// β
μ¬λ°λ₯Έ μμ
private String userName;
public void processOrder() { }
πΉ μΆμ½μ΄ (camelCase)
- μΆμ½μ΄λ μΉ΄λ© μΌμ΄μ€ μ μ©
// β μλͺ»λ μμ
String HTTP;
String DTO;
// β
μ¬λ°λ₯Έ μμ
String Http;
String Dto;
πΉ μμ (UPPER_SNAKE_CASE)
- μμλ λλ¬Έμ μ€λ€μ΄ν¬ μΌμ΄μ€(UPPER_SNAKE_CASE) μ¬μ©
// β
μ¬λ°λ₯Έ μμ
public static final int MAX_USER_COUNT = 100;
public static final String API_KEY = "1234";
πΉ ν¨ν€μ§λͺ (μλ¬Έμ)
- ν¨ν€μ§λͺ μ μ λΆ μλ¬Έμ μ¬μ©
// β
μ¬λ°λ₯Έ μμ
com.example.service;
org.myproject.utils;
πΉ μ€μλ§ μ¬μ© κΈμ§
- λ Όμλ κ²½μ°λ₯Ό μ μΈνκ³ μ€μλ§ μ¬μ© κΈμ§
// β μλͺ»λ μμ
String msg;
String err;
// β
μ¬λ°λ₯Έ μμ
String message;
String error;
πΉ CRUD method λͺ λͺ κ·μΉ
κΈ°λ₯ | Controller λ©μλλͺ | Service λ©μλλͺ | Repository λ©μλλͺ |
---|---|---|---|
Create (POST) | createUser(UserForm userForm) |
createUser(UserForm userForm) |
save(User user) (JPA κΈ°λ³Έ) |
Read (λ¨μΌ μ‘°ν) (GET) | getUserById(Long id) |
getUserById(Long id) |
findById(Long id) (JPA κΈ°λ³Έ) |
Read (λͺ©λ‘ μ‘°ν) (GET) | getUserList() |
getUserList() |
findAll() (JPA κΈ°λ³Έ) |
Update (PUT) | updateUser(Long id, UserForm userForm) |
updateUser(Long id, UserForm userForm) |
save(User user) (JPA κΈ°λ³Έ, ID μ‘΄μ¬ μ μ
λ°μ΄νΈ) |
Delete (DELETE) | deleteUserById(Long id) |
deleteUserById(Long id) |
deleteById(Long id) (JPA κΈ°λ³Έ) |
β μΌκ΄μ± μ μ§:
- Controller & Service β
get
,create
,update
,delete
- Repository β
find
,save
,deleteById
- DTO (UserDto) β μλ΅μ©, Form (UserForm) β μ λ ₯μ©
πΉ HTTP URL λͺ λͺ κ·μΉ
- prefix λ‘ api λ₯Ό κ°μ (νλ‘μ νΈ λ 벨μμ μ€μ μλ£, controller μμ μ€μ ν νμ μμ)
- λμ¬λ μ¬μ©νμ§ μκ³ http method λ₯Ό νμ©
// β μλͺ»λ μμ
/api/v1/order/create
/api/v1/order/delete
// β
μ¬λ°λ₯Έ μμ
(post) /api/v1/order
(delete) /api/v1/order
- 리μμ€λ νμ 볡μν μ¬μ©
// β μλͺ»λ μμ (λ¨μν 리μμ€)
GET /api/v1/user
POST /api/v1/order
// β
μ¬λ°λ₯Έ μμ (볡μν 리μμ€)
GET /api/v1/users
POST /api/v1/orders
- λΉμ¦λμ€ λ‘μ§μ΄ λ€μ΄κ° **νμ(Action)μ λμ¬ ν¬ν¨ (approve, ban, login λ±)
// β
μ‘μ
μ΄ νμν κ²½μ°
POST /api/v1/auth/login
POST /api/v1/auth/logout
POST /api/v1/orders/123/approve // μ£Όλ¬Έ μΉμΈ
POST /api/v1/users/456/ban // μ μ μ°¨λ¨
π¨ 3. μ½λ μ€νμΌ
{}
μ¬μ©
πΉ μ€κ΄νΈ - λ¨μΌ λΌμΈ 쑰건문μλ λ°λμ μ€κ΄νΈ
{}
μ¬μ©
// β μλͺ»λ μμ
if (a == 0) return a;
// β
μ¬λ°λ₯Έ μμ
if (a == 0) {
return a;
}
πΉDTO
-
λλ©μΈ DTO λ§κ³ Entityλ₯Ό 곡μ
@GetMapping("/{id}") public RsData<ExampleDetailResponse> get(@PathVariable("id") Long id) { ExampleDetailResponse response = exampleService.getDetail(id); return RsData.from(SUCCESS, response); } @Transactional(readOnly = true) public ExampleDetailResponse getDetail(Long id) { Example example = getEntity(id); return ExampleDetailResponse.from(example); } private Example getEntity(Long id) { return exampleRepository.findById(id).orElseThrow(() -> new BusinessException(NOT_FOUND)); }
- PostDto, PostWithPerformanceDto κ°μ μν°ν°μ λμλλ Dtoλ₯Ό λ³λλ‘ μμ±νμ§ μλλ€.
- 컨νΈλ‘€λ¬ λ©μλμ λ§€νλλ μλΉμ€ λ©μλ μμ± (μλΉμ€μμ μλ΅ DTOλ‘ λ―Έλ¦¬ λ³ννκ³ μ»¨νΈλ‘€λ¬μ λ°ν)
- μΆκ°λ‘ νμν μλΉμ€ λ©μλ μμ±(λ©μλλΌλ¦¬ λ°μ΄ν° 곡μ κ° νμνλ©΄ Entity μ κ·Ή μ¬μ©)
-
Controllerμμ λ°νν DTO
- ~~SimpleResponse β 리μ€νΈ λ°μ΄ν°(λͺ©λ‘ νμ΄μ§)
- ~~DetailResponse β μμΈ λ°μ΄ν°(μμΈ νμ΄μ§)
- PostSimpleResponse
- PostDetailResponse
πΉApiPathκ΄λ ¨
- νλμΌλλ {id}
- νλμ΄μμΌλλ {postId}μ κ°μ΄ μ¬μ©
곡ν΅κ°μ²΄
-
RsData νμ
-
μ±κ³΅: μν, λ©μμ§, λ°μ΄ν°
μ±κ³΅(νμ΄μ§)
{ "code": 200, "message": "μμ²μ΄ μ±κ³΅νμ΅λλ€.", "data": { "items": [ { "id" : 1, "title" : "κ²μκΈ μ λͺ©", "venue" : "κ³΅μ° μ₯μ", "startDate" : "2025-04-29T14:30:45", "endDate" : "2025-04-30T14:30:45", "poster" : "ν¬μ€ν° url" }, { "id" : 2, "title" : "κ²μκΈ μ λͺ©", "venue" : "κ³΅μ° μ₯μ", "startDate" : "2025-04-29T14:30:45", "endDate" : "2025-04-30T14:30:45", "poster" : "ν¬μ€ν° url" } ], "page": 1, "size": 10, "totalPages": 1, "totalElements": 2 } }
μ±κ³΅(μμΈ)
{ "code": 200, "message": "μμ²μ΄ μ±κ³΅νμ΅λλ€.", "data": { "items": [ { "postId" : 1, "title" : "κ²μκΈ μ λͺ©", "venue" : "κ³΅μ° μ₯μ", "startDate" : "2025-04-29T14:30:45", "endDate" : "2025-04-30T14:30:45", "poster" : "ν¬μ€ν° url" }, { "postId" : 2, "title" : "κ²μκΈ μ λͺ©", "venue" : "κ³΅μ° μ₯μ", "startDate" : "2025-04-29T14:30:45", "endDate" : "2025-04-30T14:30:45", "poster" : "ν¬μ€ν° url" } ], "page": 1, "size": 10, "totalPages": 1, "totalElements": 2 } }
-
μλ¬ response: μν, μ½λ, λ©μμ§
μ€ν¨
{ "code" : 404, "message" : "리μμ€κ° μ‘΄μ¬νμ§ μμ΅λλ€.", "data" : null }
-
-
PageDto
- items
- page
- totalElements
- totalPages
- size