Doc Comment ConventionsWrite for your audiencejavadocSlide 4ContractsKnow where to put comments!javadoc comment styleHTML in doc commentsIdentifiers in doc commentsCode in doc commentsTags in doc comments ITags in doc comments IIKeep comments up to dateDocument nearly everythingthis objectParenthesesWrite summariesRules for writing summariesInclude examplesInput and output conditionsBugs and missing featuresWho cares?Aren’t these just arbitrary conventions?When do you add comments?Vocabulary IVocabulary IIThe End1Doc Comment Conventions2Write for your audienceRule 32: Write documentation for–those who must use your codeUsers should not need to care how your code worksthose who must maintain your codeMaintainers need to understand your codeThese two groups of people require different kinds of documentationjavadoc is the best way to document for usersuse internal comments to explain “how it works”3javadocjavadoc is a separate program that comes with every Java installationjavadoc reads your program, makes lists of all the classes, interfaces, methods, and variables, and creates an HTML page displaying its resultsThis means javadoc documentation is always accurateYour doc comments are integrated into javadoc’s HTML pageIt’s your job to ensure these are also accurateBlueJ includes a limited version of javadoc4javadocRule 35: Use documentation comments (javadoc) to describe the programming interfacejavadoc can be set to display:only public things (classes, methods, fields)public and protected thingspublic, protected, and package thingseverything, even private thingsBlueJ emphasizes simplicity--doesn’t give optionsAlways write doc comments for the user of your classes (a programmer)5Contracts“The primary purpose for documentation comments is to define a programming contract between a client and a supplier of a service. The documentation associated with a method should describe all aspects of behavior on which a caller of that method can rely and should not attempt to describe implementation details.”--The Elements of Java Style (Rule 35)6Know where to put comments! javadoc comments must be immediately before:a classan interfacea constructora methoda fieldAnywhere else, javadoc comments will be ignored!Plus, they look silly7javadoc comment styleRule 42: Use a single consistent format and organization for all documentation comments. /** * This is where the text starts. The star lines * up with the first star above; there is a space * after each star. The first sentence is the most * important: it becomes the “summary.” * * @tag these go at the end, after a blank line */void myMethod() { // this lines up with the / in /**8HTML in doc commentsDoc comments are written in HTMLIn a doc comment, you must replace: < with < > with > ...because < and > indicate tags in HTMLOther things you may use: <i>...</i> to make something italicExample: This case should <i>never</i> occur! <b>...</b> to make something boldface <p> to start a new paragraphOther types of comments are not in HTML9Identifiers in doc commentsRule 43: Wrap keywords, identifiers, and constants with <code> . . . </code> tagsExample: /** * Sets the <code>programIsRunning</code> flag * to <code>false<code>, thus causing * <code>run()</code> to end the Thread * doing the animation. */10Code in doc commentsRule 44: Wrap code with <pre>...</pre> tags.Preformatted text is shown in a monospaced font (all letters the same width, like Courier), and keeps your original formatting (indentation and newlines)Preformatted text is also good for ASCII “drawings” <pre> NW N NE \ | / W — + — E / | \ SW S SE</pre>11Tags in doc comments IRule 46: Establish and use a fixed ordering for javadoc tags.In class and interface descriptions, use: @author your name@version a version number or dateUse the @author tag in your assignments!!!In method descriptions, use: @param p A description of parameter p.@return A description of the value returned (unless it’s void).@exception e Describe any thrown exception.12Tags in doc comments IIRule 54: Fully describe the signature of each method.The signature is what distinguishes one method from anotherthe signature includes the number, order, and types of the parametersUse a @param tag to describe each parameter@param tags should be in the correct orderDon’t mention the parameter type; javadoc does thatUse a @return tag to describe the result (unless it’s void)13Keep comments up to date Rule 33: Keep comments and code in syncAn incorrect comment is often worse than no comment at allRule 38: Describe the programming interface before you write the code.It’s better to decide what to do, then do it than it is to do something, then try to figure out what you did14Document nearly everythingRule 39: Document public, protected, package, and private members.Personally, I don’t see much need to document private variables, provided they have good, meaningful namesRule 40: Provide a summary description and overview for each package.In other words: tell what your program does!15this objectRule 52: Use “this” rather than “the” when referring to instances of the current class.In Java, this is a keyword that refers to the instance of this class that is responding to the message (that is, the instance that is executing the method)Hence, this object has an especially clear meaning in commentsExample: Decides which direction this fox should move. (As a comment in the Fox class)16ParenthesesC and C++ programmers, pay attention!Rule 52: Do not add parentheses to a method or constructor name unless you want to specify a particular signature.If, in a comment, you refer to turn( ), you are implying that turn is a method with no parametersIf that’s what you meant, fineIf that’s not what you meant, say turn insteadWhy is this different from C and C++?In C, method overloading is not allowedC++ programming is strongly rooted in C17Write summariesRule 53: Provide a summary description for each class, interface, field, and method.The first sentence in each doc comment is special; it is used as the summary sentencejavadoc puts summaries near the top of each HTML page, with a link to the complete doc comment
View Full Document