CleanCode Chapter 3:Functions
Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)
- ä½è : Robert C. Martin
- åºç社/ã¡ã¼ã«ã¼: Prentice Hall
- çºå£²æ¥: 2008/08/01
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- è³¼å ¥: 1人 ã¯ãªãã¯: 62å
- ãã®ååãå«ãããã° (21件) ãè¦ã
Chapter 1 ã¯å®éã«è²·ã£ã¦èªãã§ãããã¨ãã¦ãChapter 2 ã® Meaningful Names ã¯å½ããåããã¦é£ã°ãã¨ãã¦ãChapter 3 ã® Functions ã®ææ³ (ææ³ï¼)ã
Java 以å¤ã®è¨èªã«å½ã¦ã¯ã¾ãé¨åãå¤ããã©ãã¨ããããã«ãã´ãªã¯ Java ã§ã
è¡æ°
Kent Beck ã¯å
¨ã¦ã®é¢æ° (Every function) ã 2ã4 è¡ãªãã ãããªã
å¤å 1 è¡ã®é¢æ°ã 5 è¡ä»¥ä¸ã®é¢æ°ãæ¸ããã¨ã¯ãããã ãããã©ãå²åã¨ãã¦ã¯ç¡è¦ã§ãããããå°ãªããã ãããªã
1 è¡ã®é¢æ°ãå«ã¾ãã¦ãªãã£ã¦ãã¨ã¯ãåç´ãª getter/setter ã¯ã»ã¨ãã©æ¸ããªããã£ã¦ãã¨ã ã¨æãã
å®è£
ãã¿ã¼ã³ã§ããããªãã¨è¨ã£ã¦ããã
æ½è±¡åº¦ã®éãå¦çãã¾ããªã
èªã¿ã«ãããªããããããã¯ãã¿ã¼ã³æåãªãã¡ã¯ã¿ãªã³ã°å
¥éã§ããã£ãã¯ãã
ã§ãå¾¹åºãããã¨ãã¦ããªããªãé£ãããã¨ãªãã ããã¼ã
switch æãããã㯠if-else if ã®é£é
switch æã¯æ¬è³ªçã« N åã®ãã¨ãããã®ã§ãDo One Thing ã«åããããã£ã¦ä»£ãã«ããªã¢ã¼ãã£ãºã 使ããã¨ã
ä¾å¤ã¨ãã¦ãããªã¢ã¼ãã£ãã¯ãªãªãã¸ã§ã¯ãã®çæãè¡ãé¢æ°å
é¨ã§ã®ä½¿ç¨ãæãã¦ããã
åå
çãã¦ã¯ã±ã¯ã«ã©ã³ååãããé·ãã¦èª¬æçãªååãä»ãããã¨ã
ãã ãè±èªããã¤ãã£ãã«ä½¿ã£ã¦ã人ã ã£ããæ°ã«ãªããã®ãããããªããã©ãè±èªã§æ¸ãããé·ãååãè¦ãã ãã§èªãæ°ã失ããã¨ãè¨ããããªäººãããããã§ãä½äºãã»ã©ã»ã©ãããã¨æããªããç¡ãã
ãã¼ã¯ã¼ãå¼æ°ãããè¨èªã ã¨ãé·ã£ããããé¢æ°åã®ä¸é¨ããã¼ã¯ã¼ãã«æ¼ãä»ãããã¨ãã§ãããã ãã©ãIDE ã®æ¯æ´ãåãã«ãããªãããã§ãããå¾®å¦ã
æµãããããªã¤ã³ã¿ã¼ãã§ã¤ã¹ã«ããã®ãæã ãã©ã確å®ã«æéã¯å¢ããä¸ãå¼ã³åºãå´ã¯åããããããããããªããã©å®ç¾©ã¯é£èªåãããããããªæ°ãããã
IDE ã®æ¯æ´ãåããã¦ãæéãããã»ã©æ°ã«ãªãããå®ç¾©ãèªã¿ãããæ¹æ³ã¨ãªãã¨ãå¼æ°ç¨ã®ã¯ã©ã¹ãä½ããã¨ãã«ãªããã ãããã
ä¾ãã°ã
// static Start _(int value)ã¿ãããªã¡ã½ããä½ã£ãæ¹ããããã StringUtils.substring("hogehoge", new Start(4), new Length(2)); StringUtils.substring("hogehoge", new Start(2), new End(4));
ã»ã»ã»æéãæ°ã«ãªããª*1ã
ãã¨ãwrite(name) ãã writeField(name) ã®æ¹ããããããã
çç±ã¨ãã¦ã"write name" ãããªãã¦ã "written name" ã ããã£ã¦ãã¨ãè¨ã£ã¦ããã ãã©ãæ¥æ¬äººã«ã¯ write(name) ã®æ¹ãåãããããããããªããã¨æããã ãã©ã©ãã ããã
ã§ãwriteField(name) ã¯ãã¼ã¯ã¼ããååã«å«ãã¦ã (field == name) ä¾ã«ãªã£ã¦ããããªãã ãã©ãåãçç±ãã assertEquals 㯠assertExpectedEqualsActual ã®æ¹ããããã ãããªãã»ã»ã»ãããç¡çã
å¼æ°
å¼æ°ã®åæ°*2ã¨ãã¦ã¯ãçæ³ã¯ 0ã次㫠1 ã¤ã¨ 2 ã¤*3ã3 ã¤ã¯ã§ããã°é¿ããããããããã以ä¸ã¯ãã»ã©ã®çç±ãç¡ãéãå´ä¸ã
ããã¨ããã©ã°å¼æ°ã¯éãã¨æè¨ãã¦ããã
ã¾ãããã¯ãã®éãã ã¨æããjava.io.FileOutputStream ã®ã³ã³ã¹ãã©ã¯ã¿ã«ç¬¬äºå¼æ°ã¨ãã¦æ¸¡ãã boolean ã ãã©ãä¸æ¸ãããã®ã true ãªã®ããæ°è¦ã«ä½ãã®ã true ãªã®ããä»ã§ãè¿·ããã¨ãããã
ãã®æ¬ã§ã¯ãrender(boolean isSuite) ã£ã¦ã¡ã½ããããã£ãããrenderForSuite() 㨠renderForSingleTest() ã«åããã¹ããã¨ãã¦ãããã©ãã³ã³ã¹ãã©ã¯ã¿ã«ã¯ååãä»ããããªããããstatic ãªãã¡ã¯ããªã¡ã½ãããç¨æãããã¨ã«ãªãã
ä»ã«ããenum ãä½ã£ã¦é©åãªååãã¤ããæ¹æ³ãããã
// FileOutputStream(File/String, boolean)ãããã±ã¼ã¸ãã©ã¤ãã¼ãã«ãã¦ã // FileOutputStream(File/String, IfExistsThen)ãpublicã§è¿½å ããã¤ã¡ã¼ã¸ // ã¾ããæ¨æºAPIãå¼ããã¨ã¯ç¾å®çã§ã¯ãªãã®ã ããã©ã»ã»ã» // OutputStreamãFileOutputStreamãç¶æ¿ããæ°ããã¯ã©ã¹ä½ãã®ãä¸çªç¾å®çã㪠package java.io; public enum IfExistsThen { APPEND { FileOutputStream create(File f) throws FileNotFoundException { return new FileOutputStream(f, true); } }, OVER_WRITE { FileOutputStream create(File f) throws FileNotFoundException { return new FileOutputStream(f, false); } }; // FileOutputStreamã®ã³ã³ã¹ãã©ã¯ã¿ããå¼ã°ããã¤ã¡ã¼ã¸ abstract FileOutputStream create(File f) throws IOException; }
ãããªæãããã©ã°ã«ãããããåä½ã追å ãããã¨ããå ´åã«ã³ã³ã¹ãã©ã¯ã¿ãå¢ãããªãããããªããã©ãenum ã 㨠enum ã«è¿½å ããã ããä¾ãã°ã
public enum IfExistsThen { APPEND { FileOutputStream create(File f) throws FileNotFoundException { return new FileOutputStream(f, true); } }, OVER_WRITE { FileOutputStream create(File f) throws FileNotFoundException { return new FileOutputStream(f, false); } }, THROW_EXCEPTION { FileOutputStream create(File f) throws FileNotFoundException { if (f.exists()) throw new IllegalStateException(); return new FileOutputStream(f); } }; abstract FileOutputStream create(File f) throws IOException; }
åºåå¼æ°
å¼æ°ã¯é¢æ°ã¸ã®å
¥åã¨è§£éããã®ãä¸çªèªç¶ãªãã ãããåºåå¼æ°ã¯ä½¿ããªãã¨ã
åºåå¼æ°ã使ããããªã£ããããªãã¸ã§ã¯ãã«ãããã ã㦠obj.method() ã®å½¢ã«ãããã¨ã
çç±ã®ä¸ã¤ã«ããããåºåãå ¥åãåãããªããã£ã¦ã®ãæãã¦ãããã©ãC# ã¿ããã«å¼ã³åºãå´ã§ãå®ç¾©å´ã§ããããåºåå¼æ°ã¨åããå ´åã¯å¿ è¦ãããã°ä½¿ãã°ããã¨æãã
ã¡ãªã¿ã«æ¬ã§ã¯ StringBuilder ã appendFooter ã¡ã½ããã«æ¸¡ãä¾ãæãã¦ããã ãã©ãããããããæ¹ããããããªãã¨åçã
ãªãã¸ã§ã¯ãã®ç¶æ ãå¤æ´ããé¢æ°ã¨ããªãã¸ã§ã¯ãã«é¢ããæ å ±ãåå¾ããé¢æ°ã¯åãã
ã¾ããåºæ¬åããã¹ããªãã ãããã©ããã«ãã¹ã¬ãããèããã¨ãå¾®å¦ã
æ¬ã§ä¾ã«æãããã¦ããã®ã¯ set ã¨ããååã®ã¡ã½ãã㧠check then act ãè¡ã£ã¦ããã®ã§ãã©ã¡ããã¨ããã¨ååãæªããããªæ°ãããã
詳ããã¯Java 並è¡å¦çããã°ã©ãã³ã°åç
§ã»ã»ã»ã£ã¦ããã®æ¬ãã絶çããããä»ã«çµ¶çã«ãã¹ãæ¬ã¯ãããã§ãããã ããã«ã»ã»ã»
try-catch ãããã¯ã®æ½åº
é¢æ°ã¯ä¸ã¤ã®ãã¨ã ããããã¹ãã§ãä¾å¤å¦çã¯ã¾ãã«ãä¸ã¤ã®ãã¨ããªã®ã§ãé¢æ°ãã¨ã©ã¼ãã³ããªã³ã°ãã¦ãããã以å¤ã®ãã¨ã¯ãããªãã¨ããã¡ãã£ã¨éæ¿ã«è¦ããæè¦ã
ã¤ã¾ãã
void hoge() { // tryã使ããªãtryã®åã«ã¯ä½ãæ¸ããªã try { // ä¾å¤ãçºçããã³ã¼ããæ½åºããã®ã§å¼ã³åºãã ã hogehoge(); } catch (SomeException e) { // ä¾å¤å¦ç ... } // catch(ãããã¯finally)ã®å¾ãã«ãä½ãæ¸ããªã }
ãããªæãã«ããã¨ãããã¨ã
ã§ããå®éã«è©¦ãã¦ã¿ããã³ã¼ããã¨ã¦ãèªã¿ããããªã£ããããããã¯ãã²ã¨ãããã¹ãã
ã¡ãªã¿ã«å®éã«æ¬ã§ç´¹ä»ããã¦ãä¾ã¯ãããªæãã
public void delete(Page page) { try { deletePageAndAlReferences(page); } catch (Exception e) { logError(e); } } private void deletePageAndAlReferences(Page page) throws Exception { deletePage(page); registry.deleteReferences(page.name); configKeys.deleteKey(page.name.makeKey()); }CleanCode (P.47)
ä¾åæ§ãã°ããã
Error.java ã¨ããErrorCode.java ã¨ãããããªãã®ãä½ããªãã¨ãã話ã
ãããªãã®ãä½ã£ã¦ãã¾ãã¨ããããã£ããã¡ã¤ã«ã¯ä»ã®ããããã®ã¯ã©ã¹ãã import ãã使ãããããããã®ãã¡ã¤ã«ã«ä¿®æ£ãå ããéã«ã»ã¨ãã©å
¨é¨ã®ã¯ã©ã¹ã®åã³ã³ãã¤ã«ãåé
ç½®ãå¿
è¦ã«ãªã£ã¦ãã¾ã (ãããèè
㯠dependency magnet ã¨å¼ãã§ãã)ã
ã§ããã®ãã¨ãã Error.java ãªã©ã¨ãã£ããã¡ã¤ã«ã¸ã®ä¿®æ£ã追å ãé¿ããããã«ãªã£ã¦ãä½ãããã gdgd ã«ãªããã¨ã
å人çã«ã¯ãError.java ã ãã«éãããMessage.java ã ã¨ããConstant.java ã ã¨ããã¨ã«ããä½ããããçªã£è¾¼ãã å®æ°ã¯ã©ã¹ã¯ä½ããªãã¨ããããã
ã§ããªããããã¯ã©ã¹å/å
±éå/å
±éé¨ååãã®åã®ä¸ã«ãçã£å
ã«ä½ãããã¯ã©ã¹ãªãã ããªã
ããã¦ã³ã¼ã*4ã Excel ã§ç®¡çãã£ã¨ãâãã°ããã®ã«ã
å®éã«ã¯ä»ã«ãè²ã
ã¨æ¸ãã¦ãããããæ°ã«ãªã£ã人ã¯è²·ãã¨ãããã
Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin Series)
- ä½è : Robert C. Martin
- åºç社/ã¡ã¼ã«ã¼: Prentice Hall
- çºå£²æ¥: 2008/08/01
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- è³¼å ¥: 1人 ã¯ãªãã¯: 62å
- ãã®ååãå«ãããã° (21件) ãè¦ã
*1:ç¹ã« Start ã¨ã End ã¨ã Length ã¨ãã®å®ç¾©ãã»ã¨ãã©ä¸ç·ã«ãªãããD ã®ã¿ãããªå¼·ã typedef ã欲ããããããããã° Start ã End ã Length ã int ã typedef ãã¨ãã°ãã
*2:å¯å¤é·å¼æ°ã¯ 1 ã¤ã¨æ°ãããä¾ãã°ãString.format(String, Object...) ãªãå¼æ°ã®æ°ã¯ 2 ã¤
*3:1 ã¤ã®æ¹ã 2 ã¤ãããæã¾ãã
*4:ã½ã¼ã¹ã³ã¼ããããªãã¦ãã¨ã©ã¼ã³ã¼ãã¨ãã¡ãã»ã¼ã¸ã³ã¼ã