Pseudocoding

Pseudocoding

Explaining what your code does to a 3-year-old

Pseudocoding Course

  • What's the point of pseudocoding?
  • What are the guidelines of pseudocoding?
  • When do we use pseudocoding?
  • Examples of pseudocode

What's the point?

  • Demystify complex problems
  • Remove the need for you to hold everything in your head
  • Create clarity and more definition to reduce ambiguity
  • Give yourself a discrete, single solvable thing to work on - focus
  • Explain your approach to someone else

What are the guidelines?

  • Rule #1: No actual code allowed.
  • Rule #2: Explain what you're doing in English using very simple terms.
  • Rule #3: Each line must be something that can be turned into code.
  • Rule #4: You don't need to know how that line will be turned into code yet. 
  • Rule #5: Indent pseudocode as you would real code to show control flow (loops, conditions).

Examples

Our task: add 5 to each item in an array. 

 

Pseudocode:

//iterate through the array

    //add 5 to each item

 

Actual code:

//iterate through the array
for (var i = 0; i < arr.length; i++) {
  //add 5 to each item
  arr[i] += 5;
}

Examples

Create a function, checkStore, that searches through our bookStore array for a user-inputted book

 

Pseudocode:

//declare a function that takes a bookname string as a parameter

    //iterate through the bookStore array

       //find the title of each book

       //check if the title matches the bookname

           //if so, pop up alert box with title, author, and price

           //ask the user if they want to add the item to their cart

               //if the user selects yes, add to their cart

    //if the book isn't found, alert the user that we don't have it

Example

You'll notice that each line of pseudocode gives us clear instructions for a task we can do. 

Do we follow the guidelines? 

  • Rule #1: No actual code allowed.
  • Rule #2: Explain what you're doing in English using very simple terms.
  • Rule #3: Each line must be something that can be turned into code.
  • Rule #4: You don't need to know how that line will be turned into code yet. 
  • Rule #5: Indent pseudocode as you would real code to show control flow (loops, conditions).

When do we use it?

  • At the start of any problem we don't have an obvious solution to
  • Whenever we get stuck
  • In pair programming to explain our approach
  • When approaching any complex problem
  • In technical interviews, no matter what

When do we use it?

 

All the time. 

 

It's super valuable. 

Be The Machine

  • Practice "executing" your pseudocode with pretend inputs.
  • Just as we like stepping through our actual code line by line to explain what it's doing, we can do the same with our pseudocode. 

Be the Machine

create a function that multiplies every even number in an input array by 10 and returns the modified array

 

//declare a function that takes an array as a parameter

    //iterate through that array

        //check to see if each item is even

             //if so, overwrite the array at that position with that item multiplied by 10

    //return the array

 

Let's "invoke" this with the array [1,2,3,4]

This helps you spot bugs before you even write code!

Be the Machine

create a function that multiplies every even number in an input array by 10 and returns the modified array

 

//declare a function that takes an array as a parameter

    //iterate through that array

        //check to see if each item is even

             //if so, overwrite the array at that position with that item multiplied by 10

    //return the array

 

Let's "invoke" this with the array [1,2,3,4]

This helps you spot bugs before you even write code!

Today's Exercises

These exercises are a review from last week and are not necessarily tied to today's lecture material (though you should always use pseudocoding to solve problems!)

https://github.com/TelegraphPrep/week2

Pseudocode

By Preston Parry

Pseudocode

An intro to pseudocoding for Telegraph Prep

  • 1,837