AAB - So... How is? (Lesson)
So... How is?
So... How is an Abstraction Used?
Let’s see how the abstraction works.
I will start with the very generic code of making a peanut butter and jelly sandwich.
- Assemble the ingredients (the data) on my work surface.
-
- Slices of bread (2)
- Peanut butter jar
- Jelly jar
B. Assemble the tools needed on my work surface
-
- Knife
- Plate
Now I will write the pseudocode to build the sandwich.
-
- Place the two slices of bread separately on the plate with both top ends of the bread facing the same direction.
- Open the jelly jar (multiple steps here omitted, but you know how to do that already).
- Pick up the knife with your hand by the handle.
- Put the knife inside the peanut butter jar twisting to bring out peanut butter on the knife.
- Spread the peanut butter on the bread with the knife by moving the peanut butter side of the knife on the bread.
- Need more peanut butter?
- Yes, repeat steps 3 – 6 until the answer to 6 is no (one or both breads can have peanut butter, your choice).
- Put the knife down on the plate.
- Close the peanut butter jar (multiple steps here omitted, but you know how to do that already).
- Open the jelly jar (multiple steps here omitted, but you know how to do that already).
- Pick up the knife with your hand by the handle.
- Put the knife inside the jelly jar twisting to bring out jelly on the knife.
- Put the spoon in the jelly jar and turn it to lift some jelly from the jar.
- Turn the spoon over and drop the jelly on the bread.
- Spread the jelly on top of the peanut butter on one side of the bread by moving the jelly side of the knife on the bread.
- Need more jelly?
- Yes, repeat steps 12 – 16 until the answer to 16 is no.
- Put the spoon down on the plate.
- Close the jelly jar (multiple steps here omitted, but you know how to do that already).
- Pick up the side of the bread with only peanut butter or no peanut butter.
- Place the bread (peanut butter side down if it has peanut butter on it) on top of the slice with the peanut butter and jelly, matching the tops.
- Sandwich is created. Enjoy.
Reflection and Analysis
How many times did I indicate to open a jar? Twice. I also mentioned that there were missing steps as this algorithm would be much longer had I added in the steps of the open jar abstraction. I would have had to write those lines down twice for a total of 8 lines (6 added accounting for my open jar lines, 8 – 2 = 6). By abstracting (setting the obvious open jar code aside I saved some lines of code and created only one set of 4 lines that I need to adjust if something goes wrong). Both would get corrected with the one correction.
How many times did I indicate to close a jar? Twice. I also mentioned that there were missing steps as this algorithm would be much longer had I added in the steps of the open jar abstraction. I would have had to write those lines down twice for a total of 10 lines (8 added accounting for my open jar lines, 10 – 2 = 8). By abstracting (setting the obvious open jar code aside I saved some lines of code and created only one set of 4 lines that I need to adjust if something goes wrong). Both would get corrected with the one correction.
By creating an abstraction, I saved steps. I reduced the complexity of my steps because if I made a mistake in the abstraction, I only needed to fix it once, not twice.
Note that the abstraction is set aside, hidden from the main code, but we understand that when I say open the jelly jar or open the peanut butter jar, the steps can be located and followed, then returning back to where I left off in my algorithm.
Is the abstractions an algorithm? Yes. The open jar abstraction has the steps to follow to solve the problem of opening a jar. The close jar abstraction has the steps to follow to solve the problem of closing a jar.
The Pseudocode?
I know you are thinking that knowing when and how to use abstractions or repeats are not initially obvious. You are correct, they are not. Everyone starts at the beginning, with a list of everything that you think has to happen to solve a problem. So let’s create a list of the steps to write pseudocode.
-
- Write out in short steps all you believe has to happen to solve the problem.
- Review your steps and add any forgotten steps.
- Review your steps and mark all steps that are similar, different color highlighters work great.
- Review similar highlighted steps. Can you make them more similar by taking something out and putting it in another step outside of the similar steps? If so, rewrite in that for creating a name for each of the similar groups of steps as I did for the open jar or close jar.
- Review for areas that might be repeated. Can we adjust this to be a repeat for more flexibility?
IMAGE CREATED BY GAVS AND USED ACCORDING TO TERMS OF USE.