백엔드 코딩 컨벤션
기본적인 스타일 가이드는 https://google.github.io/styleguide/javaguide.html 를 따릅니다.
com.
├── manager
│ ├── controller
│ ├── converter
│ ├── dto
│ ├── entity
│ ├── service
│ └── repository
├── user
│ ├── controller
│ ├── converter
│ ├── dto
│ ├── entity
│ ├── service
│ └── repository
...
UserService, ManagerControllerUser, ManagerDTO 접미사를 사용합니다.
UserDTO , ManagerDTOUserRepository, VehicleServicegetUser, createParkingLottestGetUser, shouldCreateParkingLotuserId, parkingLotName중괄호 없는 if문을 사용하지 않습니다.
중괄호 스타일: K&R 스타일을 사용합니다.
if (condition) {
// do something
} else {
// do something else
}
클래스 주석: 클래스 상단에 Javadoc 형식으로 작성합니다.
/**
* This class represents a user in the system.
*/
public class User {
// class body
}
메서드 주석: 메서드 상단에 Javadoc 형식으로 작성합니다.
/**
* Retrieves a user by their ID.
*
* @param id the ID of the user
* @return the user with the given ID
*/
public User getUserById(Long id) {
// method body
}
클래스 레벨 어노테이션: 클래스 선언 위에 위치시킵니다.
한 줄에 하나의 어노테이션만 작성합니다.
@RestController
@RequestMapping("/users")
public class UserController {
// class body
}
@Data 를 사용하지 않고 개별로 어노테이션을 작성합니다.@Setter를 사용을 권장하지는 않지만 코드가 복잡하거나 변경이 많은 경우 따로 컬럼에 대한 메소드를 정의하여 사용합니다.예외 처리: 예외는 적절하게 처리하고, 필요한 경우 사용자 정의 예외를 사용합니다.
try {
// code that may throw an exception
} catch (SpecificException e) {
// handle specific exception
} catch (Exception e) {
// handle generic exception
}
로깅: SLF4J를 사용하여 로깅합니다.
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
public void myMethod() {
logger.info("This is an info message");
logger.error("This is an error message");
}
application.properties: 설정 파일은 명확하게 주석을 달아 관리합니다.
# Server configuration
server.port=8080
# Database configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
테스트 클래스: 테스트 대상 클래스와 같은 패키지에 위치시키고, Test 접미사를 추가합니다.
public class UserServiceTest {
// test methods
}
테스트 프레임워크: JUnit 5와 Mockito를 사용합니다.
@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
public void testGetUserById() {
// test implementation
}
}