APIs, Libraries, and Documentation
Programs are built mostly from classes someone else wrote, and those classes come with documentation. Take an invented library for running a garden's watering system.
Libraries and packages
A library holds classes that other programmers can use, grouped into packages of related classes. One package of the garden library holds three classes.
Package garden
Sprinkler waters one zone of the garden for a set time
SoilProbe reports how damp the soil is in one zone
Timer turns the water on at set times of day
A programmer who needs to water the lawn uses
Sprinkler without reading the code inside it. What they read is the library's API specification, the published description of what each class stores and what it can do. Each class also defines a reference type. Just as String is a type a variable can have, so is Sprinkler, and a program can create Sprinkler objects from the existing class.Attributes and behaviors
The documentation for
Sprinkler lists its members.Name Description
zone the zone of the garden this sprinkler waters
runMinutes the number of minutes in one watering
turnOn starts the water
turnOff stops the water
setRunMinutes changes the number of minutes in one watering
The first two members are data a sprinkler stores, so they are attributes, each kept in a variable. The last three are things a sprinkler can do, so they are behaviors, each defined by a method. The descriptions say which is which. A noun phrase names data, and a description that starts with a verb names an action.
Comments
A comment is text in a program that the compiler ignores. It's written for people reading the code, and anything inside it is never run, even a statement.
int runMinutes = 16;
// System.out.println("Old time: " + runMinutes);
runMinutes = runMinutes * 2;
System.out.println(runMinutes);
The two slashes make the rest of their line a comment, so the first print statement never runs. The segment prints one line, 32.
A comment that starts with
/* lasts until */, across as many lines as it takes.int zones = 8;
/* System.out.println("Zones before:");
System.out.println(zones); */
System.out.println(zones - 2);
Both print statements between the markers are ignored, so the segment prints one line, 6.
A third kind starts with
/**. It's still ignored by the compiler, but a documentation tool reads it and builds the API specification from it. The Sprinkler class has one above its setRunMinutes method./**
* Changes the number of minutes in one watering.
* Precondition: m is between 1 and 60
* Postcondition: runMinutes is equal to m
*/
Preconditions and postconditions
The comment above
setRunMinutes states two conditions. The precondition, that m is between 1 and 60, has to hold right before the method runs if it's to work as described. The postcondition, that runMinutes equals m, is true after it runs. It describes the result through an attribute's new value.When a method returns a value, its postcondition can be stated in terms of that value. A
SoilProbe method might promise "the value returned is between 0 and 100."The precondition is the caller's responsibility. The method isn't expected to check it, and a comment can't make the compiler check it either. So before calling
setRunMinutes(75), it's up to the programmer to notice that 75 isn't between 1 and 60.