SlideShare uma empresa Scribd logo
1 de 13
CLEAN CODE
문체가 수시로 바꾸면 책을 읽기 어렵다. 마찬가지로 코드 형식의 일관성이 없으면 코드 가독성이 떨어진다. 코드의 가독성은 코드의 품질을 유지하는데 지대한 영향을 미친다.
적절한 소스 파일의 크기는 얼마? FitNesse의 경우를 살펴보면 평균 200줄그리고 500줄을 넘지않는 소스 파일로도 	50,000줄에 육박하는 시스템을 구축할 수 있다.
신문 기사는 주제, 개요, 세부 사항순으로 전개된다. 마찬가지로 소스 파일도 적절한 주제를 파일 이름으로 처음에는 고차원적 개념과 알고리즘을 설명하고 마지막에 저차원 함수와 세부 내역이 나오도록 하자.
책을 쓸 때 문단을 구분하듯이 코드를 작성할 때도 개념 사이를 빈 행으로 구분하라. package fitnesse.wikitext.widget; import java.util.regex.*; Public class BoldWidget extends ParentWidget {    public static final String REGEXP = “’’’.+?’’’”’    private static final Pattern pattern = Pattern.compile(“’’’(.+?)’’’”, Pattern.MULTILINE +Pattern.DOTALL    );    public BoldWidget(ParentWidget parent, String text()) throws  Exception    {        super(parent);        Matcher match = pattern.matcher(text); match.find(); addChildWidgets(match.group(1));    }     public String render() throws Exception    { StringBuffer html = new StringBuffer(“<b>”); html.append(childHtml()).append(“</b>”);        return html.toString();    } } package fitnesse.wikitext.widget; import java.util.regex.*; Public class BoldWidget extends ParentWidget{    public static final String REGEXP = “’’’.+?’’’”’    private static final Pattern pattern = Pattern.compile(“’’’(.+?)’’’”, Pattern.MULTILINE +Pattern.DOTALL);     public BoldWidget(ParentWidget parent, String text()) throws  Exception{         super(parent);        Matcher match = pattern.matcher(text); match.find(); addChildWidgets(match.group(1));}     public String render() throws Exception{ StringBuffer html = new StringBuffer(“<b>”); html.append(childHtml()).append(“</b>”);        return html.toString();    } }
반면에 서로 연관성이 있다면, 코드를 세로로 가까이 놓아라. 연관성이 높은 가까이 두지 않으면 코드를 읽기 위해서 스크롤을 위,아래로 뒤지거나 이 파일, 저 파일을 오가면서 코드를 읽어야한다.
지역 변수는 사용하는 위치에서 최대한 가까이 선언한다. 인스턴스 변수는 세로 거리가 없이 클래스의 시작 또는 마지막에 선언한다.
한 함수가 다른 함수를 호출하는 것과 같이 종속성이 있다면 세로로 가까이 배치한다. 호출 함수를 먼저 배치하고 피호출 함수를 그 다음에 배치한다.
개념적인 친화도가 높은 코드는 가까이 배치한다. EX) public class Assert {    static public void assertTrue(String message, boolean condition)    {        if(!condition)            fail(message);    }     static public void assertTrue(boolean condition)    { assertTrue(null, condition);    }     static public void assertFalse(String message, boolean condition)    { assertTrue(message, !condition);    }    static public void assertFalse(booleancondtion)    { assertFalse(null, condtion);    }     /*…*/ };
한 행의 길이는 모니터에서 가로 스크롤이 생기지 않을 정도로 하자. 가로 공백은 밀접한 개념과 느슨한 개념을 표현한다. 공백이 있으면 두 요소가 나눠진다는 사실이 분명해진다. 가로 정렬은 없어도 된다.
정보의 범위에 따라서 들여쓰기를 하라. EX) Public class FitNesseServer implements SocketServer { private FitNesseContextconstext; public FitNesseServer(FitNesseContext context) { this.context = context; } public void serve(Socket s) { serve(s, 10000); } public void serve(Socket s, long requestTimeout) { try {  FitNesseExpediter sender = new FitNesseExpediter(s, context); sender.setRequestParsingTimeLimit(requestTimeout); sender.start(); } catch(Exception e) { e.printStatckTrace(); } } } vs Public class FitNesseServer implements SocketServer {     private FitNesseContextconstext;      public FitNesseServer(FitNesseContext context)     {  this.context= context;     }     public void serve(Socket s)     {         serve(s, 10000);     }     public void serve(Socket s, long requestTimeout)     {         try        {   FitNesseExpeditersender = new FitNesseExpediter(s, context); sender.setRequestParsingTimeLimit(requestTimeout); sender.start();         }         catch(Exception e)         {  e.printStatckTrace();         }     }  }
간단한 if문, 짧은 while문과 같이 코드가 짧을 때 들여쓰기를 무시하고픈 유혹이 생지만 들여쓰기를 꼭 하라. Public class CommentWidget extends TextWidget {    public static final String REGEXP = “^#[^]*(?:(?:)||)?”;     public CommentWidget(ParentWidget parent, String text) { super(parent, text); }    public String render() throws Exception { return “”; } } vs Public class CommentWidget extends TextWidget {     public static final String REGEXP = “^#[^]*(?:(?:)||)?”;     public CommentWidget(ParentWidget parent, String text)     {         super(parent, text);     }     public String render() throws Exception    {         return “”;    } }
끝

Mais conteúdo relacionado

Destaque

Clean code(03)
Clean code(03)Clean code(03)
Clean code(03)규열 김
 
Clean code chapter11 - systems
Clean code   chapter11 - systemsClean code   chapter11 - systems
Clean code chapter11 - systemsitomcc
 
Clean code chapter1
Clean code chapter1Clean code chapter1
Clean code chapter1ukjinkwoun
 
Tobi 스프링 2장 php version
Tobi 스프링 2장   php versionTobi 스프링 2장   php version
Tobi 스프링 2장 php versionukjinkwoun
 
만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장
만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장
만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장ukjinkwoun
 
깨끗한 코드 (클린 코드, Clean Code)
깨끗한 코드 (클린 코드, Clean Code)깨끗한 코드 (클린 코드, Clean Code)
깨끗한 코드 (클린 코드, Clean Code)Jay Park
 

Destaque (9)

Clean code(03)
Clean code(03)Clean code(03)
Clean code(03)
 
Clean code chapter11 - systems
Clean code   chapter11 - systemsClean code   chapter11 - systems
Clean code chapter11 - systems
 
Clean code chapter1
Clean code chapter1Clean code chapter1
Clean code chapter1
 
Tobi 스프링 2장 php version
Tobi 스프링 2장   php versionTobi 스프링 2장   php version
Tobi 스프링 2장 php version
 
만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장
만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장
만들면서 배우는 Cocos2d x 멀티 플랫폼 게임 프로그래밍 10-11장
 
Clean code Chapter.2
Clean code Chapter.2Clean code Chapter.2
Clean code Chapter.2
 
Chean code chapter 1
Chean code chapter 1Chean code chapter 1
Chean code chapter 1
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
깨끗한 코드 (클린 코드, Clean Code)
깨끗한 코드 (클린 코드, Clean Code)깨끗한 코드 (클린 코드, Clean Code)
깨끗한 코드 (클린 코드, Clean Code)
 

Semelhante a Clean code(05)

[2012 02 03]clean_code 5장
[2012 02 03]clean_code 5장[2012 02 03]clean_code 5장
[2012 02 03]clean_code 5장Jong Pil Won
 
2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여
2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여
2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여iamprogrammerofficial
 
Programming skills 1부
Programming skills 1부Programming skills 1부
Programming skills 1부JiHyung Lee
 
PHP 7의 새로운 특징과 기능 요약
PHP 7의 새로운 특징과 기능 요약PHP 7의 새로운 특징과 기능 요약
PHP 7의 새로운 특징과 기능 요약정아 손
 
Effective c++(chapter 5,6)
Effective c++(chapter 5,6)Effective c++(chapter 5,6)
Effective c++(chapter 5,6)문익 장
 
Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초Park Jonggun
 
GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출
GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출
GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출GDG Korea
 
Hello Swift 4/5 : Closure and Enum
Hello Swift 4/5 : Closure and EnumHello Swift 4/5 : Closure and Enum
Hello Swift 4/5 : Closure and EnumCody Yun
 
Multi-thread : producer - consumer
Multi-thread : producer - consumerMulti-thread : producer - consumer
Multi-thread : producer - consumerChang Yoon Oh
 
읽기 좋은 코드가 좋은 코드다 Part one
읽기 좋은 코드가 좋은 코드다   Part one읽기 좋은 코드가 좋은 코드다   Part one
읽기 좋은 코드가 좋은 코드다 Part oneJi Hun Kim
 
MongoDB 하루만에 끝내기
MongoDB 하루만에 끝내기MongoDB 하루만에 끝내기
MongoDB 하루만에 끝내기Seongkuk Park
 
[Swift] Protocol (2/2)
[Swift] Protocol (2/2)[Swift] Protocol (2/2)
[Swift] Protocol (2/2)Bill Kim
 
[Swift] Generics
[Swift] Generics[Swift] Generics
[Swift] GenericsBill Kim
 
[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5Hyeonseok Yang
 
Hacosa js study 2주차
Hacosa js study 2주차Hacosa js study 2주차
Hacosa js study 2주차Seong Bong Ji
 
자바프로그래머를 위한 스칼라
자바프로그래머를 위한 스칼라자바프로그래머를 위한 스칼라
자바프로그래머를 위한 스칼라Jong Gook Bae
 

Semelhante a Clean code(05) (20)

[2012 02 03]clean_code 5장
[2012 02 03]clean_code 5장[2012 02 03]clean_code 5장
[2012 02 03]clean_code 5장
 
2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여
2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여
2015 나는 프로그래머다 컨퍼런스 (11) 염산악 - 가독성에 대하여
 
Programming skills 1부
Programming skills 1부Programming skills 1부
Programming skills 1부
 
Scala
ScalaScala
Scala
 
PHP 7의 새로운 특징과 기능 요약
PHP 7의 새로운 특징과 기능 요약PHP 7의 새로운 특징과 기능 요약
PHP 7의 새로운 특징과 기능 요약
 
Effective c++(chapter 5,6)
Effective c++(chapter 5,6)Effective c++(chapter 5,6)
Effective c++(chapter 5,6)
 
Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초Start IoT with JavaScript - 1.기초
Start IoT with JavaScript - 1.기초
 
GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출
GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출
GKAC 2015 Apr. - Battery, 안드로이드를 위한 쉬운 웹 API 호출
 
Hello Swift 4/5 : Closure and Enum
Hello Swift 4/5 : Closure and EnumHello Swift 4/5 : Closure and Enum
Hello Swift 4/5 : Closure and Enum
 
Multi-thread : producer - consumer
Multi-thread : producer - consumerMulti-thread : producer - consumer
Multi-thread : producer - consumer
 
읽기 좋은 코드가 좋은 코드다 Part one
읽기 좋은 코드가 좋은 코드다   Part one읽기 좋은 코드가 좋은 코드다   Part one
읽기 좋은 코드가 좋은 코드다 Part one
 
MongoDB 하루만에 끝내기
MongoDB 하루만에 끝내기MongoDB 하루만에 끝내기
MongoDB 하루만에 끝내기
 
[Swift] Protocol (2/2)
[Swift] Protocol (2/2)[Swift] Protocol (2/2)
[Swift] Protocol (2/2)
 
[Swift] Generics
[Swift] Generics[Swift] Generics
[Swift] Generics
 
[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5[Study]HeadFirst JSP&servlet chapter5
[Study]HeadFirst JSP&servlet chapter5
 
Hacosa js study 2주차
Hacosa js study 2주차Hacosa js study 2주차
Hacosa js study 2주차
 
[Codelab 2017] ReactJS 기초
[Codelab 2017] ReactJS 기초[Codelab 2017] ReactJS 기초
[Codelab 2017] ReactJS 기초
 
JDK 변천사
JDK 변천사JDK 변천사
JDK 변천사
 
Java(2/4)
Java(2/4)Java(2/4)
Java(2/4)
 
자바프로그래머를 위한 스칼라
자바프로그래머를 위한 스칼라자바프로그래머를 위한 스칼라
자바프로그래머를 위한 스칼라
 

Clean code(05)

  • 2. 문체가 수시로 바꾸면 책을 읽기 어렵다. 마찬가지로 코드 형식의 일관성이 없으면 코드 가독성이 떨어진다. 코드의 가독성은 코드의 품질을 유지하는데 지대한 영향을 미친다.
  • 3. 적절한 소스 파일의 크기는 얼마? FitNesse의 경우를 살펴보면 평균 200줄그리고 500줄을 넘지않는 소스 파일로도 50,000줄에 육박하는 시스템을 구축할 수 있다.
  • 4. 신문 기사는 주제, 개요, 세부 사항순으로 전개된다. 마찬가지로 소스 파일도 적절한 주제를 파일 이름으로 처음에는 고차원적 개념과 알고리즘을 설명하고 마지막에 저차원 함수와 세부 내역이 나오도록 하자.
  • 5. 책을 쓸 때 문단을 구분하듯이 코드를 작성할 때도 개념 사이를 빈 행으로 구분하라. package fitnesse.wikitext.widget; import java.util.regex.*; Public class BoldWidget extends ParentWidget { public static final String REGEXP = “’’’.+?’’’”’ private static final Pattern pattern = Pattern.compile(“’’’(.+?)’’’”, Pattern.MULTILINE +Pattern.DOTALL ); public BoldWidget(ParentWidget parent, String text()) throws Exception { super(parent); Matcher match = pattern.matcher(text); match.find(); addChildWidgets(match.group(1)); } public String render() throws Exception { StringBuffer html = new StringBuffer(“<b>”); html.append(childHtml()).append(“</b>”); return html.toString(); } } package fitnesse.wikitext.widget; import java.util.regex.*; Public class BoldWidget extends ParentWidget{ public static final String REGEXP = “’’’.+?’’’”’ private static final Pattern pattern = Pattern.compile(“’’’(.+?)’’’”, Pattern.MULTILINE +Pattern.DOTALL); public BoldWidget(ParentWidget parent, String text()) throws Exception{ super(parent); Matcher match = pattern.matcher(text); match.find(); addChildWidgets(match.group(1));} public String render() throws Exception{ StringBuffer html = new StringBuffer(“<b>”); html.append(childHtml()).append(“</b>”); return html.toString(); } }
  • 6. 반면에 서로 연관성이 있다면, 코드를 세로로 가까이 놓아라. 연관성이 높은 가까이 두지 않으면 코드를 읽기 위해서 스크롤을 위,아래로 뒤지거나 이 파일, 저 파일을 오가면서 코드를 읽어야한다.
  • 7. 지역 변수는 사용하는 위치에서 최대한 가까이 선언한다. 인스턴스 변수는 세로 거리가 없이 클래스의 시작 또는 마지막에 선언한다.
  • 8. 한 함수가 다른 함수를 호출하는 것과 같이 종속성이 있다면 세로로 가까이 배치한다. 호출 함수를 먼저 배치하고 피호출 함수를 그 다음에 배치한다.
  • 9. 개념적인 친화도가 높은 코드는 가까이 배치한다. EX) public class Assert { static public void assertTrue(String message, boolean condition) { if(!condition) fail(message); } static public void assertTrue(boolean condition) { assertTrue(null, condition); } static public void assertFalse(String message, boolean condition) { assertTrue(message, !condition); } static public void assertFalse(booleancondtion) { assertFalse(null, condtion); } /*…*/ };
  • 10. 한 행의 길이는 모니터에서 가로 스크롤이 생기지 않을 정도로 하자. 가로 공백은 밀접한 개념과 느슨한 개념을 표현한다. 공백이 있으면 두 요소가 나눠진다는 사실이 분명해진다. 가로 정렬은 없어도 된다.
  • 11. 정보의 범위에 따라서 들여쓰기를 하라. EX) Public class FitNesseServer implements SocketServer { private FitNesseContextconstext; public FitNesseServer(FitNesseContext context) { this.context = context; } public void serve(Socket s) { serve(s, 10000); } public void serve(Socket s, long requestTimeout) { try { FitNesseExpediter sender = new FitNesseExpediter(s, context); sender.setRequestParsingTimeLimit(requestTimeout); sender.start(); } catch(Exception e) { e.printStatckTrace(); } } } vs Public class FitNesseServer implements SocketServer { private FitNesseContextconstext; public FitNesseServer(FitNesseContext context) { this.context= context; } public void serve(Socket s) { serve(s, 10000); } public void serve(Socket s, long requestTimeout) { try { FitNesseExpeditersender = new FitNesseExpediter(s, context); sender.setRequestParsingTimeLimit(requestTimeout); sender.start(); } catch(Exception e) { e.printStatckTrace(); } } }
  • 12. 간단한 if문, 짧은 while문과 같이 코드가 짧을 때 들여쓰기를 무시하고픈 유혹이 생지만 들여쓰기를 꼭 하라. Public class CommentWidget extends TextWidget { public static final String REGEXP = “^#[^]*(?:(?:)||)?”; public CommentWidget(ParentWidget parent, String text) { super(parent, text); } public String render() throws Exception { return “”; } } vs Public class CommentWidget extends TextWidget { public static final String REGEXP = “^#[^]*(?:(?:)||)?”; public CommentWidget(ParentWidget parent, String text) { super(parent, text); } public String render() throws Exception { return “”; } }
  • 13.