RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab01b
lab01b

Discussion: Commenting code

Alas, just getting a function to run isn't enough. Why not? Well, consider coming across the following code (which does run):

  double f( double r ) {
    return 3.14 * r * r * r / 3;
    }
Is this a good function? Is the formula it computes correct? We have no idea! If trying to compute the volume of a right-cone with radius and height of r, then it's correct1; if trying to compute the surface area of the cone then it's wrong, and if it's calculating something else entirely (a quarter of a sphere?), it may or may not be correct.
The Zeroth Law of Programming
Code is meant to be read by humans.
At the least, this law requires that each function must have a clear description of what it computes. Even if a computer might be able to do something with undocumented code, it's useless to people (including the person who wrote it, after a week, when they don't remember exactly what each function does.)

(For example: One very easy mistake to make with functions we've written, is remembering that pizzaArea should be passed the pizza's diameter, not the radius. There is no way for the computer to help us remember this.)

We'll come back later to what comprises an adequate description. Note that just the fact of choosing a good “self-documenting” name goes a long way; avoid generic names like f or calculate. But, just because a name seems self-documenting to you doesn't necessarily mean that further comments aren't needed. For example,

     double rightConeSurfaceArea( double r )
This name is fairly descriptive about what the function returns. But other programmers who use this function might still have questions: Does this function include the cone's base, in the surface area? Does it include the inside and outside of the cone (which is important if painting an actual cone inside and out).

Comments, and Javadoc

Okay, back to our pizzaArea example, Attempt 3, where we add a description. Descriptions precede the function, and are enclosed by /* and */.

  /** Calculate the area of a pizza, given its diameter.
   * @param diam The diameter (in inches); must be non-negative.
   * @return the surface area of the pizza top (in square inches).
   *         Accurate to within 1%.
   *   pizzaArea(16) ~= 200.96
   *   pizzaArea(12) ~= 113.04
   *   pizzaArea(20) ~= 314.0
   *   pizzaArea( 0) ~=   0.0
   *   pizzaArea( 2) ~=   3.14
   */
  double pizzaArea( double diam ) {
    return 3.14 * (diam/2) * (diam/2);
    // Single-line comments can also start with '//',
    // and they last until the end of that line.
    }


  /* You can have comments which begin with '/*',
   * instead of '/**'.  These are just regular multi-line comments, 
   * and are not used to generate java documentation.
   */

  /* By the way,
     you don't need that row of stars down the
     left side; that's just to improve readability.
   */
This is the first acceptable version (though there are a couple of improvements yet in store). The comments are in a special format, called “javadoc”. They explain what the parameter diam is meant to stand for and what the returned answer means. They help people understand your program, without having to read the bare code. Javadoc: Full options, and tips from the experts.

There is a bonus to writing this information using those tags @param and @return: In BlueJ, look at the code window, and in the upper-right corner change Implementation to instead ask for the Interface view. Voila! Other programmers can look at this web page for pizzaServer, and know how to call our functions, without ever actually having to look at our code!

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


Writing a method

For today's lab, you must work with a partner. We will check off your work, before leaving.

In yesterday's lecture, we saw that one function (like friPizzaArea) might call another function (like pizzaArea).

Solutions


1 the area of a 2-d triangle is 1/2 base times height; the area of a 3-d cone is 1/3 base times height.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Aug.27 (Mon)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme