AP Computer Science A · Unit 3 lecture · Part 2 of 10 · 6 min

3.2 Impact of program designCode that real people depend on

Mr. Thompkins
A bug in a phone game is annoying. A bug in a bike dock means somebody returns a bike, the dock says "full", and they get charged for the next hour. Programs touch real people, so let's talk about doing it responsibly.

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?
A logic error is a mistake that compiles and runs but gives the wrong result, like using < where you needed <=. The compiler can't catch it, because the code is legal Java. Only testing, or careful tracing, finds it.

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}
  1. line 16-19 An empty dock refuses a rental
  2. line 26-29 A full dock refuses a return. This is the check that matters in 3.2
The correct BikeDock. Each method refuses the one request it can't honor and reports false.

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
  1. line 4 No check against capacity
  2. line 9-10 A boundary test: return a bike to a dock that is already full
output › true 3
A buggy returnBike that forgot to check the capacity, and one boundary test that exposes it.
TestDock beforeCallExpectedBuggy code gives
Typical1 of 2 bikesreturnBike()true, 2 bikestrue, 2 bikes
Boundary: full2 of 2 bikesreturnBike()false, 2 bikestrue, 3 bikes
Boundary: empty0 of 2 bikesrent()false, 0 bikesfalse, 0 bikes
Three tests of the dock. The typical test passes even with the bug. Only the boundary test at "full" catches it.
Pause and think
The typical test passed with the buggy code. Why isn't one passing test enough to call the method correct?
I thought about it. Show me
Because a bug can hide in a case you didn't try. The typical case never reaches the line that should refuse a return, so it can't tell a correct method from a broken one. Bugs cluster at boundaries, so you test the edges on purpose.

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.

Choose the test cases
StudyRoom has 3 seats. enter() should add one person and return true only when a seat is free; otherwise it returns false and changes nothing. Pick a small set of tests that would catch the most likely bugs, and give the expected result of each.
  1. Typical: 0 people inside, call enter(). Expect true, and 1 person inside.
    Why?
    It confirms the basic behavior works at all.
  2. 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.
  3. 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.
  4. 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
  1. Where are the edges for a room with 3 seats?
  2. Test right at full, and one below full.
  3. For each test, write down both the return value and the number of people afterward.
Check yourself
A student finds a sorting method on a website and wants to use it in a class project. What should the student do?
  1. ACheck the code's license or terms, and use it only as allowed, with credit to the source
  2. BUse it freely, since anything posted publicly has no owner
  3. CRename the variables so the code counts as the student's own work
  4. 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.