A Good Algorithm Should Survive Handoff
Suppose someone writes:
“Take the numbers, do the usual calculation, and repeat until it looks right.”
That may make sense to the person who wrote it, but it is not a reliable algorithmic description.
A clearer version might say:
- Input a nonempty list of real numbers.
- Set
total = 0. - Add each list value to
total. - Divide
totalby the number of values. - Return the quotient.
Now the task can be executed, traced, tested, and implemented.
\[ \boxed{ \text{clear algorithm} = \text{precise intent} + \text{explicit structure} + \text{readable representation} } \]Know the Audience and Purpose
An algorithm may be written for:
- a student learning a method,
- a programmer implementing it,
- a mathematician checking correctness,
- a technician following a procedure,
- a future version of yourself maintaining the work.
The mathematical content should remain precise, but the amount of explanation can change with the audience.
A classroom explanation may include intuition and examples. A technical specification may emphasize input types, constraints, outputs, and guarantees.
State the Problem First
Before listing steps, tell the reader what the algorithm is supposed to accomplish.
Weak
“Use the following steps.”
Better
“Given two real numbers \(a\) and \(b\), return the larger value.”
That statement identifies the task before the implementation begins.
A good problem statement makes it possible to ask whether the algorithm actually solves the intended problem.
Name Inputs, Outputs, and Preconditions
A reader should not have to infer what information is required.
Example
Input: real numbers \(a\) and \(b\).
Output: the larger of \(a\) and \(b\).
Precondition: both inputs are valid real numbers.
For division, a precondition may be:
\[ b\neq0. \]For a mean:
\[ n\geq1. \]Explicit preconditions prevent ambiguity about which inputs the procedure is designed to handle.
Use Meaningful Names
Names are part of algorithmic communication.
Compare:
x = 0
FOR y in z
x = x + y
END FOR
with:
total = 0
FOR value in measurements
total = total + value
END FOR
The second version reveals the role of each variable.
Short names such as \(i\), \(j\), and \(n\) are still useful when their roles are conventional or local, especially for counters and mathematical indices. The goal is not long names; it is meaningful names.
Write Atomic, Ordered Steps
An atomic step performs one clear action.
Too Compressed
“Process the values and return the average.”
Clearer
- Set
total = 0. - Set
count = 0. - For each value, add it to
total. - Increase
countby \(1\). - Return
total / count.
Breaking a process into explicit actions makes errors easier to locate and logic easier to trace.
Make Control Structure Visible
Readers should be able to see:
- which steps happen in sequence,
- which steps belong to a branch,
- which steps repeat,
- where a procedure returns or stops.
Indentation is one of the simplest ways to show this.
IF score >= 60
status = "pass"
IF score >= 90
distinction = true
END IF
ELSE
status = "fail"
END IF
The indentation makes the nesting visible before the reader examines every word.
From Idea to Clear Algorithm
Use Consistent Pseudocode
Pseudocode works best when its conventions remain consistent.
For example:
MAXIMUM(a, b)
IF a >= b
RETURN a
ELSE
RETURN b
END IF
END
The exact words are less important than consistency.
Avoid switching unpredictably among:
RETURNandPRINTwhen they mean different things,=as assignment and equality without context,- different loop styles for the same kind of repetition.
Use Comments for Intent, Not Noise
Comments are useful when they explain why a step exists.
Weak Comment
total = total + value
# add value to total
The comment merely repeats the line.
Better Comment
# Maintain the running sum of all processed measurements.
total = total + value
The second comment explains the role of the update.
Useful comments may document:
- assumptions,
- non-obvious formulas,
- why a branch exists,
- why a particular method was chosen,
- important invariants or safety checks.
Choose the Right Representation
Different representations emphasize different features.
| Representation | Best for Showing |
|---|---|
| Natural language | Motivation and accessible explanation |
| Pseudocode | Ordered algorithmic structure |
| Flowchart | Branching and control flow |
| Trace table | Changing state across steps |
| Decision table | Combinations of conditions |
| Formula | Compact mathematical relationships |
Good communication sometimes uses more than one representation because no single form shows everything equally well.
Include Examples and Trace Cases
A short example can reveal how an algorithm is intended to behave.
Example
For:
ABSOLUTE_VALUE(x)
IF x >= 0
RETURN x
ELSE
RETURN -x
END IF
END
include examples such as:
\[ \operatorname{ABSOLUTE\_VALUE}(5)=5, \] \[ \operatorname{ABSOLUTE\_VALUE}(-5)=5, \] \[ \operatorname{ABSOLUTE\_VALUE}(0)=0. \]The zero example is especially useful because it shows how the boundary case is handled.
Document Conditions and Edge Cases
A clear algorithm should say what happens when:
- input is empty,
- a denominator is zero,
- a value lies exactly on a boundary,
- no solution exists,
- an approximation fails to converge,
- an input is outside the intended domain.
Example
Instead of merely writing:
RETURN total / count
state the precondition
\[ count>0 \]or include a guard:
IF count = 0
RETURN "no data"
END IF
State Invariants and Guarantees
For more technical algorithms, it helps to communicate properties that remain true during execution.
Example: Running Sum
After processing the first \(k\) values, maintain:
\[ \boxed{ total=x_1+x_2+\cdots+x_k } \]
This invariant explains what the variable total means at every
stage.
A final guarantee might state:
“When the algorithm terminates, total equals the sum of
all input values.”
Such statements make correctness easier to reason about and maintain.
Communicate Efficiency When It Matters
For small procedures, performance notes may be unnecessary. For larger algorithms, a reader may need to know how the amount of work grows.
For example:
“The algorithm scans the list once.”
This tells the reader that the amount of work grows roughly in proportion to the list length \(n\).
A more technical description may later use notation such as
\[ O(n). \]At this stage, the key communication habit is simply to mention important resource behavior when it affects method choice.
Remove Ambiguity
Ambiguous words often hide missing rules.
Examples:
- “large,”
- “close,”
- “until done,”
- “repeat several times,”
- “use the best value.”
Replace them with measurable conditions.
Instead of
“Repeat until the values are close.”
Write
\[ |x_{n+1}-x_n|<10^{-6}. \]Now “close” has a precise operational meaning.
Review and Revise for Clarity
A useful final review asks:
- Can another reader identify the problem immediately?
- Are the inputs and outputs explicit?
- Are all variables defined before use?
- Are branches and loops visually clear?
- Are boundary cases documented?
- Does each comment add information?
- Can the procedure be tested from the description alone?
- Does the representation match the complexity of the algorithm?
Clarity is not decoration added after the mathematics. It is part of making the mathematics reproducible.
Worked Examples
Example 1: Rewrite a Vague Procedure
Vague: “Keep dividing until the number is small.”
Clear:
INPUT positive real x
WHILE x >= 10
x = x / 10
END WHILE
RETURN x
Now the input domain, action, stopping condition, and output are explicit.
Example 2: Improve Variable Names
Before:
s = 0
FOR x in a
s = s + x
END FOR
After:
sum = 0
FOR value in measurements
sum = sum + value
END FOR
The second version communicates the roles without changing the algorithm.
Example 3: Document a Boundary
Problem: classify temperature as freezing or above freezing using Celsius.
IF temperature <= 0
RETURN "freezing"
ELSE
RETURN "above freezing"
END IF
The use of
\[ \leq0 \]makes the boundary case explicit.
Example 4: State a Precondition
For:
\[ q=\frac{a}{b}, \]document:
\[ \boxed{b\neq0}. \]This communicates when the algorithm is valid.
Example 5: Add an Example to Clarify Intent
Suppose an algorithm rounds a positive number to the nearest integer. Include:
\[ 3.2\mapsto3, \qquad 3.8\mapsto4. \]If ties matter, also state the convention for
\[ 3.5. \]The example exposes a rule that might otherwise remain unstated.
Example 6: Choose a Better Representation
For a simple formula such as
\[ A=\pi r^2, \]a formula may communicate the process more efficiently than a flowchart.
For a multi-branch eligibility rule, a decision table or flowchart may be clearer than a single dense formula.
Common Communication Errors
1. Assuming the Reader Knows the Goal
Always state what the algorithm is intended to accomplish.
2. Using Undefined Variables
A symbol should not appear before its meaning is established.
3. Hiding Several Actions inside One Vague Step
Break complicated instructions into smaller operations.
4. Inconsistent Indentation or Notation
Visual inconsistency can make branches and loops difficult to interpret.
5. Overcommenting Obvious Lines
Comments should explain intent, assumptions, or non-obvious structure.
6. Omitting Boundary and Invalid Cases
Readers need to know what happens at the edges of the domain.
7. Choosing a Representation That Hides the Important Structure
Use a table for state, a flowchart for control flow, and a formula for compact relationships when those forms fit the task.
8. Writing for the Author Instead of the Reader
An algorithm should remain understandable even after the original context is forgotten.
Practice
Improve the Condition
Rewrite “repeat until the error is small” using a tolerance of \(0.001\).
Check Answer
\[ \boxed{|error|<0.001} \]or an equivalent explicit stopping condition.
Choose a Name
Which variable name better communicates a running sum?
A. x
B. total
Check Answer
\[ \boxed{\texttt{total}} \]unless the surrounding mathematical context already gives \(x\) a clear conventional meaning.
Choose a Representation
Which representation is especially useful for showing changing variable values during execution?
Check Answer
\[ \boxed{\text{trace table}} \]Precondition
What precondition should accompany a procedure returning
\[ \frac{a}{b}? \]Check Answer
\[ \boxed{b\neq0}. \]Clarity Check
Why is “repeat several times” weaker than “repeat exactly five times”?
Check Answer
“Several” does not determine a unique execution count, while “exactly five times” is precise and testable.
The Deeper Idea
An algorithm is both a process and a representation of that process.
Communication determines whether another person can recover the intended structure:
\[ \boxed{ \text{problem} \to \text{specification} \to \text{representation} \to \text{execution} } \]This connects the entire section:
- problem statements define the task,
- inputs and outputs define the interface,
- branching and iteration define control flow,
- pseudocode records the procedure,
- tables reveal state,
- testing checks whether the description and behavior agree.
Clear algorithmic communication is therefore not merely writing style. It is part of correctness.
If a procedure is so ambiguous that two careful readers can execute it differently, then the algorithm has not yet been communicated precisely enough.
Summary
- Algorithm communication should be matched to the audience and purpose.
- State the problem before listing the procedure.
- Identify inputs, outputs, constraints, and preconditions explicitly.
- Use variable names that reveal their roles.
- Break complicated work into clear, ordered steps.
- Use indentation to make branches, loops, and nested structure visible.
- Keep pseudocode conventions consistent.
- Use comments to explain intent, assumptions, and non-obvious reasoning.
- Choose representations according to what needs to be seen: formulas, pseudocode, tables, decision tables, or flowcharts.
- Examples and trace cases clarify intended behavior.
- Document boundary, exceptional, and invalid-input cases.
- Invariants and guarantees explain what should remain true and what the algorithm promises at completion.
- Performance characteristics should be communicated when they affect method choice.
- Replace vague language with measurable conditions.
- Review an algorithm from the reader's perspective before considering the description finished.
A well-communicated algorithm can be handed to someone else and still retain its meaning. Its inputs are clear, its steps are visible, its conditions are precise, and its outputs can be checked. That clarity turns private reasoning into reusable mathematics.
Algorithms and Computational Thinking
- Previous: Testing, Debugging, and Error Detection
- Algorithms and Computational Thinking — return to the lesson directory.