← Back to course
Python 11-12 / Week 12 / Thursday
4/6
Week 12 · Reading Java II and Ship

Thursday

Recursion in Java, and a code review
// ArrayList, sorting, searching and recursion in Java; then a code review and the FieldSim release
⏱ about 30 min

Thursday: Recursion in Java, and a Code Review

Comet stretches. "FieldSim shipped. Can we call it finished?"

Wren shakes his head and clips a fresh sheet to his clipboard. "What does the evidence say? A code review checks the qualities of a program, not just whether it runs."

Nova projects one more Java file first, the recursive countdown. "How can I help?" she asks. "Then I will show a checklist. You decide what it says about FieldSim."

Comet reads the Java countdown and grins. "That is our countdown, with braces."

Wren hands you the checklist. "Lead programmer, you are the reviewer today."

Recursion in Java

// CountdownDemo.java
// The recursive countdown from recursion.py, written in Java.
public class CountdownDemo {
    public static void countdown(int n) {
        if (n == 0) {
            System.out.println("Go!");
        } else {
            System.out.println(n);
            countdown(n - 1);
        }
    }

    public static void main(String[] args) {
        countdown(3);
    }
}
FIND THE PARTS
  • Read the question.
  • Tap your answer.
Which Java line is the base case test?
Where does CountdownDemo start running?
3
2
1
Go!
# countdown_twin.py
# The Python twin of CountdownDemo.java, using your recursion.py.
from recursion import countdown

countdown(3)
At your computer
1. Type countdown_twin.py in your fieldsim folder and run it. Compare with the Java output.
2. Change countdown(3) to countdown("3"), with quote marks. Run it and read the error calmly.
3. Take the quote marks away and run it again.
4. Then type route_count.py, below, and run it. You will use it in the review.
# countdown_twin.py
# The Python twin of CountdownDemo.java, using your recursion.py.
from recursion import countdown

countdown("3")
3
Traceback (most recent call last):
  File "countdown_twin.py", line 5, in <module>
    countdown("3")
    ~~~~~~~~~^^^^^
  File "recursion.py", line 10, in countdown
    countdown(n - 1)
              ~~^~~
TypeError: unsupported operand type(s) for -: 'str' and 'int'
READ THE ERROR
  • Read the question.
  • Tap your answer.
Why did countdown("3") print 3 and then stop?

The review checklist

A code review checks qualities such as correctness, usability, readability, efficiency, portability and scalability.

Wren adds one fact to the review. This short program counts the letters in Pip's route:

# route_count.py
route = "FFRFFFLFFLFRRFFLF"
print(len(route), "commands")
print(route.count("F"), "steps forward")
17 commands
11 steps forward
QualityQuestion for fieldsim.pyEvidence to look at
CorrectnessDoes it give the right result?The output and test_fieldsim.py
ReadabilityCan another programmer follow it?Names, docstrings and comments
EfficiencyHow much work does it do?collect_here runs once per command
UsabilityIs the output clear to a reader?The grid, the status line and the list
PortabilityWill it run on another computer?It uses only Python and its own files
ScalabilityWould it handle a bigger field or more rovers?One Rover named Pip is built in main
REVIEW FIELDSIM
  • Read the question.
  • Tap your answer.
collect_here runs once per command letter. How many times does it run in main?
Which is evidence about correctness?
Which is evidence about readability?
Which change would help scalability most?
PUT A CODE REVIEW IN ORDER
  • ?Run the program and the tests
  • ?Read the code with the checklist
  • ?Write one finding for each quality
  • ?Choose the most useful change to make next
WHY THIS EXERCISEA review starts from what the program actually does, then records findings and picks a next step.

That was a careful review, lead programmer. Tomorrow you will write down the design so the next programmer can understand it.

← Wednesday