SlideShare a Scribd company logo
1 of 11
CLEAN CODE
변수를 private로 설정하고 멤버 함수를 둔다고 클래스가 되는 것은 아니다. 그보다 추상 인터페이스를 제공해 사용자가 구현을 모른 채 자료의 핵심을 조작할 수 있어야진정한 클래스다. 변수를 private로 설정하고 모든 변수에 조회/설정 함수를 추가하는 것은 public변수와 같다.
No SilverBullet 모든 것을 객체지향으로 풀려고 하지 마라. 때로는 단순한 자료 구조와 절차적 코드가 가장 적합한 상황도 있다.
객체는 자료를 숨기고함수를 공개한다. 디미터 법칙 “클래스 C의 메소드 f는 다음 객체의 메소드만 호출해야 한다.” ,[object Object]
f가 생성한 객체
f의 인자로 넘어온 객체
C 인스턴스 변수에 저장된 객체쉽게 말하자면 낯선 사람은 경계하고 친구하고만 놀라는 의미다.
final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath(); 위와 같은 코드를 기차충돌이라고 하고 디미터 법칙을 위배한다. 만약 final String outputDir = ctxt.Options.scratchDir.absolutePath; 와 같이 일반 자료 구조라면 디미터 법칙을 위배한 것은 아니다.
자료 구조와 객체만 존재한다면 간단하지만 단순한 자료 구조에도 조회/설정 함수를 정의하라고 요구하는 프레임워크와 표준이 존재한다. 따라서 객체와 자료 구조가 혼재된 구조가 발생한다. 이런 구조는 결국 악용되고 새로운 함수와 새 자료 구조도 추가하기 어려워진다.
만약 위의 예가 디미터 법칙을 위반하지 않으려면 내부구조를 감춰야 한다. ctxt.getAbsolutePathOfScratchDirectoryOption(); 위의 방법은 추상화는 되었지만 ctxt가 알아야할 메소드가 많아진다. ctxt.getScratchDirectoryOption().getAbsolutePath(); 위의 방법은 ctxt.getScratchDirectoryOption()이 객체가 아니라 자료 구조를 반환해야한다. 어느 방법도 썩 내키지 않는다.
절대 경로는 왜 필요한가? 다음은 같은 모듈에서 가져온 코드다. String outFile = outputDir + “/” + className.replace(‘.’,’/’)+”.class”; FileOutputStreamfout = new FileOutputStream(outFile); BufferedOutputStreambos = new BufferedOutputStream(fout); 절대 경로가 필요한 이유가 임시 파일을 생성하기 위해서다. ctxt에게 책임을 맡기자. BufferedOutputStreambos = ctxt.createScratchFileStream(classFileName); 이제 ctxt의 내부 구조가 드러나지 않으므로 디미터 법칙을 따른다.

More Related Content

Viewers also liked

Auto green(Automatic greenhouse control system)
Auto green(Automatic greenhouse control system)Auto green(Automatic greenhouse control system)
Auto green(Automatic greenhouse control system)현우 이
 
Vodafone Youth Market Segmentation
Vodafone Youth Market SegmentationVodafone Youth Market Segmentation
Vodafone Youth Market Segmentationyaltsiner
 
14 Youth Marketing Digital Trends For Brands To Ride On!
14 Youth Marketing Digital Trends For Brands To Ride On!14 Youth Marketing Digital Trends For Brands To Ride On!
14 Youth Marketing Digital Trends For Brands To Ride On!Happy Marketer
 
Green Business Practices
Green Business PracticesGreen Business Practices
Green Business PracticesMelvin Pereira
 
Clean City, Clean India and Green World
Clean City, Clean India and Green WorldClean City, Clean India and Green World
Clean City, Clean India and Green WorldSunil Bhatt
 
So You Wanna Change the World?
So You Wanna Change the World?So You Wanna Change the World?
So You Wanna Change the World?Lab80
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with DataSeth Familian
 

Viewers also liked (8)

Auto green(Automatic greenhouse control system)
Auto green(Automatic greenhouse control system)Auto green(Automatic greenhouse control system)
Auto green(Automatic greenhouse control system)
 
Vodafone Youth Market Segmentation
Vodafone Youth Market SegmentationVodafone Youth Market Segmentation
Vodafone Youth Market Segmentation
 
14 Youth Marketing Digital Trends For Brands To Ride On!
14 Youth Marketing Digital Trends For Brands To Ride On!14 Youth Marketing Digital Trends For Brands To Ride On!
14 Youth Marketing Digital Trends For Brands To Ride On!
 
Green Business Practices
Green Business PracticesGreen Business Practices
Green Business Practices
 
Clean City, Clean India and Green World
Clean City, Clean India and Green WorldClean City, Clean India and Green World
Clean City, Clean India and Green World
 
So You Wanna Change the World?
So You Wanna Change the World?So You Wanna Change the World?
So You Wanna Change the World?
 
Goal setting ppt
Goal setting pptGoal setting ppt
Goal setting ppt
 
Visual Design with Data
Visual Design with DataVisual Design with Data
Visual Design with Data
 

Similar to Clean code(06)

[스프링 스터디 1일차] 템플릿
[스프링 스터디 1일차] 템플릿[스프링 스터디 1일차] 템플릿
[스프링 스터디 1일차] 템플릿AnselmKim
 
2014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #72014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #7Chris Ohk
 
객체지향 단어가 의미하는 것
객체지향 단어가 의미하는 것객체지향 단어가 의미하는 것
객체지향 단어가 의미하는 것jaypi Ko
 
