책책책 책을 읽읍시다
-
엘레강트 오브젝트 - 퍼플릭 상수(Public Constant)를 사용하지 말자책책책 책을 읽읍시다/프로그래밍 2023. 8. 1. 23:35
'상수(constant)'라고도 불리는 public static final 프로퍼티는 객체 사이에 데이터를 공유하기 위해 사용하는 매우 유명한 메커니즘이다. 글자 그대로 상수를 사용하는 이유는 데이터를 공유(또는 다른 객체를 공유)하기 위해서인데, 객체들은 어떤 것도 공유해서는 안된다. 대신 독립적이어야 하고 '닫혀 있어야(closed)'한다. 간단하게 말해서 상수를 이용한 공유 메커니즘은 캡슐화와 객체지향적인 사고 전체를 부정하는 일이다. 다음 예제는 구조화된 데이터를 Writer 객체에 기록하고, 각 줄을 '개행(new line) 문자'로 종료하는 메서드를 나타낸 것이다. public class Records { private static final String EOL = "\r\n"; void wr..
-
엘레강트 오브젝트 - 주 생성자 재활용책책책 책을 읽읍시다/프로그래밍 2023. 8. 1. 00:07
응집도가 높고 견고한 클래스에는 적은 수의 메서드와 상대적으로 더 많은 수의 생성자가 존재한다. 생성자의 개수가 더 많을수록 클래스는 더 개선되고, 사용자 입장에서 클래스를 더 편하게 사용할 수 있다. 아래와 같이 Cash란 클래스가 있고 다양한 방법으로 Cash 인스턴스를 생성하고 싶은 경우 다음과 같이 여러 종류의 생성자를 활용한다. public class Cash { private int dollars; public Cash(int dollars) { this.dollars = dollars; } public String usd() { return String.format("$ %d", this.dollars); } } new Cash(30); new Cash("$29.95"); new Cash(2..
-
엘레강트 오브젝트 - 접미사 er 사용을 자제하자책책책 책을 읽읍시다/프로그래밍 2023. 7. 30. 22:57
저자 : Yegor Bugayenko 엮자 : 조영호 들어가며 저자는 순수 OOP를 지향한다. 이러한 방식은 무조건 옳다라고 말하는 점에서 명쾌하기도 하고 너무 극단적이라 난해하기도 했던게 흥미로웠다. 이보다도 엮자 서문이 더 흥미로웠다. 보통은 책 내용에 동조하며 좋은 면을 위주로 소개하는데 반해 이 책을 읽을 때 주의할 점 먼저 설명한다. 엮자인 조영호 님의 저서인 '객체지향의 사실과 오해', '오브젝트'을 읽으며 객체지향에 대한 생각이 일관되게 잘 정리된 흔들리지 않을 느낌을 받았는데, 저자도 생각이 다르지만 결은 같은 사람 같았다. 좌파 정치인이 우파 정치인의 책을 출간한 느낌이랄까? 소개된 내용 중 실무에서 했던 내 과오와 연결되는 것이 많아서 반성문 쓴다라는 느낌으로 정리해보았다. -er로 끝..
-
이펙티브 자바 : Checked Exception vs. UncheckedException책책책 책을 읽읍시다/프로그래밍 2023. 7. 21. 21:33
자바는 문제 상황을 알리는 타입(throwable)으로 검사 예외(Checked Exception), 런타임 예외(UncheckedException), 에러, 이렇게 세 가지를 제공하는데, 언제 무엇을 사용해야 하는지 헷갈려 하는 프로그래머들이 종종 있다. 언제나 100% 명확한 건 아니지만 이럴 때 참고하면 좋은 멋진 지침들이 있다. 호출하는 쪽에서 복구하리라 여겨지는 상황이라면 검사 예외를 사용하라. 검사와 비검사 예외를 구분하는 기본 규칙이다. 검사 예외를 던지면 호출자가 그 예외를 catch로 잡아 처리하거나 더 바깥으로 전파하도록 강제하게 된다. 따라서 메서드 선언에 포함된 검사 예외 각각은 그 메서드를 호출했을 때 발생할 수 있는 유력한 결과임을 API 사용자에게 알려주는 것이다. 달리 말하면..
-
이펙티브 자바 : 박싱 타입 vs. 기본 타입책책책 책을 읽읍시다/프로그래밍 2023. 7. 18. 21:49
자바의 데이터 타입은 크게 두 가지로 나눌 수 있다. 바로 int, double, boolean 같은 기본 타입과 String, List 같은 참조 타입이다. 그리고 각각의 기본 타입에 대응하는 참조 타입이 하나씩 있으며, 이를 박싱된 기본 타입이라고 한다. 예컨대 int는 Integer, double은 Double, boolean은 Boolean이다. 기본 타입과 박싱된 기본 타입의 주된 차이는 크게 세 가지이다. 첫 번째, 기본 타입은 값만 가지고 있으나, 박싱된 기본 타입은 값에 더해 식별성(identity)이란 속성을 갖는다. 달리 말하면 박싱된 기본 타입의 두 인스턴스는 값이 같아도 서로 다르다고 식별될 수 있다. 두 번째, 기본 타입의 값은 언제나 유효하나, 박싱된 기본 타입은 유효하지 않은 ..
-
이펙티브 자바 : 제네릭 클래스와 제네릭 메서드책책책 책을 읽읍시다/프로그래밍 2023. 7. 7. 22:57
제네릭 클래스 아래와 같은 비(非)제네릭 스택 클래스가 있다고 해보자. public class Stack { private Object[] elements; private int size = 0; private static final int DEFAULT_INITIAL_CAPACITY = 16; public Stack() { elements = new Object[DEFAULT_INITIAL_CAPACITY]; } public void push(Object e) { ensureCapacity(); elements[size++] = e; } public Object pop() { if (size == 0) throw new EmptyStackException(); Object result = element..
-
이펙티브 자바 : static 멤버 클래스책책책 책을 읽읍시다/프로그래밍 2023. 7. 5. 22:04
중첩 클래스(nested class)란 다른 클래스 안에 정의된 클래스를 말한다. 중첩 클래스는 자신을 감싼 바깥 클래스에서만 쓰여야 하며, 그 외의 쓰임새가 있다면 톱레벨 클래스로 만들어야 한다. 중첩 클래스의 종류는 정적 멤버 클래스, (비정적) 멤버 클래스, 익명 클래스, 지역 클래스, 이렇게 네 가지다. 이 중 첫 번째를 제외한 나머지는 내부 클래스(inner class)에 해당한다. 이번에는 각각의 중첩 클래스를 언제 그리고 왜 사용해야 하는지 알아보자. 정적 멤버 클래스 vs. 비정적 멤버 클래스 정적 멤버 클래스는 다른 클래스 안에 선언되고, 바깥 클래스의 private 멤버에도 접근할 수 있다는 점만 제외하고는 일반 클래스와 똑같다. 정적 멤버 클래스는 다른 정적 멤버와 똑같은 접근 규칙을..
-
이펙티브 자바 - 정적 팩터리 메서드책책책 책을 읽읍시다/프로그래밍 2023. 6. 22. 23:46
저자 : 조슈아 블로크(Joshua Bloch) 옮긴이 : 이복연 들어가며 2015년 11번가 인턴 때 중도포기, 2017년쯤 이랜드에서도 중도포기... 영화든 책이든 재미없더라도 끝까지 가고 보는 성격인데 왠지 이 책만큼은 재미있음에도 완독을 못했다. 이번에는 꼭 성공하길 바라는 마음에서 완독 전에 먼저 블로그 글을 올리는 배수진을 쳤다. 그땐 몰랐는데 지금은 당연하게 실무에서 적용하고 있는 패턴이 여기에 소개되어있어 신기했다. 그만큼 내용적으로 훌륭해서 다른 곳에서 재생산되어 나에게 왔다는 의미로 받아들여졌다. 알고 있는 패턴들을 다시 찬찬히 보며 생각할게 많아져서 이번에는 소단원 단위로 정리하겠다. 정적 팩터리 메서드 너무나도 유명하므로 장단점만 간단하게 짚어본다. 장점 첫째, 이름을 가질 수 있다..