Skip to content

๐Ÿšš ๋ฐฑ์—”๋“œ ์ปจ๋ฒค์…˜

๊น€๋ฏผ์ข… edited this page Aug 19, 2024 · 6 revisions

๐Ÿง‘โ€๐Ÿ’ปย ๊ฐœ๋ฐœ ์ปจ๋ฒค์…˜


1. ๋ฉ”์„œ๋“œ ์ˆœ์„œ

  • a() ๋ฉ”์„œ๋“œ๊ฐ€ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” b(), c() ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ”๋กœ ์•„๋ž˜์— ์œ„์น˜ํ•œ๋‹ค.
  • static ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ๋‹จ์— ์ž‘์„ฑํ•œ๋‹ค.
class Example { 
    public static void hello() {
        // ...
    }
    
    private static void hi() {
        // ...
    }
    
    public void a() {
        b();
        c();
    }
    
    private void b() {
        // ...
    }
    
    private void c() {
        // ...
    }
    
    public void d() {
        // ..
    }
}
  • d() ๋ฉ”์„œ๋“œ๋„ b() , c() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์ง€๋งŒ a() ๊ฐ€ ๋จผ์ € ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋‹ˆ a() ๋ฉ”์„œ๋“œ ์•„๋ž˜์— ์œ„์น˜ํ•œ๋‹ค.
class Example {

    public void a() {
        b();
        c();
    }
    
    private void b() {
        // ...
    }
    
    private void c() {
        // ...
    }
    
    public void d() {
        b();
        c();
    }
}

2. import๋ฌธ ๋‹จ๋ฝ

import๋ฌธ ๋‹จ๋ฝ์€ ์•„๋ž˜์˜ ์ˆœ์„œ๋กœ ์„ ์–ธํ•œ๋‹ค. ๋ธ”๋Ÿญ ์‚ฌ์ด์—๋Š” ์ค„๋ฐ”๊ฟˆ์„ ํ•œ๋‹ค. java, javax, jakarta, org, net, com, ๊ทธ ์™ธ

์‚ฌ์šฉ ์˜ˆ์‹œ

package roomescape.reservation.service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import roomescape.member.domain.Member;
import roomescape.member.dto.LoginMemberInToken;
import roomescape.member.repository.MemberRepository;
import roomescape.reservation.domain.FreeReservations;
import roomescape.reservation.domain.Payment;

3. static import ์‚ฌ์šฉ

  • Test ํ”„๋ ˆ์ž„์›Œํฌ๋Š” static import ์‚ฌ์šฉํ•œ๋‹ค. (ex: JUnuit & AssertJ Assertions)

4. final ์„ ์–ธ

  • ์žฌํ• ๋‹น๋˜์ง€ ์•Š๋Š” ๋กœ์ปฌ๋ณ€์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฉค๋ฒ„๋ณ€์ˆ˜์— final ์„ ์–ธ์„ ํ•œ๋‹ค.
  • catch ๋ธ”๋ก์—์„œ final ์„ ์–ธ์„ ํ•œ๋‹ค.
  • for-each ๋ธ”๋ก์—์„œ final ์„ ์–ธ์„ ํ•œ๋‹ค.
  • ํด๋ž˜์Šค ์„ ์–ธ, ๋ฉ”์„œ๋“œ ์„ ์–ธ์‹œ์—๋Š” final ์„ ์–ธ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
public int sum(final int x, final int y) {
    return x + y;
}
try {
    // ๋กœ์ง
} catch (final NullPointerException e) {

}
for (final int num : nums) {
    System.out.println(num);
}

5. ํƒ€์ž… ์ถ”๋ก  ์‚ฌ์šฉ ์œ ๋ฌด

  • ํƒ€์ž… ์ถ”๋ก (var)์€ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ณ€์ˆ˜ ํƒ€์ž…์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ ์–ธํ•œ๋‹ค.

6. ์ปค๋ฐ‹ ์ปจ๋ฒค์…˜

  • ํด๋ž˜์Šค๋ช… ์—†์ด ์•„๋ž˜์˜ ์ปค๋ฐ‹ ํƒœ๊ทธ์— ๋งž๊ฒŒ ์ž‘์„ฑํ•œ๋‹ค.
feat ์‹ ๊ทœ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ
refactor ๋ฆฌํŽ™ํ„ฐ๋ง
docs ๋ฌธ์„œ ์ž‘์—…
chore ์˜์กด์„ฑ ์ถ”๊ฐ€, ๊ธฐํƒ€ ์ž‘์—…
test ํ…Œ์ŠคํŠธ ๊ด€๋ จ ์ž‘์—…
fix ๋ฒ„๊ทธ ํ”ฝ์Šค
style ์ฝ”๋“œ ๋ณ€๊ฒฝ ์—†๋Š” ํฌ๋งทํŒ…, ์ฃผ์„ ์ œ๊ฑฐ์™€ ๊ฐ™์€ ์ž‘์—…
// ์˜ˆ์‹œ
feat: ์‚ฌ์šฉ์ž ๋„๋ฉ”์ธ ์ถ”๊ฐ€

7. ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ ์ด๋ฆ„๊ณผ DisplayName ์„ค์ •

  • DisplayName์€ ํ–‰์œ„์— ๋Œ€ํ•œ ์„œ์ˆ ์„ ํ•œ๋‹ค.
  • ๋ฉ”์„œ๋“œ์˜ ๋ช…์€ DisplayName์„ ์˜์–ด๋กœ ์ž‘์„ฑํ•œ๋‹ค.
  • ๊ตฌ์ฒด์ ์ธ ๋ฉ”์„œ๋“œ๋ช…์€ ๋“ค์–ด๊ฐ€์ง€ ์•Š๋Š”๋‹ค.
@DisplayName("์ธ์‚ฌ๋ฅผ ํ•œ๋‹ค.")
@Test
void sayHello() {
    Member member = new Member();
    
    assertThat(member.sayHello()).isEqualTo("Hello World");
}

8. ํด๋ž˜์Šค ์„ ์–ธ ํ›„ ์ค„๋ฐ”๊ฟˆ

  • ํด๋ž˜์Šค ์„ ์–ธ ํ›„ ์ค„๋ฐ”๊ฟˆ์„ ๋„ฃ์–ด์ค€๋‹ค.
public class Main {
    
    private int foo;
}

9. ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํ•„๋“œ์˜ ์ค„๋ฐ”๊ฟˆ

  • ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํ•„๋“œ๋Š” ์ค„๋ฐ”๊ฟˆ์„ ๋„ฃ์–ด์ฃผ๊ณ , ์•„๋‹Œ ํ•„๋“œ๋Š” ๊ธฐ์กด ๋ฐฉ์‹์ฒ˜๋Ÿผ ์ค„๋ฐ”๊ฟˆ ์—†์ด ์„ ์–ธํ•œ๋‹ค.
class Member {
    
    private static final int MAX_VALUE = 3;
    private static final int MIN_VALUE = 1;

    @Id
    @Column(name = "id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "email", nullable = false)
    private String email;
    
    @Column(name = "name", nullable = false)
    private String name;
    
    @Column(name = "password", nullable = false)
    private String password;
    
    @Column(name = "profile_photo", nullable = false)
    private String profilePhoto;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "role", nullable = false)
    private Role role;
    
    private int num1;
    private int num2;

}

10. ํด๋ž˜์Šค & ๋ฉ”์„œ๋“œ ๋ ˆ๋ฒจ ์–ด๋…ธํ…Œ์ด์…˜ ๋ฐฐ์น˜ ์ˆœ์„œ

  • ํด๋ž˜์Šค & ๋ฉ”์„œ๋“œ ๋ ˆ๋ฒจ์— ์‚ฌ์šฉ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜๋“ค์€ ํด๋ž˜์Šค & ๋ฉ”์„œ๋“œ ๊ด€๋ จ๋„๊ฐ€ ๋†’์„์ˆ˜๋ก ์•„๋ž˜์— ๋ฐฐ์น˜ํ•œ๋‹ค.

Entity

@Getter
@NoArgs
@Table
@Entity
public class Member {
}

Service

@Getter
@Required
@Service
public class Member {
}

Test Code

@DisplayName()
@Test
void test {
}

11. Lombok

๊ธฐ๋ณธ์ ์œผ๋กœ Lombok์„ ์‚ฌ์šฉํ•˜๋˜ ์•„๋ž˜ ๋ช…์‹œ๋œ ๊ฒƒ๋“ค๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • getter
  • noargs
  • builder
  • required
  • slf4j

12. ๊ฐ์ฒด ์ƒ์„ฑ์ž

  • ํ•„๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 3๊ฐœ ์ดํ•˜๋ผ๋ฉด new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด ์ƒ์„ฑํ•˜๊ณ , 4๊ฐœ ์ด์ƒ์ด๋ฉด Builder๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

ํ•„๋“œ๊ฐ€ 3๊ฐœ ์ดํ•˜์ผ ๊ฒฝ์šฐ

public class Main {
    
    private int a;
    private int b;
    private int c;
    
    private Main(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

ํ•„๋“œ๊ฐ€ 4๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ

public class Main {
    
    private int a;
    private int b;
    private int c;
    private int d;
    
    @Builder 
    private Main(int a, int b, int c, int d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }
}

13. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ จ ๋ฌธ์ž์—ด

ํ…Œ์ด๋ธ”, ์ปฌ๋Ÿผ๋ช… ๋Œ€๋ฌธ์ž๋กœ ํ†ต์ผํ•œ๋‹ค.

@Entity
@Table(name = "MEMBER")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member {

    public static final String DEFAULT_PROFILE_IMAGE_URL = "";

    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "EMAIL", nullable = false)
    private String email;
    
    @Column(name = "NAME", nullable = false)
    private String name;
    
    @Column(name = "PASSWORD", nullable = false)
    private String password;
    
    @Column(name = "PROFILE_PHOTO", nullable = false)
    private String profilePhoto;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "ROLE", nullable = false)
    private Role role;
  
}

