Algorithm. We deal with algorithms from early childhood. At some point, your mom or older brother sends you to the store, for example, to buy kefir or bread.
What does the algorithm look like? — Leave the house, walk to the store, find the kefir/bread in the store, go to the checkout, pay the required amount, wait for change (if you paid more than required), walk back home, put the kefir in the fridge, put the bread in the bread bin.
What do we see in this algorithm? — A set of instructions, a condition (if the amount paid is greater than required, then wait for change), and a criterion for success.
The action plan can fail:
-
You could get stuck in the elevator.
-
You could lose the money on the way.
-
The store might be closed for technical reasons.
-
The kefir / bread might be out of stock: sold out, not yet delivered, expired.
What else can we note? — The algorithm contains high-level instructions.
“Leave the house” is not a trivial (simple) action. To leave the house, you need to execute another algorithm:
-
Find the apartment keys.
-
Open the door.
-
Step outside.
-
Close the door behind you.
-
Press the elevator call button.
-
If the elevator doesn’t come, walk down the stairs.
-
If the elevator arrives, press the button for the first floor.
-
Wait for the elevator to reach the first floor and the doors to open.
-
Exit the elevator.
-
Leave the building.
What could be some corner / edge cases?
For example, the person executing the algorithm might live on the first floor, so everything related to the elevator should be skipped.
Or the person might live in a five-story building without an elevator, so again, all elevator-related steps should be skipped.
The apartment might have a vestibule with its own door, meaning the algorithm would have two instructions for “open the door” and “close the door.”
The kefir might have different fat percentages, the bread might be from different brands, and it could be a brick loaf, a baguette, etc.
Key takeaways from the above:
-
Programming is the process of designing an action algorithm;
-
A complex algorithm relies on complex actions, each of which can be another algorithm itself;
-
An algorithm can be interrupted if any action fails, meaning it’s crucial to understand what can disrupt the successful execution of a given action;
-
An algorithm executes within a specific context (in our example, the presence of an elevator, the floor the executor lives on, etc.), and this context determines which actions must be performed and which must not; anything that falls outside a typical context is called corner/edge cases; the ability to program includes the ability to identify such edge cases and define the algorithm’s behavior for them;
-
The standard execution of the algorithm, where everything goes well—the elevator works, the store is open, the kefir / bread is available—is called the happy path.
Food for Thought
Think about what else is not accounted for in the original algorithm?
Answer
The issue of change might be irrelevant if payment is made by card. However, it might turn out that there are insufficient funds on the card, or the internet is down, preventing connection with the bank.
Vocabulary
-
Algorithm
-
Instruction / High-level instruction / Low-level instruction
-
Condition
-
Programming
-
Standard execution (Happy path)