Pseudocode Demonstration
Learning Goals
- Observe an experienced developer write pseudocode
Introduction
If we're trying to code with the Flatiron Process in mind, we should train our use of the process on small problems first and work our way up. To make sure that we're not bringing in other new technical ideas, we're going to write pseudocode to create even chunks of delicious baguette for several people at our party.
Baguettes are good to think about because they're basically rulers that you can eat. Here's a picture.
A gluten-bomb of deliciousness
Demonstrate Pseudocode Guided by the Flatiron Process
Note: For this exercise we'll ignore that the baguette is rounded at the ends and that there may be tiny differences between sections. Most people you'd want to share a baguette would ignore these as well
Step 1: Identify the problem
The problem is that there is a number of people with whom I'd like to share some tasty baguette. But I also want to make sure that everyone gets an equal amount.
Step 2: Identify the output that would solve the problem
If I were to have a collection of pieces that were all of the same size, everyone could share equally in the delicious baguette goodness.
So the change to the baguette in a way that solves the problem would be:
"Divide a baguette evenly among a number of people and return the pieces"
Talk Like a Programmer: Most programmers and math types would say "divide among n people."
Step 3: Name the Procedure That Fixes the Problem
So let's call this procedure "divide baguette evenly."
The process name tells us what we should expect it to return: a collection of evenly-sized bread pieces.
Decor Tip: Maybe put the even bread pieces a nice straw basket with a decorative gingham handkerchief lining it?
Step 4: Identify What Inputs are Needed to Create the Output
To create the even pieces of baguette we certainly need....a baguette.
There's also a hint in this adjective we keep using even. What is even? For our purposes, even means that the pieces all have the same length. Given that our procedure includes the words "among n people," we need to know the number of people
So our inputs are:
- Baguette
- Number of people
Step 5: Define the Procedure’s Implementation
In our discussion of _even_above, we found that knowing the even length would be helpful to us. The even length is calculated by dividing the baguette_length by the number of people (or, n).
At this point we know the even_length.
Tip: This is the purpose of local variables in a procedure (function, method, all the same thing). They're there to make the implementation clearer by holding information that gets reused in the procedure.
"Wait a second!" you might be thinking. We didn't define the measure
procedure. That's true. This is pseudocode, not actual code. When you can
easily imagine that something to get a value exists, there's no reason to not
use it as if it exists when it's reasonably clear.
So while the baguette's length is longer than an even piece, the baguette can be cut. The part that's cut off should go into a basket and the part that remains should be tested for whether another piece can be cut off of it. At the end, we'll be done and have our pieces.
"Wait a second!" you might be thinking. You're using weird methods like add()
or x, y = someProcedure(argument)
! Those don't exist. That's not Ruby /
Python / JavaScript!?"
Again, we're writing pseudocode and there's no standard for what it looks like. The goal here is not to write the code, but to think about what the code needs to do. If one language has a very clear way of expressing that idea, sometimes you might use it.
Here's the pseudocode in text format:
Procedure DivideBaguetteEvenly(baguette, n):
baguette_length = measure(baguette)
even_length = baguette_length / n
collection = []
while baguette_length >= even_length:
piece, rest = cut_bread(baguette, even_length)
collection.add(piece)
baguette = rest
baguette_length = measure(baguette)
even_pieces = collection
return even_pieces
Next Steps
We're going to cover "Step 6: Verify the Procedure's Output" and "Step 7: Code It" until a later lesson. While we think the pseudocode is readable and correct, what do you think? We're going to ask you about it in the next lesson.
Step through the pseudocode in your mind or on paper. Do you get the output you expect?
Conclusion
Pseudocoding is a valuable way to make sure you're designing your methods in the Flatiron Process. Building small, focused, tested procedures your can rely upon ensures that when all those procedures work together, the program works.
While pseudocode might, sometimes, feel like extra work, it has a powerful ability to break programmers out of default assumptions and help them write focused, clear code.