전체 글
-
15장 - 리시버를 명시적으로 참조하라Kotlin/Effective Kotlin 요약 2023. 2. 13. 00:01
This 대표적으로 함수와 프로퍼티를 지역 또는 톱 레벨 변수가 아닌 다른 리시버로부터 가져온다는 것을 나타낼 때가 있습니다. 예를 들어 클래스의 메서드라는 것을 나타내기 위한 this가 있습니다. class User: Person(){ private var beersDrunk: Int = 0 fun drinkBeers(num: Int){ this.beersDrunk += num } } 명시적으로 표현하지 않음 vs 명시적으로 표현 fun List.quickSort(): List{ if(size < 2) return this ... } 명시적이지 않은 구현입니다. fun List.quickSort(): List{ if(this.size < 2) return this ... } 명시적인 구현입니다. 두 함..
-
14장 - 변수 타입이 명확하지 않은 경우 확실하게 지정하라Kotlin/Effective Kotlin 요약 2023. 2. 12. 00:01
타입 추론 시스템 코틀린은 개발자가 타입을 지정하지 않아도 자동으로 타입을 추론하여 넣어줍니다. 이를 통해 코드가 짧아지며 개발시간을 줄여줄 수 있습니다. 하지만 타입이 명확하지 않다면 남용하지 않는것이 좋습니다. val num = 10 val data = getSomeData() num을 타입은 Int입니다. data의 타입은 무엇일까요? 코드를 읽으면서 함수 정의를 보며 타입을 확인하면 되지 않나? 라고 생각할 수 있습니다. 하지만 이는 곧 가독성이 떨어진다는 것을 의미합니다. val data: UserData = getSomeData() 이와 관련되어 아이템3: 최대한 플랫폼 타입을 사용하지 말라, 아이템4: inferred 타입으로 리턴하지 말라가 있습니다. 타입은 개발자와 컴파일러 모두에게 중요..
-
13장 - Unit?을 리턴하지 말라Kotlin/Effective Kotlin 요약 2023. 2. 11. 00:01
일반적으로 Unit?을 사용한다는 것 Unit은 공식문서에서 볼 수 있듯 한 가지 값을 가질 수 있는 타입을 나타내는 객체이다. Java에서 void 와 비슷한 타입으로 볼 수 있다. Unit?을 하면 null을 반환하거나 아무것도 반환하지 않습니다. 지금까지 여러 코드를 보면서 Unit?을 쉽게 읽을 수 있는 경우는 거의 보지 못했습니다. 기본적으로 Unit?을 리턴하거나, 이를 기반으로 연산하지 않는 것이 좋습니다.
-
12장 - 연산자 오버로드 할 때는 의미에 맞게 사용하라Kotlin/Effective Kotlin 요약 2023. 2. 10. 00:01
팩토리얼을 구하는 함수 fun main() { print(6.factorial()) } fun Int.factorial(): Int = (1..this).product() fun Iterable.product(): Int = fold(1) {acc, i -> acc * i} //출력결과 : 720 팩토리얼은 다음과 같이 ! 기호를 사용해 표기하는 것을 우리는 알고 있습니다. 코틀린은 이런 연산자를 지원하지 않지만 다음과 같이 연산자 오버로딩을 활용하면 만들어낼 수 있습니다. fun main() { print(!6) print(6.not()) } operator fun Int.not() = factorial() fun Int.factorial(): Int = (1..this).product() fun It..
-
11장 - 가독성을 목표로 설계하라Kotlin/Effective Kotlin 요약 2023. 2. 9. 00:01
개요 널리 알려진 이야기로 개발자가 코드를 작성하는데 1분 걸리지만 읽는 데는 10분이 걸립니다. 실제로 프로젝트를 진행할 때 코드 한 줄 고치기 위해 몇 주 동안 코드를 살피는 상황도 많습니다. 프로그래밍은 쓰기보다 읽기가 더 중요하며 가독성을 항상 고려하며 코드를 작성해야 합니다. 인식 부하 감소 //구현 A if(person != null && person.isAdult){ view.showPerson(person) } else { view.showError() } //구현 B person?.takeIf {it.isAduilt} ?.let(view::showPerson) ?: view.showError() A가 B보다 읽기 쉽습니다. B는 경험많은 코틀린 개발자라면 코드를 쉽게 읽을 수 있지만 대부..
-
JPA 복합키 엔티티JPA 2023. 2. 8. 00:01
개요 JPA에서 단일 PK가 아닌 2개 이상의 PK를 가질 경우 어떻게 구성하는지 알아보고자 합니다. MySQL 테이블 데이터 create table my_table ( a varchar(50) not null, b varchar(20) not null, c varchar(50), constraint pk_my_table primary key (a, b) ); 테이블 복합키를 담고 있는 식별자 클래스 생성 data class MyTableId( val a: String, val b: String, ) : Serializable{ constructor() : this("", "") } @IdClass를 사용하기 위한 식별자 클래스를 생성할 때는 다음과 같은 조건을 만족해야 합니다. - 식별자 클래스의 변수..
-
SpringBatch Cursor 기반 vs Paging 기반프로젝트/스프링 배치 튜토리얼 2023. 2. 7. 00:01
개요 SpringBatch의 ItemReader에 Cursor기반, Paging 기반이 존재한다는 것을 알게 되었습니다. 각각은 어떤 것인지 찾아보고 더 적합한 것을 적용해보고자 합니다. Spring Batch의 ItemReader Spring Batch의 Chunk Tasklet은 아래와 같은 과정을 통해 진행됩니다. Spring Batch의 ItemReader는 데이터를 읽어들입니다. 꼭 DB의 데이터만을 이야기하는 것은 아니며 File, XML, JSON 등 다른 데이터 소스를 배치 처리의 입력으로 사용할 수 있습니다. 보통 Spring Batch에서 쓰이는 itemReader로서 가장 대표적인 구현체로 JdbcPagingItemReader가 있습니다. 해당 클래스의 계층 구조는 다음과 같습니다. ..
-
Spring 버전에 맞는 Dependency 버전 찾기Spring Framework 2023. 2. 6. 00:01
개요 Spring Batch 애플리케이션을 다루는 중 JPA에서 Cursor가 지원되지 않는다는 글을 보았습니다. 하지만 Spring Batch v4.3.0이 릴리즈 되면서 JpaCursorItemReader가 도입되었습니다. 이렇게 되면서 Spring Batch 4.3 버전을 사용하기 위해서 Spring Boot는 최소 몇 버전을 사용해야 할까? 라는 의문점에서 시작되어 Spring 버전에 맞는 Dependency 버전을 찾아보고자 합니다. Dependency 버전 찾기 1. 스프링부트 사이트에 들어가서 사용하려는 스프링 부트 버전의 Reference Doc. 을 클릭 https://spring.io/projects/spring-boot#learn Spring Boot Commercial support..