A plan in words is good. A plan you can see is better. A flowchart draws the steps as boxes, the decisions as diamonds, and the path as arrows. You can follow it with your finger and see where it goes wrong before any code exists.
Difficulty: Beginner · Know this first: F04 — Thinking in Steps: Programming Fundamentals & Logic Building
The bar below is a worked example for this lesson, not a measurement. It shows shares of 100 for the effort of drawing a flowchart. The shape is the point: the step boxes are most of the drawing. Switch the scenario and watch the diamond and the loop-back arrow take their share.
Extra diagram · from scratch to master
Start with the start oval. Add one shape at a time until the whole vocabulary is on the map.
Extra diagram · from scratch to master
Follow a morning decision through the chart. Change the weather and the path changes.
Read a flowchart without drawing one. Pick a shape and see what it means, then walk the umbrella chart with the weather you choose. The paths are exact. The lesson is the shapes: four of them, and every chart is built from those four.
Try this: (1) Pick the diamond. It is a question with two labelled arrows. (2) Set the weather to raining. The path is Start, look outside, raining? yes, take umbrella, leave. That is 5 stops. (3) Set it to dry. The path is Start, look outside, raining? no, go without, leave. Also 5 stops, a different middle.
Three questions. No score is saved anywhere — this is just you against the material.
I take the same drawing four times here. First I learn the four shapes. Then I draw a chart with a decision. Then I draw one with a loop. Last I use charts to review other people’s plans.
Definition: A flowchart uses four shapes. An oval means start or stop. A rectangle means do this. A diamond means a question with two answers. A slanted box means input or output. Arrows show the order. That is the whole vocabulary.
Types:
Uses: I use this to draw any plan before I write code. Four shapes are enough for every plan in this course.
Dimensions: One chart, a few boxes, one path. What breaks first is a box with two actions in it, and I notice it when my finger does not know what to do in that box. It costs minutes to draw a small chart. It costs hours when I skip the drawing and code from my head. The trade is simple: a drawing you can follow beats a plan you have to remember.
Definition: At this level I draw decisions that split the path and rejoin it. Two arrows leave the diamond, labelled yes and no. Both paths lead somewhere, and both reach the stop oval. I check the chart by walking each path with my finger.
Types:
Uses: I use this when a task has choices. I draw both sides of every choice, because the side I do not draw is the side that breaks.
Dimensions: A chart with a few decisions. What breaks is an unlabelled arrow, and I notice it when two people walk the same chart and go different ways. It costs care to label every arrow. The trade is that labels cost seconds, and they buy a chart anyone can follow.
Definition: At this level I draw loops: an arrow back to an earlier box, and a diamond that decides whether to go around again. I check that something changes each time around, so the loop can end. I check the empty case, where the loop runs zero times.
Types:
Uses: I use this for any task that repeats. I draw the loop, I draw the exit, and I walk it with a pile of one item and a pile of zero items.
Dimensions: A chart with loops and decisions together. What breaks is a loop that never changes anything, and I notice it when my finger goes around forever. It costs discipline to check the empty pile. The trade is that the empty pile costs one walk-through, and it buys a chart that works on real data.
Draw the failure path, not just the happy path. A flowchart with only yes arrows is a wish, not a plan. Every decision diamond has a no arrow, and that arrow needs a box of its own. I draw what happens when the password is wrong, when the file is missing, and when the network is down. The happy path is the easy part.
$ python3 -c "
def login(password):
if len(password) < 8:
return 'reject: too short'
if password.isdigit():
return 'reject: numbers only'
return 'accept'
for pw in ['abc', '12345678', 'correct-horse']:
print(pw, '->', login(pw))"
abc -> reject: too short
12345678 -> reject: numbers only
correct-horse -> accept
Every flag in the commands above, letter by letter.
python3 -c: -c means run the string that follows as code. The quotes hold the program. Without -c, python3 expects a filename.python3 -m runs a module by name. python3 -m json.tool reads JSON and prints it indented.Check it: run python3 -c "print(1+1)". It prints 2.
Try it: draw the login flowchart on paper with three diamonds, one for each check. Then run the code above. Each printed line is one path through your chart. If a path on paper has no printed line, your test missed it.
A loop in a flowchart needs a counter. A retry loop without a limit retries forever. I draw the counter on the chart, next to the loop arrow, so the limit is visible. Three tries, then stop. The number on the chart is a promise, and the code has to keep it.
$ python3 -c "
tries = 0
while tries < 3:
tries += 1
print('try', tries, 'failed, retrying')
print('stopped after', tries, 'tries')"
try 1 failed, retrying
try 2 failed, retrying
try 3 failed, retrying
stopped after 3 tries
Try it: run the code above, then change 3 to 30 and run it again. Thirty lines print. The chart would have said 30, and the code would have done 30. The limit on the chart and the limit in the code must be the same number.
Definition: At this level I use flowcharts to review plans other people wrote. I ask for the chart before I read the code. I walk it with my finger. I ask what happens on the no path, what happens when the pile is empty, and where every loop ends. The chart is the review.
Types:
Uses: I use this when a plan is too big to hold in my head. I ask for the drawing, and the drawing shows me the holes.
Dimensions: Many charts, many people, one system. What breaks is the chart nobody drew, and I notice it when a change breaks a path nobody knew existed. It costs time to draw and review charts. The trade is that drawing costs hours, and an undrawn path costs incidents.
The flowchart that lies. A chart can be neat and still wrong, because it skips a step the code takes. The usual missing step is the timeout. The chart shows try, fail, retry. The code waits ten seconds between tries, and the chart says nothing about it. I add the wait to the chart, because the wait is where the time goes.
$ timeout 5 python3 -c "
import time
tries = 0
while tries < 3:
tries += 1
print('try', tries)
time.sleep(1)
print('done')"
try 1
try 2
try 3
done
Every flag in the commands above, letter by letter.
timeout 5: the 5 is seconds. After 5 seconds, timeout sends TERM to the command. The loop in this box sleeps 1 second per try, so 3 tries finish well inside 5 seconds. The timeout is a safety net, not the thing doing the work.timeout -s KILL sends KILL straight away instead of TERM. The program gets no chance to clean up. The default signal is TERM, which lets it finish what it is doing first.Check it: run timeout 2 sleep 10; echo $?. It prints 124 after 2 seconds. Then run timeout -s KILL 2 sleep 10; echo $?. It prints 137. Same wait, different signal, different code.
Try it: run the code above. It takes three seconds, not zero. The sleeps are invisible in the output but real in the clock. If your chart has no wait step, your time estimate is wrong by the sum of all the waits.
Every diamond is a test case. A flowchart with four diamonds needs at least four tests, one per branch. I read the diamonds off the chart and turn each one into a test. When a diamond has no test, I write one before I call the work done.
$ python3 -c "
def login(password):
if len(password) < 8:
return 'reject: too short'
if password.isdigit():
return 'reject: numbers only'
return 'accept'
print(login('abc'))
print(login('12345678'))
print(login('correct-horse'))"
reject: too short
reject: numbers only
accept
Try it: count the diamonds in the login chart. There are two. Count the test lines above. There are three, one per path. A chart with two diamonds and three paths needs three tests, and the code above runs all three.
The shapes are a language, and like any language, the point is that other people can read it. A chart only you can read is a note, not a flowchart. Label every arrow. Write one action per box. Your future self is one of the readers.
A flowchart is a test you can run with your finger. That is its power. Code needs a machine to test it. A chart needs a finger and a real example. Run the chart before you run the code.
The most common flowchart mistake is a diamond with one arrow out. A question with two answers needs two arrows. If you only drew one, you have not decided what happens on the other side. That is a bug, drawn on paper, waiting for a user to find it.
At 10x the size, a flowchart gets too big for one page. You split it: one chart per function, and a big chart that shows how the functions connect. The shapes stay the same. The paper multiplies.
At 100x, the chart is the design of the whole system. Boxes become services, arrows become network calls, diamonds become load balancers and retries. It is this lesson, drawn bigger.
START
READ the number
IF the number is bigger than biggest:
biggest = the number
IF the pile is empty:
STOP
OTHERWISE go back to READ
SHOW biggest
STOP
That is the find-the-biggest plan as a chart in words. Every line is a box. The IF lines are diamonds. The go back line is the loop-back arrow.
Try it: the retry counter. Run the retry loop with a limit of 3. It prints three tries and stops. Change the limit to 1 and run it again. It prints one try and stops. The counter is the only thing between a retry loop and an infinite loop, and it is one line of code.
$ python3 -c "
tries = 0
while tries < 3:
tries += 1
print('try', tries, 'failed, retrying')
print('stopped after', tries, 'tries')"
try 1 failed, retrying
try 2 failed, retrying
try 3 failed, retrying
stopped after 3 tries
A chart for approving a request had a diamond with one arrow out. The question was: is the request approved? The yes arrow went to send it. The no arrow was never drawn. When the first request was rejected, nobody knew what to do with it. It sat in a pile for a week.
Two things. First, every diamond gets two labelled arrows before the chart is called done. Second, I walk every chart with the no answer, not just the yes answer. The happy path is easy to draw. The other path is where the work hides.
Draw, in words, the flowchart for logging in: type your name, type your password, check if they match, and either let the person in or show an error. Say which shape each box is, and label every arrow out of every diamond.
Start with the boxes. About eight: start, read the name, read the password, check them, let the person in or show an error, stop. Two diamonds at most: do they match, and have there been three tries. The chart fits on one page. If it does not, it has too many jobs in it.
A chart that shows every retry is honest and busy. A chart that hides the retries is clean and hides the logic. The trade is that detail costs boxes, and it buys a chart that tells the truth about what the program does.