3.2 Impact of program designCode that real people depend on

A system is reliable when it does what it's supposed to under every condition it will meet. No program is perfectly reliable, so programmers work to make it as reliable as they can, mostly by testing it with a variety of conditions. Good test cases come in three kinds: typical inputs, boundary inputs at the edges (empty, full, zero, the first and last item), and unusual inputs nobody expects but somebody will try.
?Wait, what's a logic error again?
Here is the whole BikeDock class, written carefully. You'll learn every part of it in the next sections. For now, look at the two if statements: each one guards a boundary, an empty dock and a full dock.
1public class BikeDock2{3 private String station;4 private int capacity;5 private int bikes;67 public BikeDock(String station, int capacity)8 {9 this.station = station;10 this.capacity = capacity;11 bikes = capacity;12 }1314 public boolean rent()15 {16 if (bikes == 0)117 {118 return false;119 }120 bikes--;21 return true;22 }2324 public boolean returnBike()25 {26 if (bikes == capacity)227 {228 return false;229 }230 bikes++;31 return true;32 }3334 public int getBikes()35 {36 return bikes;37 }38}
- line 16-19 An empty dock refuses a rental
- line 26-29 A full dock refuses a return. This is the check that matters in 3.2
Now suppose the author forgot the second guard. Watch how easy it is to miss.
1// a buggy version of returnBike2public boolean returnBike()3{4 bikes++;15 return true;6}78// the test: a full dock with capacity 29BikeDock d = new BikeDock("Elm St", 2);210System.out.println(d.returnBike() + " " + d.getBikes());2
- line 4 No check against capacity
- line 9-10 A boundary test: return a bike to a dock that is already full
| Test | Dock before | Call | Expected | Buggy code gives |
|---|---|---|---|---|
| Typical | 1 of 2 bikes | returnBike() | true, 2 bikes | true, 2 bikes |
| Boundary: full | 2 of 2 bikes | returnBike() | false, 2 bikes | true, 3 bikes |
| Boundary: empty | 0 of 2 bikes | rent() | false, 0 bikes | false, 0 bikes |
I thought about it. Show me
Reliability is one kind of impact. There are legal and ethical ones too. Code is intellectual property: the person or company who wrote it owns it. You may use someone else's code only with permission, and a license is the document that grants it and lists the conditions. Many open source licenses let anyone use and change the code if they give credit, and some require you to share your changes under the same license. Pasting code from a classmate or a website without permission and credit is plagiarism, in school and at work.
Programs also shape society, the economy and culture, sometimes in ways nobody intended. Picture a bike-share app that only accepts one kind of payment card. Nobody meant to leave anyone out, but riders without that card can't use the bikes. Design choices are choices about people, so ask who your program serves, who it might leave out, and what happens to them when it fails.
- Typical: 0 people inside, call enter(). Expect true, and 1 person inside.
Why?
It confirms the basic behavior works at all. - Just below the edge: 2 people inside, call enter(). Expect true, and 3 inside.
Why?
This catches a check written as people < seats - 1, which wrongly turns away the last person. - At the edge: 3 people inside, call enter(). Expect false, and still 3 inside.
Why?
This catches a missing check, or one written as <= that lets a fourth person in. - Check the state, not just the return value: after a false, the count must not change.
Why?
A method can return the right boolean and still change the data, like the buggy dock that returned true and went to 3 bikes.
Answer: Test 0 people (true, becomes 1), 2 people (true, becomes 3) and 3 people (false, stays 3), and check the count after every call.
Stuck? Get a hint
- Where are the edges for a room with 3 seats?
- Test right at full, and one below full.
- For each test, write down both the return value and the number of people afterward.
- ACheck the code's license or terms, and use it only as allowed, with credit to the source
- BUse it freely, since anything posted publicly has no owner
- CRename the variables so the code counts as the student's own work
- DUse it only if it compiles without changes
Show the answer
A. Code is intellectual property: use it only with permission and give credit. The second choice is the top trap: being public on the web is not the same as being free to reuse. Renaming variables doesn't make someone else's work yours.
- Reliable means it works under all conditions it will meet. Raise reliability by testing typical, boundary and unusual cases.
- Check both the return value and the object's state after each test.
- Code is intellectual property. Use others' code only with permission (a license), and give credit.
- Programs affect society, the economy and culture, sometimes in ways no one intended. Ask who could be left out.