Skip to content

Code convention

grolarkim edited this page Aug 20, 2023 · 11 revisions

์ฝ”๋“œ ์ปจ๋ฒค์…˜

Java Google Style Guide

์ฝ”๋“œ ์Šคํƒ€์ผ์˜ ๊ฒฝ์šฐ Java Google Style Guide๋ฅผ ์‚ฌ์šฉํ•˜๋˜ ์ผ๋ถ€๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์ถ”๊ฐ€ ์‚ฌํ•ญ

1. Tab Size

  • Tab size๋Š” 4๋กœ ์‚ฌ์šฉํ•œ๋‹ค. (๊ตฌ๊ธ€ ์Šคํƒ€์ผ : 2)

2. ๋ฉ”์†Œ๋“œ ์ตœ๋Œ€ ๊ธธ์ด

  • ๋ฉ”์†Œ๋“œ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” ๊ถŒ์žฅ 10์ค„ ์ด๋‚ด, ์ตœ๋Œ€ 15์ค„ ์ด๋‚ด๋กœ ํ•œ๋‹ค.

3. Get vs Find

  • ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค ์‹œ ๋ช…๋ช…ํ•˜๋Š” ๋ฐฉ๋ฒ•, get๊ณผ find๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋˜ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ž‘์„ฑํ•œ๋‹ค.
    • get โ† ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ๋ฐ˜ํ™˜, ๊ฐ์ฒด๊ฐ€ ์—†๋‹ค๋ฉด ์˜ˆ์™ธ๋ฅผ ๋˜์ง
    • find โ† Optional๋กœ ๊ฐ์ฒด๋ฅผ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜

4. ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค ๋„ค์ด๋ฐ

  • ์ธํ„ฐํŽ˜์ด์Šค โ†’ ํ˜•์šฉ์‚ฌ๋‚˜ ๋ช…์‚ฌ๋กœ ์ด๋ฆ„์„ ์ง“๋Š”๋‹ค.
  • ํด๋ž˜์Šค โ†’ ๋ช…์‚ฌ๋กœ ์ด๋ฆ„์„ ์ง“๋Š”๋‹ค.

5. DTO ๋„ค์ด๋ฐ

  • ์š”์ฒญ : Request๋ฅผ ์ ‘๋ฏธ์‚ฌ๋กœ ๊ฐ€์ง„๋‹ค. ์˜ˆ) LoginRequest
  • ์‘๋‹ต : Response๋ฅผ ์ ‘๋ฏธ์‚ฌ๋กœ ๊ฐ€์ง„๋‹ค. ์˜ˆ) LoginResponse

6. Validation ์ „๋žต

  • Controller -> DTO์— @Valid ํ™œ์šฉํ•˜์—ฌ Notnull, NotBlank, Size ๋“ฑ ์‚ฌ์ „์กฐ๊ฑด์„ ๊ฒ€์ฆํ•œ๋‹ค.
  • Service -> Repository์—์„œ ๋ฐ˜ํ™˜ํ•œ Optional.empty ๋“ฑ ๋น„์ฆˆ๋‹ˆ์Šค ํ”Œ๋กœ์šฐ ์ƒ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”์ง€ ๊ฒ€์ฆํ•œ๋‹ค.
  • Domain -> ๋„๋ฉ”์ธ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ฒ€์ฆํ•œ๋‹ค. ์˜ˆ) ํŒ๋งค ์‹œ์ž‘ ์‹œ๊ฐ„์ด ํŒ๋งค ์ข…๋ฃŒ ์‹œ๊ฐ„๋ณด๋‹ค ์•ž์„œ์•ผ ํ•œ๋‹ค. ๋“ฑ
  • Repository, Entity -> ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์— ์ œ์•ฝ ์กฐ๊ฑด์„ ๋ช…์‹œํ•˜๋˜ ์ถ”๊ฐ€์ ์ธ ๊ฒ€์ฆ์€ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

7. ์–ด๋…ธํ…Œ์ด์…˜ ์ˆœ์„œ

  • Annotation์˜ ์ˆœ์„œ๋Š” ์งง์€ ๊ฒƒ์ด ์œ„๋กœ, ๊ธด ๊ฒƒ์ด ์•„๋ž˜๋กœ ๊ฐ„๋‹ค.
  • ์˜ˆ์‹œ
    @Service
    @RequiredArgsConstructor
    @Transactional(readOnly = true)
    public class OrderService implements OrderUseCase {

8. ๋ถˆ๋ณ€ ๊ฐ์ฒด final

  • ๋ถˆ๋ณ€ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ์„ ์–ธ์‹œ final์„ ๋ถ™์ธ๋‹ค.
  • ์˜ˆ์‹œ
    @Override
    @Transactional
    public OrderIdResponse orderCartByCartIdAndUserId(final long cartId, final long userId) {
        final Cart cart = cartConnector.findByCartIdAndUserId(cartId, userId)
            .orElseThrow(() -> new DoesNotFindCartException(cartId, userId));
        final Order order = OrderMapper.toDomain(idGenerator, userId, cart, timeUtil.now());
        orderRepository.persist(order);
        return new OrderIdResponse(order.getId());
    }

9. Code Smell with SonarLint

  • SonarLint๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ ์Šค๋ฉœ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ์—†์•ค๋‹ค.
  • Code Smells แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-08-20 แ„‹แ…ฉแ„’แ…ฎ 3 35 16

ํ…Œ์ŠคํŠธ ์ปจ๋ฒค์…˜

1. ํ…Œ์ŠคํŠธ

  • ๋‹จ์œ„ : ํด๋ž˜์Šค๋‹จ์œ„, ๊ฐ™์€ ๋ ˆ์ด์–ด์—์„œ ๋‹ค๋ฅธ ๋ ˆ์ด์–ด์˜ ์ฐธ์กฐ๊ฐ€ ํ•„์š”์—†๋Š” ๊ฒƒ์„ ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  • ํ†ตํ•ฉ : ํด๋ž˜์Šค๋‹จ์œ„, ๋‹ค๋ฅธ layer๋Š” ์˜์กด์„ฑ์˜ ๋ฐฉํ–ฅ์—๋”ฐ๋ผ ์„ ํƒ์ ์œผ๋กœ Mocking ํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  • ์ธ์ˆ˜ : API๋ถ€ํ„ฐ ์ „์ฒด๋ฅผ ํ…Œ์ŠคํŠธํ•œ๋‹ค. ํด๋ž˜์Šค mocking์€ ์—†๋‹ค. Tomcat๊ณผ DB๋Š” ํ…Œ์ŠคํŠธ์šฉ Mocking์„ ์‚ฌ์šฉํ•œ๋‹ค.

2. ํ…Œ์ŠคํŠธ ์ ์šฉ ๋ฒ”์œ„

  • Controller โ†’ ์ธ์ˆ˜ํ…Œ์ŠคํŠธ๋กœ ๋Œ€์ฒดํ•œ๋‹ค.
  • Service โ†’ ํ†ตํ•ฉํ…Œ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•˜๋ฉด์„œ ๋น„์ฆˆ๋‹ˆ์Šค ํ”Œ๋กœ์šฐ๋ฅผ ๊ฒ€์ฆํ•œ๋‹ค.
  • Domain โ†’ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•˜๋ฉด์„œ ์ตœ๋Œ€ํ•œ๊ผผ๊ผผํžˆ, ๋™์ž‘, ์˜ˆ์™ธ, ๋ฐ˜๋ก€๊ฐ™์€๊ฑฐ ์ƒ๊ฐํ•˜๋ฉด์„œ ํ…Œ์ŠคํŠธํ•œ๋‹ค.
  • Repository โ†’ ํ†ตํ•ฉํ…Œ์ŠคํŠธ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

3. Nested Test

  • ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด์„œ @Nested๋ฅผ ํ™œ์šฉํ•œ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  • ์˜ˆ์‹œ
    @DisplayName("OrderService ํด๋ž˜์Šค")
    @ExtendWith(SpringExtension.class)
    @ContextConfiguration(classes = OrderService.class)
    class OrderServiceTest {
    
        @Nested
        @DisplayName("orderCartByCartIdAndUserId ๋ฉ”์†Œ๋“œ๋Š”")
        class orderCartByCartIdAndUserIdMethod {
    
            @Test
            @DisplayName("cartId์™€ userId๋ฅผ ๋ฐ›์•„, ์ฃผ๋ฌธ์— ์„ฑ๊ณตํ•˜๋ฉด, cartId๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.")
            void returnCartIdWhenOrderSuccess() {
                // given, when, then ... 
      
            }
        }
    }

4. Test Coverage

  • ํ…Œ์ŠคํŠธ ์ปค๋ฒ„๋ฆฌ์ง€์˜ ๋ชฉํ‘œ๋Š” 70%๋กœ ํ•œ๋‹ค.
  • ๋‹จ DTO, Lombok ๋“ฑ ์ž๋™์ƒ์„ฑ๋˜๊ฑฐ๋‚˜ ํ…Œ์ŠคํŠธ๊ฐ€ ํ•„์š”์—†๋Š” ๊ฒฝ์šฐ ํ…Œ์ŠคํŠธ ์ปค๋ฒ„๋ฆฌ์ง€์—์„œ ์ œ์™ธํ•œ๋‹ค.
  • Coverage แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2023-08-20 แ„‹แ…ฉแ„’แ…ฎ 3 36 05
Clone this wiki locally