DOC PREVIEW
Penn CIT 591 - How to Write Good Comments

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

How to Write Good CommentsWrite for your audienceInternal commentsGood code requires few commentsGood uses of internal commentsjavadocSlide 7Contractsjavadoc is a contractKnow where to put comments!javadoc comment styleHTML in doc commentsIdentifiers in doc commentsCode in doc commentsTags in doc commentsKeep comments up to dateDocument nearly everythingthis objectParenthesesThe first sentence is specialRules 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 EndJan 14, 2019How to Write Good Comments2Write for your audienceProgram documentation is for programmers, not end usersThere are two groups of programmers, and they need different kinds of documentationSome programmers need to use your codeDo not explain to them how your code works--they don’t care and don’t want to knowTell them what your methods do, how to call them, and what they returnJavadoc is the best way to document your code for usersOther programmers need to maintain and enhance your codeThey need to know how your code worksUse internal comments for these programmersWhen you work on your program, you are in both groupsDocument as though, the next time you see your program, you will have forgotten everything about it!3Internal commentsUse internal comments to:Explain the use of temporary variablesLabel closing braces in deeply nested statements, or when many lines are between the open and close braceswhile (i != j) { ... ... ... ... ... ... } // end whileExplain complex or confusing codeExplain what the next section of code doesNever repeat the code!count = count + 1; // add one to count4Good code requires few commentsUse internal comments to:Explain the use of temporary variablesBetter: Give them self-explanatory namesLabel closing braces in deeply nested statements, or when many lines are between the open and close bracesBetter: Don’t nest statements that deeplyBetter: Keep your methods shortExplain complex or confusing codeBetter: Rewrite the codeIf it’s complex or confusing, it’s probably buggy as wellExplain what the next section of the method doesBetter: Make it a method with a self-explanatory name5Good uses of internal commentsUse internal comments:If you really can’t make the code simple and obviousTo reference a published algorithmTo mark places in the code that need workEclipse provides three tags for this purpose (you can add more):TODO – Means: This code still needs to be writtenFIXME -- Means: This code has bugsXXX -- Means: I need to think about this some moreTo see these, choose Window --> Show View --> TasksTo indicate an intentional flow-through in a switch statementTo temporarily comment out code (Eclipse: control-/)6javadocjavadoc is a separate program that comes with every Java installationjavadoc reads your program, makes lists of all the classes, interfaces, methods, and variables, and creates HTML pages displaying its resultsThis means javadoc’s generated documentation is always accurateYou can write special documentation (“doc”) commentsYour doc comments are integrated into javadoc’s HTML pageIt’s your job to ensure these are also accurateJavadoc’s output is very professional lookingThis makes you look goodIt also helps keep your manager from imposing bizarre documentation standards7javadocAlways use doc comments to describe the API, the Application Programming InterfaceDescribe all the classes, interfaces, fields, constructors, and methods that are available for usejavadoc can be set to display:only public elementspublic and protected elementspublic, protected, and package elementseverything--that is, public, protected, package, and private elementsRemember, doc comments for the programmer who uses your classesAnything you want to make available outside the class should be documentedIt is a good idea to describe, for your own use, private elements as well8Contracts“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 by Allan Vermeulen and 6 others9javadoc is a contractIn the “real world,” almost all programming is done in teamsYour Javadoc is a contract between you and the other members of your teamIt specifies what you expect from them (parameters and preconditions)It specifies what you promise to give them in returnDo not be overly generous!Provide what is really needed, but...Remember that anything you provide, you are stuck with debugging, maintaining, and updatingProviding too much can really hamper your ability to replace it with something better someday10Know where to put comments! javadoc comments must be immediately before:a classan interfacea constructora methoda fieldAnywhere else, javadoc comments will be ignored!Plus, they look silly11javadoc comment styleUse this format for all doc comments: /** * This is where the text starts. The asterisk lines * up with the first asterisk above; there is a space * after each asterisk. The first sentence is the most * important: it becomes the “summary.” * * @param x Describe the first parameter (don’t say its type). * @param y Describe the first parameter (don’t say its type). * @return Tell what value is being returned (don’t say its type). */public String myMethod(int x, int y) { // p lines up with the / in /**12HTML in doc commentsDoc comments are written in HTMLIn a doc comment, you must replace: < with &lt; > with &gt; & with &amp; ...because these characters are special in HTMLOther things you may use: <i>...</i> to make something italicExample: This case should <i>never</i> occur! <b>...</b> to make something boldface <p> to start a new paragraphOther types of comments are not in HTML13Identifiers in doc commentsWrap keywords and the names of variables and methods with <code> . . . </code> tagsExample: /** * Sets the <code>programIsRunning</code> flag * to <code>false<code>, thus


View Full Document

Penn CIT 591 - How to Write Good Comments

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download How to Write Good Comments
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view How to Write Good Comments and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view How to Write Good Comments 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?