The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표재정 이
 
Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4연우 김
 
Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Ryan Park
 
Effective c++(chapter 5,6)
Effective c++(chapter 5,6)Effective c++(chapter 5,6)
Effective c++(chapter 5,6)문익 장
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinDong Chan Shin
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4성연 김
 
프로그래밍 언어 기초(델파이,C++)
프로그래밍 언어 기초(델파이,C++)프로그래밍 언어 기초(델파이,C++)
프로그래밍 언어 기초(델파이,C++)Devgear
 
[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿해강
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinDong Chan Shin
 
Chapter7~9 ppt
Chapter7~9 pptChapter7~9 ppt
Chapter7~9 pptInjae Lee
 

Similar to Clean code(06) (14)

[스프링 스터디 1일차] 템플릿
[스프링 스터디 1일차] 템플릿[스프링 스터디 1일차] 템플릿
[스프링 스터디 1일차] 템플릿
 
2014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #72014-15 Intermediate C++ Study #7
2014-15 Intermediate C++ Study #7
 
객체지향 단어가 의미하는 것
객체지향 단어가 의미하는 것객체지향 단어가 의미하는 것
객체지향 단어가 의미하는 것
 
The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표
 
Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4Effective c++ 정리 chapter 4
Effective c++ 정리 chapter 4
 
Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005Working Effectively With Legacy Code - xp2005
Working Effectively With Legacy Code - xp2005
 
Effective c++(chapter 5,6)
Effective c++(chapter 5,6)Effective c++(chapter 5,6)
Effective c++(chapter 5,6)
 
Effective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshinEffective c++ chapter7_8_9_dcshin
Effective c++ chapter7_8_9_dcshin
 
Effective c++chapter4
Effective c++chapter4Effective c++chapter4
Effective c++chapter4
 
프로그래밍 언어 기초(델파이,C++)
프로그래밍 언어 기초(델파이,C++)프로그래밍 언어 기초(델파이,C++)
프로그래밍 언어 기초(델파이,C++)
 
[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿[아꿈사] The C++ Programming Language 13장 템플릿
[아꿈사] The C++ Programming Language 13장 템플릿
 
Effective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshinEffective c++ chapter1 2_dcshin
Effective c++ chapter1 2_dcshin
 
Chapter7~9 ppt
Chapter7~9 pptChapter7~9 ppt
Chapter7~9 ppt
 
Java script
Java scriptJava script
Java script
 

Clean code(06)

  • 2. 변수를 private로 설정하고 멤버 함수를 둔다고 클래스가 되는 것은 아니다. 그보다 추상 인터페이스를 제공해 사용자가 구현을 모른 채 자료의 핵심을 조작할 수 있어야진정한 클래스다. 변수를 private로 설정하고 모든 변수에 조회/설정 함수를 추가하는 것은 public변수와 같다.
  • 3. No SilverBullet 모든 것을 객체지향으로 풀려고 하지 마라. 때로는 단순한 자료 구조와 절차적 코드가 가장 적합한 상황도 있다.
  • 4.
  • 7. C 인스턴스 변수에 저장된 객체쉽게 말하자면 낯선 사람은 경계하고 친구하고만 놀라는 의미다.
  • 8. final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath(); 위와 같은 코드를 기차충돌이라고 하고 디미터 법칙을 위배한다. 만약 final String outputDir = ctxt.Options.scratchDir.absolutePath; 와 같이 일반 자료 구조라면 디미터 법칙을 위배한 것은 아니다.
  • 9. 자료 구조와 객체만 존재한다면 간단하지만 단순한 자료 구조에도 조회/설정 함수를 정의하라고 요구하는 프레임워크와 표준이 존재한다. 따라서 객체와 자료 구조가 혼재된 구조가 발생한다. 이런 구조는 결국 악용되고 새로운 함수와 새 자료 구조도 추가하기 어려워진다.
  • 10. 만약 위의 예가 디미터 법칙을 위반하지 않으려면 내부구조를 감춰야 한다. ctxt.getAbsolutePathOfScratchDirectoryOption(); 위의 방법은 추상화는 되었지만 ctxt가 알아야할 메소드가 많아진다. ctxt.getScratchDirectoryOption().getAbsolutePath(); 위의 방법은 ctxt.getScratchDirectoryOption()이 객체가 아니라 자료 구조를 반환해야한다. 어느 방법도 썩 내키지 않는다.
  • 11. 절대 경로는 왜 필요한가? 다음은 같은 모듈에서 가져온 코드다. String outFile = outputDir + “/” + className.replace(‘.’,’/’)+”.class”; FileOutputStreamfout = new FileOutputStream(outFile); BufferedOutputStreambos = new BufferedOutputStream(fout); 절대 경로가 필요한 이유가 임시 파일을 생성하기 위해서다. ctxt에게 책임을 맡기자. BufferedOutputStreambos = ctxt.createScratchFileStream(classFileName); 이제 ctxt의 내부 구조가 드러나지 않으므로 디미터 법칙을 따른다.
  • 12. 자료 구조의 전형적인 형태는 공개 변수만 있고 함수가 없는클래스다. 이런 구조를 Data Transfer Object(DTO) 또는 Plain of Data(POD)라고 한다. 이 구조체는 데이터베이스와 통신하거나 소켓에서 받은 메시지의 구문을 분석할 때 유용하다.
  • 13. 편견을 가지지 마라. 문제의 최적인 해결책을 선택하라.
  • 14.