Javadocãæ¸ã
æè¿ã¯æéãä½ã£ã¦Effective Javaã®2çãããã§ã¾ãã
- ä½è : Joshua Bloch
- åºç社/ã¡ã¼ã«ã¼: Prentice Hall
- çºå£²æ¥: 2008/05/08
- ã¡ãã£ã¢: ãã¼ãã¼ããã¯
- è³¼å ¥: 6人 ã¯ãªãã¯: 65å
- ãã®ååãå«ãããã° (42件) ãè¦ã
ã»ã¨ãã©1çã¨åãå 容ã§ããã"Item 44: Write doc comments for all exposed API elements" ãèªãã§ããã¾ã¨ã¾ã£ã¦ããªã¨æã£ãã®ã§ã触çºããã¦ã¡ã¢ãã¦ãã«ç§ã®ããæ¹ãã
å¼ç¨ã®2段è½ç®ã¯åºæ¬çã«è¶ ç´ã
ã©ãã«æ¸ãã
If an API is to be usable, it must be documented.
ã¦ã¼ã¶ãå©ç¨å¯è½ãªãã¹ã¦ã®APIã«Javadocãæ¸ãã
ããã¯ã¨ããããå¿ é ã ã¨æãã¾ããã¡ãªã¿ã«ç§ã¯ãã®è¾ºã«ã¯å¿ ãå ¥ããããã«å¿ããã¦ã¾ãã
- public ãªåããã£ã¼ã«ããã³ã³ã¹ãã©ã¯ã¿ãã¡ã½ãã
- protected ãªãã£ã¼ã«ããã¡ã½ãã
- package private ãªã¡ã½ããã®ãã¡ããã¹ããããã¹ããã®
ãªã«ãæ¸ãã
The doc comment for a method should describe succinctly the contract between the method and its client.
ã¡ã½ããã®Javadocã«ã¯ãã¡ã½ããã¨å©ç¨è éã®å¥ç´ãç°¡æ½ã«æ¸ãã
å¥ç´ãã®ãã表ç¾ãæãã¤ãã¾ããã§ããããç§ã¯ãã§ãã¯ãªã¹ãçãªãã®ã«çãã¦ããå½¢ã§Javadocæ¸ãã¦ã¾ãã
- ãã®ã¡ã½ããã¯ä½ããã¾ããï¼ (what)
- ãã®ã¡ã½ããã¯(渡ããå¼æ°/ãªãã¸ã§ã¯ãã®ç¶æ
/ç°å¢ã®ç¶æ
ã«å¯¾ãã¦)ä½ãè¿ãã¾ããï¼ (invariant, postcondition)
- è¿ãããå¤ã«ã¯ã©ã®ãããªç¹å¾´ãããã¾ããï¼
- è¿ãããå¤ãã©ã使ãã°ããã§ããï¼
- ãã®ã¡ã½ããã«ã¯ãã©ããªå¼æ°ã渡ãã°ããã§ããï¼ (precondition)
- ããã«æ¸ãã¦ããªãå¤ã渡ãããå ´åãä½ãèµ·ããã¾ããï¼
- ãã®ã¡ã½ãããå¼ã³åºãåã«ãä½ãããã°ããã§ãããä½ããã¦ã¯ãããªãã§ããï¼ (precondition)
- ãã®æ¡ä»¶ãæºããã¦ããªãå ´åãä½ãèµ·ããã¾ããï¼
- ãã®ã¡ã½ãããå¼ã³åºãã¨ãã©ããªå½±é¿ãããã¾ããï¼ (side effect)
- (ããã¾ã§ã«æ¸ãããã¹ã¦ã®åè©ã«å¯¾ãã¦) ãã®åä½ã失æãããã¨ã¯ããã¾ããï¼
- 失æããå ´åãã©ã®ãããªå½±é¿ãããã¾ããï¼
大æµã®ã¡ã½ããã®æ£å¸¸ç³»ã¯ç©ºæ°ãèªãã°åãããããã®ã§ãããç°å¸¸ç³»ã¯è¨è¨è ã®Whyã¾ã§æ±²ã¿åããªãã¨åºæ¬çã«å¤ãã¾ããã
/** * ãã®ãªãã¸ã§ã¯ãã«æ ¼ç´ããããã¼ã¿ã®ãã¡ãæå®ã®ãã¬ãã£ãã¯ã¹ãæã¤ãã®ãè¿ãã * @param prefix ãã¬ãã£ãã¯ã¹ * @return æå®ã®ãã¬ãã£ãã¯ã¹ãæã¤ãã® */ String[] find(String prefix);
ã¨ãããã¦ããè¦ã¤ãããªãã£ãã¨ãã«nullãè¿ã£ã¦ããã®ãé·ã0ã®Stringé
åãªã®ããjava.util.NoSuchElementExceptionãã¹ãã¼ãããã®ããã¾ãã¡ä¸æã§ãã
è¦ã¤ãã£ãè¦ç´ ã®åæ°ã調ã¹ããã ããªã®ã«ããããããããªã³ã¼ãæ¸ããªãããªããªããªãã¾ãã
try { String[] results = find("com.example."); if (results == null) { return 0; } return results.length; } catch (NoSuchElementException e) { return 0; }
ãã®ç¨åº¦ã ã£ããã½ã¼ã¹ã³ã¼ãèªãããå®è¡ãã¦ã¿ã¦çµé¨çã«ãã¨ããæµãããããã¾ãããã©ã¡ãã«ããã¹ãã¼ãæã失ããã¾ãã
ããããã§ãã¯ãªã¹ãã¨ç §ããåããã¦æ¸ããªããããªæãã§ãããã ([ã]ã®é¨åã¯è¨è¼ããªãã§ãã)ã
/** * ãã®ãªãã¸ã§ã¯ãã«æ ¼ç´ããããã¼ã¿ã®ãã¡ãæå®ã®ãã¬ãã£ãã¯ã¹ãæã¤ãã®ãè¿ãã * [è¿ãããå¤ã®ç¹å¾´] * è¿ããã{@link String}ã®é åã«å«ã¾ããåè¦ç´ ã¯ããã¹ã¦å¼æ°ã«æå®ããã * æååããã®å é ã«æã¡ãè¾æ¸å¼é åºã«å¾ã£ã¦æ´åãããã * [åä½ã®å¤±æ] * å¼æ°ã«æå®ãããæååããã¬ãã£ãã¯ã¹ã«ãã¤ãã¼ã¿ããã®ãªãã¸ã§ã¯ãã«åå¨ããªãå ´åã * ãã®å¼ã³åºãã¯é·ã0ã®é åãè¿ãã * [å¼æ°ã®èª¬æã«ãªãå¤] * {@code prefix}ã«ç©ºã®æååã渡ãããå ´åã * è¿ãããé åã«ã¯ãªãã¸ã§ã¯ãã«æ ¼ç´ããããã¹ã¦ã®ãã¼ã¿ãå«ã¾ããã * @param prefix * ãã¬ãã£ãã¯ã¹ãã¾ãã¯ç©ºã®æåå * @return * ãã®ãªãã¸ã§ã¯ãã«æ ¼ç´ããããã¼ã¿ã®ãã¡ãæå®ã®ãã¬ãã£ãã¯ã¹ãæã¤ãã®ã®ä¸è¦§ã * å¼æ°ã«ç©ºã®æååãæå®ãããå ´åã¯ããã®ãªãã¸ã§ã¯ãã«æ ¼ç´ããããã¹ã¦ã®ãã¼ã¿ * @throws IllegalArgumentException * [å¼æ°ã®èª¬æã«ãªãå¤] * å¼æ°ã«{@code null}ãæå®ãããå ´å */ String[] find(String prefix);
æ®æ®µã¯ããã¡ãã£ã¨æµæ¢ã«ãªãããã«æ ¡æ£ãã¾ããã大ä½ãããªæãã§ããããã¾ã§æ¸ããã°å¤§æµã®åä½ãã¹ãã±ã¼ã¹ã¯æµ®ãã³ä¸ããã¾ãããå®è£ ãã»ã¨ãã©é ã®ä¸ã«åºæ¥ä¸ãã£ã¦ãç¶æ ã«ãªãã¾ãã
ãããgetter/setterã®Javadocã¯ç¡é§ãã¨ãããã¨ãèãã¾ããããã§ãã¯ãªã¹ãã«å½ã¦ã¯ãã¦ã¿ãã¨æå¤ã¨æ¸ããªãããªããªããã¨ããã£ãããã¾ãã
/** * ãã®ãªãã¸ã§ã¯ãã«ä»ä¸ãããå±æ§ã®ä¸è¦§ãè¿ãã * [ãªãã¸ã§ã¯ãã®ç¶æ ] * ãã®ãªãã¸ã§ã¯ãã«å±æ§ãã²ã¨ã¤ãè¨å®ããã¦ããªãå ´åã * ãã®å¼ã³åºãã¯è¦ç´ æ°0ã®{@link Set}ãè¿ãã * [ã©ã使ãã] * è¿ããã{@link Set}ãç´æ¥æä½ãããã¨ã§ã * ãã®ãªãã¸ã§ã¯ãã®å±æ§ãå¤æ´ãããã¨ãã§ããã * ãã ããã®æä½ã¯ã¹ã¬ããã¢ã³ã»ã¼ããªã®ã§ã * è¤æ°ã®ã¹ã¬ãããããã®ãªãã¸ã§ã¯ããå©ç¨ããå ´åãé©åã«åæããã¦ããªããã°ãªããªãã * @return ãã®ãªãã¸ã§ã¯ãã«ä»ä¸ãããå±æ§ã®ä¸è¦§ * @throws IllegalStateException * [precondition] * ãã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦ãã§ã«{@link #close()}ãå¼ã³åºãããå©ç¨ä¸å¯è½ã«ãªã£ã¦ããå ´å */ Set<String> getAttributes();
ã¨ãã£ãå ·åã§ãã
ã¨ã¯ãã£ã¦ãä¸è¨ã®ãã§ãã¯ãªã¹ããå ¨é¨ã¡ããã¨ããã®ã¯ããã©ããã¼ã¨èªåã§ãããæãã®ã§ãããç§ã¯ããã§éã«Javadocãæ¸ããããããã«ã¤ã³ã¿ã¼ãã§ã¼ã¹ãè¨è¨ããªããã¦ã¾ããJavadocã«æ¸ãããã¨ã¯åä½ãã¹ããããã¨ããèªåã«ã¼ã«ãããã®ã§ã100è¡ããããããªJavadocãæ¸ãããããã ãã§åä½ãã¹ãã50é ç®ãããå¢ãã¾ãã
Javadocåç主義
ä½ã§ç§ããããªã«Javadocã«ãã ãããã¨ããã°ã
- Javadocã«æ¸ããªããããªããã°ã©ã ã¯ã©ãããã¾ãåããªããã人ã«èª¬æã§ããªã
- å®è£ å§ããåã«ãJavadocã«åä½ãè¦å®ã§ããã試ãã¦ã¿ã
- å®ããã°ã©ã ã¯æ½è±¡åº¦ãä½ããã¦ç¶²ç¾
æ§ã®æ¤è¨¼ãé£ãã
- ã©ããèªç¶è¨èªã§èãããã ãããJavadocã§ãã£ãã»ãã楽
- å®è£
ãããªãç¶æ
ã§åä½ãã¹ãæ¸ãã¨ãå®è£
ã«é
æ
®ãããã¹ãã«ãªã
- ãæå³ããåä½ãã¨ãå®è£ ãã®ä¹é¢ãæ¤åºããããã«ãã¹ãæ¸ãã®ã«ããå®è£ ããããæå³ããåä½ããéç®ããã°ããããã
ã¨ããèããããããã§ãã
ä¸ã®äººã¯ãããªã«ãã ãããªãã¦ãããã®ããã
- 常ã«åä½ã説æã§ãã
- å®è£ ããªãããã¹ç¶²ç¾ ã§ãã
- æå³ããåä½ãæåããæå¾ã¾ã§ã¶ããªã
- å¥ã«è©³ç´°è¨è¨æ¸ä½ã£ã¦ãã
ã¡ãªã¿ã«ç¾å¨ãã¡ã½ããå¼ã³åºããã¨ãã«@throwsã«æ¸ãã¦ãªãã©ã³ã¿ã¤ã ä¾å¤ãé£ãã§ããã¨æ®å¿µãªæ°æã¡ã«ãªãç¨åº¦ã®ã¹ãã¼ã¸ã§ãã