๋„ค์ด๋ธŒ ์ฟผ๋ฆฌ, JPQL ์‚ฌ์šฉ์‹œ์—๋„ ๋Œ€๋ฌธ์ž๋กœ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค

@Query("""
        SELECT m
        FROM Member m
        WHERE m.id = :id
        """)
Optional<Member> findById(@Param("id") long id);

14. ๋ ˆ์ฝ”๋“œ ์ค„๋ฐ”๊ฟˆ

@Schema(description = "๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ ์ƒ์„ฑ ์š”์ฒญ ๋ฐ”๋””")
public record ReferenceLinkCreateRequest(
        @Schema(description = "๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ ๊ฐ’", example = "<https://www.naver.com>")
        @NotBlank(message = "๋นˆ url์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
        String url,
        
        @Schema(description = "๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ ๊ฐ’", example = "<https://www.naver.com>")
        @NotBlank(message = "๋นˆ url์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
        String name
) {
}
@Schema(description = "๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ ์ƒ์„ฑ ์š”์ฒญ ๋ฐ”๋””")
public record ReferenceLinkCreateRequest(@Schema(description = "๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ ๊ฐ’", example = "<https://www.naver.com>") 
                                         @NotBlank(message = "๋นˆ url์€ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.") 
                                         String url) {
}

์• ๋…ธํ…Œ์ด์…˜ ์—†์ด ํ•„๋“œ ๊ฐฏ์ˆ˜๊ฐ€ 2๊ฐœ ์ดํ•˜๋ผ๋ฉด ์ค„๋ฐ”๊ฟˆํ•˜์ง€ ์•Š๋Š”๋‹ค.

์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๊ฒฝ์šฐ ์ค„๋ฐ”๊ฟˆ์„ ํ•œ๋‹ค. ํ•„๋“œ๋งˆ๋‹ค๋„ ์ค„๋ฐ”๊ฟˆ์„ ํ•ด์ค€๋‹ค.

๋‹จ, ํ•„๋“œ์— ์—๋…ธํ…Œ์ด์…˜์ด 1๊ฐœ๋ผ๋ฉด ์˜ˆ์™ธ ์ ์œผ๋กœ ์ฒซ์ค„์„ ์ œ์™ธํ•˜๊ณ  ์ค„๋ฐ”๊ฟˆ์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.

public record PairRoomRequest(
        @Schema(description = "ํŽ˜์–ด๋ฃธ ์ ‘๊ทผ ์ฝ”๋“œ", example = "abcdef") String accessCode, 
        @Schema(description = "ํŽ˜์–ด๋ฃธ ์ ‘๊ทผ ํ† ํฐ", example = "abcdef") String accessToken
) {
}

15. ์ปจํŠธ๋กค๋Ÿฌ ๋ฉ”์„œ๋“œ ๋„ค์ด๋ฐ

์˜ˆ์‹œ) Member ์ปจํŠธ๋กค๋Ÿฌ

16. CURD

  • C โ†’ createMember
  • R โ†’ getMember, getMembers
  • U โ†’ updateMember
  • D โ†’ deleteMember

17. ์ปจํŠธ๋กค๋Ÿฌ DTO์—์„œ๋Š” Wrapper class ์‚ฌ์šฉ

  • Spring validation message ์ถœ๋ ฅ ์šฉ์ด์„ฑ์„ ์œ„ํ•จ.
  • ๊ทธ ์™ธ ํ•˜์œ„ ๊ฐœ์ธต์—์„œ๋Š” primitive type์„ ์‚ฌ์šฉํ•œ๋‹ค. (long, int, byte, double, float ๋“ฑ๋“ฑ)
    • ๋ถˆ๋ณ€ ๊ฐ์ฒด ๋ณ€ํ™˜ ์šฉ์ด์„ฑ
    • NPE ๋ฐœ์ƒ ๋ฐฉ์ง€
    • ์กฐ๊ธˆ์˜ ์„ฑ๋Šฅ ๊ฐœ์„ 
    • ์˜ˆ์™ธ) ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐ•์ œ ์‚ฌํ•ญ์ผ ์‹œ ์ œ์™ธ (e.g, hibernate)

18. DTO๋Š” ์„œ๋น„์Šค์— ๋‘”๋‹ค.(To be update...)

๐Ÿ€ ์ฝ”๋”ฉํ•ด๋“€์˜ค

์ „์ฒด

์ •๋ณด

BE

FE

๊ธฐ์ˆ 

์ปจ๋ฒค์…˜

ํ…Œ์ŠคํŠธ

์„ฑ๋Šฅ ๊ฐœ์„  & ์ ‘๊ทผ์„ฑ ๊ฐœ์„  ๋ฆฌํฌํŠธ

์ธํ”„๋ผ

Clone this wiki locally