CleanCode Chapter 4:Comments
Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)
- ä½è : Robert C. Martin
- åºç社/ã¡ã¼ã«ã¼: Prentice Hall
- çºå£²æ¥: 2008/08/01
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- è³¼å ¥: 1人 ã¯ãªãã¯: 62å
- ãã®ååãå«ãããã° (21件) ãè¦ã
この前のエントリã«å¼ãç¶ããCleanCode ããä»æ¥ã¯ Chapter 4:Comments ã®ææ³ã¨ããããæ¦è¦ (æ¦è¦ï¼)ã
ã³ã¡ã³ãã ãã§ä¸ç« 使ã£ã¦èª¬æãã¦ãæ¬ã¯ç¥ãéãã§ã¯ãã®æ¬ã®ã¿ (ã®ã¯ã)ã
ããã³ã¡ã³ã
æçãªã³ã¡ã³ãã ããããªãã¦ãå¿
è¦ãªã³ã¡ã³ãããããã³ã¡ã³ã (Good Comments)ãã¨ãããããã«ãã¦ããã
ããã³ã¡ã³ãã®ä¸è¦§ã
- èä½æ¨©è¡¨ç¤ºã¨ãã©ã¤ã»ã³ã¹ã¨ãä½è ãªã©ã¨ãã£ããLeagal Comment
- æ å ±ãä¸ããã³ã¡ã³ã (Informative Comment)
- æå³ã®èª¬æ
- 説æ (Clarification)
- é大ãªè¦å
- TODO ã³ã¡ã³ã
- å¢å¹ (Amplification)
- public 㪠API ã®ããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ã
èä½æ¨©è¡¨ç¤ºã¨ãã©ã¤ã»ã³ã¹ã¨ãä½è ãªã©ã¨ãã£ããLegal Comment
ã§ãå¯è½ãªãå¤ã«åºãã
æ å ±ãä¸ããã³ã¡ã³ã (Informative Comment)
// Returns an instance of the Responder begin tested. protected abstract Responder responderInstance();CleanCode
ã§ãååãå¤æ´ããã°å¿ è¦ãªããªããã¨ãå¤ããä¸ã®ä¾ã§ã¯ãresponderBeingTested ã«å¤æ´ããã¨ãã
æå³ã®èª¬æ
ããªããããããããããªãããããªãããã«ã¤ãã¦ã®ã³ã¡ã³ãã®ãã¨ã
説æ (Clarification)
ããã¯ä¾ãè¦ã¦ãããã®ãä¸çªåãããããã
public void testCompareTo() throws Exception { WikiPagePath a = PathParser.parse("PageA"); WikiPagePath ab = PathParser.parse("PageA.PageB"); WikiPagePath b = PathParser.parse("PageB"); WikiPagePath aa = PathParser.parse("PathA.PathA"); WikiPagePath bb = PathParser.parse("PathB.PathB"); WikiPagePath ba = PathParser.parse("PathB.PathA"); assertTrue(a.compareTo(a) == 0); // a == a assertTrue(a.compareTo(b) != 0); // a != b assertTrue(ab.compareTo(ab) == 0); // ab == ab assertTrue(a.compareTo(b) == -1); // a < b assertTrue(aa.compareTo(ab) == -1); // aa < ab assertTrue(ba.compareTo(bb) == -1); // ba < bb assertTrue(b.compareTo(a) == 1); // b > a; assertTrue(ab.compareTo(aa) == 1); // ab > aa assertTrue(bb.compareTo(ba) == 1); // bb > ba }CleanCode
ãããªã³ã¡ã³ãããã ãcompareTo ã«é¢ãã¦ã¯ã常㫠0 ã¨ã®å¤§å°æ¯è¼ãè¡ãã°ããã»ã©ã³ã¡ã³ããå¿ è¦ã ã¨ã¯æããªããªãã
a.compareTo(b) == 0; // a == b ã®æå³ a.compareTo(b) < 0; // a < b ã®æå³ a.compareTo(b) > 0; // a > b ã®æå³ a.compareTo(b) <= 0; // a <= b ã®æå³ a.compareTo(b) >= 0; // a >= b ã®æå³
ãããªæãã«ãä¸çå·ã®åã㨠a 㨠b ã®é çªã¨ãä¸è´ããã
ããã¨ãæ¬ã§ã¯ãã®ç¨®ã®ã³ã¡ã³ãã®ãªã¹ã¯ã«ã¤ãã¦ãè¨åãã¦ããã
ã©ããããã¨ãã¨ããã¨ããã®ç¨®ã®ã³ã¡ã³ããééã£ã¦ããå ´åããããæ£ãã®ãé常ã«å°é£ã¨ãããã¨ã
ä¾ãã°ãå¼ç¨ããã³ã¡ã³ããã©ããééã£ã¦ããå ´åãèãã¦ã¿ãã¨ããã
ãå©ç¨ã¯è¨ç»çã«ã
é大ãªè¦å
ãããä¾ãè¦ãã®ãåãããããã
// Don't run unless you // have some time to kill. public void _testWithReallyBigFile() { writeLinesToFile(10000000); response.setBody(testTile); response.readyToSend(this); String responseString = output.toString(); assertSubString("Content-Length: 10000000", responseString); assertTrue(bytesSent > 10000000); }CleanCode
ä¸å¿èª¬æãã¦ããã¨ãJUnit3 ã§ã¯ãtest ããã¯ãã¾ãã¡ã½ãããå®è¡ããããã«ãªã£ã¦ãã¦ãä¸ã®ä¾ã§ã¯ã¢ã³ãã¼ãã¼ãä»ãã¦ãããããã¹ããç¡å¹åããã¦ãããã§ããªãã§ç¡å¹åããã¦ãããã¨ããã®ãã³ã¡ã³ãã«æ¸ãã¦ããã
ãã ãæ¬ã®ä¸ã«ããããã«ãJUnit4 ã§ã¯ã¢ããã¼ã·ã§ã³ã使ã£ã¦ã
@Test @Ignore("Takes too long to run") public void testWithReallyBigFile() { ... }
ã¨ããããã«å¤æ´ãããã
ã³ã¡ã³ããããªãã¦ã¢ããã¼ã·ã§ã³ã使ããã¨ã§ãããã°ã©ã å´ããå¼ããã¨ãã§ããã®ã§ãããããç¨éã®ã³ã¡ã³ãã¯ã©ãã©ãã¢ããã¼ã·ã§ã³ã«ç½®ãæããã°ããã¨æãã
TODO ã³ã¡ã³ã
TODO ã³ã¡ã³ãã¯èª¬æããå¿
è¦ã¯ãªãã¨æãã
ä¸è¬ç㪠IDE ã ã£ãã TODO ã³ã¡ã³ãããªã¹ãã¢ãããã¦ããããã強調表示ãã¦ããããããã®ã§æ´»ç¨ãã¹ããä¹±ç¨ã¯ãããªããã©ã*1ã
å¢å¹ (Amplification)
é大ãªè¦åã®å¼±ãçï¼æå³ã®èª¬æã¨ãåãããããªã»ã»ã»ããããããã
public 㪠API ã®ããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ã
ä»ã®ã³ã¡ã³ãã¨åãããã«ãããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ãã誤解ã»nonlocalã»ééããå¯è½æ§ã¯ããã®ã§ã注æããããã¨ã
æªãã³ã¡ã³ã
ã»ã¨ãã©ã®ã³ã¡ã³ãããã®ã«ãã´ãªã«å±ãããããªã
æªãã³ã¡ã³ãã®ä¸è¦§ã
- ä½è¨ã£ã¦ããåãããªãã³ã¡ã³ã
- åé·ãªã³ã¡ã³ã (Redundant Comments)
- 誤解ãæãã³ã¡ã³ã
- å¼·å¶ãããã³ã¡ã³ã
- å±¥æ´ (Journal Comments)
- ãã¤ãº (Noise Comments)
- ãã£ã¨ãããããªãã¤ãº (Scary Noise)
- é¢æ°ãå¤æ°ã使ãã°ãããããªã³ã¡ã³ã
- ãã¼ã«ã¼
- éãæ¬å¼§ã®ã³ã¡ã³ã
- å±æ§ãç½²å
- ã³ã¡ã³ãã¢ã¦ã
- HTML ã³ã¡ã³ã
- ãµãããããªãå ´æã«è¨è¿°ãããã³ã¡ã³ã
- é¥èãããã³ã¡ã³ã
- 説æã®è¶³ããªãã³ã¡ã³ã
- é¢æ°ã®ããã
- public ã§ã¯ãªãã³ã¼ãã®ããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ã
- ä¾ç¤º
ãã¤ãºã¨ãã£ã¨ãããããªãã¤ãºãã©ãéããããããã
åé·ãªã³ã¡ã³ã (Redundant Comments)
ä¾ãã°ãä½ããã£ã¦ããã説æãããããªã³ã¡ã³ãã
æ¬ã§ã¯ã
// Utility method that returns when this.closed is true. Throws an excetion // if the timeout is reached. public synchronized void waitForClose(final long timeoutMillis) throws Exception { if (!closed) { wait(timeoutMillis); if (!closed) throw new Exception("MockResponseSender could not be closed"); } }CleanCode
ãããªä¾ã®ä»ã«ãåé·ã§å½¹ã«ç«ããªãããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ãã«ã¤ãã¦ã Tomcat ã®ã³ã¼ããä¾ã«æãã¦è¨åãã¦ããã
ãã ããã®é¨åã¯ä»ã¨ãã¶ãã®ã§å¾ã§ã
å¼·å¶ãããã³ã¡ã³ã
ã³ã¼ãã£ã³ã°è¦ç´ã¨ãã§ãå¿ ãããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ããè¨è¿°ãããã¨ãã¨ãããã¨ã
/** * @param workCode ä½æ¥ã³ã¼ã * @param worker ä½æ¥è */ public void startWork(String workCode, Worker worker) { ... }
ã¿ãããªä½ã®å½¹ã«ãç«ããªããããªã³ã¡ã³ããæ¸ããããããã¨ãã話ã
å½¹ã«ç«ããªãã ããªãã¾ã ããããããªãã®ã大éã«ããã¨ãã³ã¼ããåããã¦ãã¾ã£ã¦èªã¿ã«ãããªãã
å±¥æ´ (Journal Comments)ãå±æ§ãç½²å
ãããå¼·å¶ãããã«æ¸ãã¦ãããã°ã©ããä¸ä½ã©ãã»ã©ãããã ãããå½ç¶ããã¼ã¸ã§ã³ç®¡çã·ã¹ãã ã使ã£ãã»ããããã
ã¡ãªã¿ã«ãå±¥æ´ (Journal Comments) ã¯ãã¡ã¤ã«ã®å
é ã«æ¸ããããããªã³ã¡ã³ããå±æ§ãç½²åã£ã¦ã®ã¯ããããããããã¾ã§ã¯ã ãã ãã追å ãããã¨ãããããã£ãé¡ã®ã³ã¡ã³ãã®ãã¨ã
ãã¤ãº (Noise Comments)
/** * Default constructor. */ protected AnnualDateRule() { }/** The day of the month. */ private int dayOfMonth;/** * Returns the day of the month. * * @return the day of the month. */ public int getDayOfMonth() { return dayOfMonth; }CleanCode
ãããªããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ããæ¸ããè¦ãããã¾ãããï¼
ãã£ã¨ãããããªãã¤ãº (Scary Noise)
ä¸ã¨ã®éãã¯è¯ãããããªãã
/** The name. */ private String name; /** The version. */ private String version; /** The licenceName. */ private String licenceName; /** The version. */ private String info;CleanCode
ããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ãã¯ç°¡åã«ãã¤ãºã«ãªãã¨ãã話ãinfo ã®ã³ã¡ã³ãã¯ã³ãããã¦ãã®ã¾ã¾ä¿®æ£ãããã«æ®ã£ã¦ãã¦ãããããã³ã¡ã³ãããScary Noiseãã§ããã¨è¨ã£ã¦ããã®ããªï¼
ã¨ã«ãããä¸ã¨åããã¦èããã¨ããååè¦ã¦æãããªãã®ã«ããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ããæ¸ãã¦ãããã¤ãºã«ãããªããªããã¨ãããã¨ã ã¨æãã
ã¾ãããã¯å®éãã®éãã§ãä¾ãã°ã
- final 㪠getter/setter ãªã©ã®åç´ãªã¡ã½ãã
- public/protected 以å¤ã®ã¡ã³ã
- é public ãªã¯ã©ã¹ã® public ãªã¡ã½ãã*2
ãªããã¯ããã¥ã¡ã³ãã¼ã·ã§ã³ã³ã¡ã³ãã¯ä¸è¦ããã
éãæ¬å¼§ã®ã³ã¡ã³ã
ããã«ã¤ãã¦ã¯Clean Code vs. プログラミングのセオリー - ぐるぐる〜ã§æ¸ãããã©ãã³ã¡ã³ãã®ãChapter 3: Function ã«æ¸ãããå
容ãã¯éã£ã¦ãw
ã§ãã
try to shorten your function instead.
CleanCode
é©å½è¨³ï¼ãããã«é¢æ°ãçãããããã«åªåãã¹ãã
ã£ã¦è¨ã£ã¦ãããããã£ã±ãé¢æ°å ã®è©±ãªã®ã¯ééããªããã©ã
HTML ã³ã¡ã³ã
確ãã«ãã¿ã°ã§ãã¡ããã¡ããã¦ãä¸ãå®ä½åç
§ããä½ããã使ããã ãã¨ãç´æ¥èªãã®ã¯ã¤ããããªã¼ã
åºåã HTML ã ããã¨è¨ã£ã¦ãå
¥åã¾ã§ HTML ã§ããå¿
è¦ã¯ãªããèªã¿ã«ããä¸æ¸ãã«ãããªãã¦ãããã¨ãããªãããã ãã
é¢æ°ã®ããã
çãé¢æ°ã«ã¯ã³ã¡ã³ããªãã¦ä¸è¦ã ãã¨ãã話ãã¾ã確ãã«ãçãé¢æ°ã«æ°è¡ã®ã³ã¡ã³ãæ¸ãã®ã¯éçç£çã ããªããã¨æãã
çãé¢æ°ã«ã¯ãããã«å¿ããåãããããååããä»ãã¦ããã°ã³ã¡ã³ãã¯ä¸è¦ã
Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)
- ä½è : Robert C. Martin
- åºç社/ã¡ã¼ã«ã¼: Prentice Hall
- çºå£²æ¥: 2008/08/01
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- è³¼å ¥: 1人 ã¯ãªãã¯: 62å
- ãã®ååãå«ãããã° (21件) ãè¦ã