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 “”; } }