Session 4 Top-down design, Breaking down a problem, Modeling OOP

Goals

By the end of this session, you will be able to:
  • understand assessment expectations
  • define top-down design and object oriented programming
  • break down real-world tasks using top-down design strategies
  • model real-world objects and apps by breaking down their attributes and behaviors

Agenda

Materials

  • Notebook
  • Writing instrument
  • Laptop
  • Headphones & mic

Housekeeping and Goals

Wins

  • Nice work figuring out pull requests!
  • Creative, well-done git diagrams

Follow-Ups

  • Some low survey scores on how to hook up a local repository (on your computer) with a remote one (on github). Definitely go over the task 1 from the hw again if it’s still fuzzy
  • Don’t “Initial Commit” on an already-existing project (like the mod0resources repository); remember to start with a present-tense verb with a capital letter (like Add, Change, Remove, Fix, Make, etc.)
  • Edit files using your text editor, not echo

Other Notes

  • Good commit message format:
Add list of mod 0 resources
Remove reference to old blog post
Change data type of age field
Fix spelling mistake
  • Not good commit message format:
Rachel is adding her list of mod 0 resources
old blog post
changed data type for age field
Oops, I need to fix my spelling mistake
  • Today’s Goals

Intros, Review, and Icebreaker

Intros, Review, and Icebreaker

Person with the shortest first name goes first.

1. Introduce yourself: name, pronouns

2. What's something you're really good at? (Don't be humble!)

3. Accountability review: what tangible progress have you made toward the focus skills you identified at the beginning of Mod 0?

Have extra time? Share what extra things you're doing to get ready to start school at Turing.

Top Down Design

“Programing is hard because it requires us to solve ill-defined problems with unknown solutions. Our job is to invent the solutions. Coding is inherently creative.” - Danny Smith on Breaking Down Problems

Top Down Design (or step-wise design) is an approach to breaking down a problem or system. In this approach, the designer lays out the problem or system’s high-level overview, then breaking down the overview into sub-systems (or sub-steps), then repeating that process until the system has been broken down into the smallest pieces.

This is not top-down design:

draw an owl meme

Credit: https://eurokeks.com/

This is the start of a top-down approach:

draw an owl top down design

Why is this important? A problem that is not broken down into its smallest components remains too complex and abstract to code. In addition, small components allow for reusable and replaceable units of code.

As humans, we memorize and practice the steps that it takes in order to do even the most basic tasks. If you tell a human to tie their shoe, you (generally) don’t need to specify any further instructions. However, if you were to build a shoe-tying machine, you would need to break down the process into the most basic steps.

Try It Together: Making Pizza

Follow along with your paper and pencil as we walk through breaking down the process of making pizza.

Now, in groups!

Try It (Break Out Rooms): Top Down Design (~12 minutes)

The person whose first name starts closest to the letter F will pick a scenario below:

  • Reheating a meal
  • Mailing a package
  • Walking a dog
  • Putting children to bed
  • Applying for a job
  • Writing an essay
  • Starting a campfire
  • [Choose your own adventure]

As a group, use a Top-Down Design approach to break down the scenario. Everyone should have their own diagram.

Done? Review, revise, and choose another scenario.

Be ready to share and explain.

Top Down Design in Programming

encrypt and decrypt top down design

Credit: Liam McQuay (IGCSE Computer Science Youtube Tutorial)

Breaking down problems using top down design lends itself nicely to the object-oriented design principles of abstraction (where an object performs a task without other objects being concerned about how it is done) and encapsulation (where an object handles its own internal states and behind-the-scenes work).

Notating Top-Down Design for the Mod 0 Assessment

On your Mod 0 Assessment, you will be asked to break down a scenario using Top Down Design. We’ll demonstrate the pizza scenario in a Github Gist.

Try It: Notating Top Down Design (~5 minutes)

Open a new gist and use decimal notation to write out your top down design appoach with the scenario you chose from your group.

Post your gist link in the chat, and be ready to give feedback to the person you're paired up with (will be posted in the chat while you're working).


BREAK

Turn off your mics and videos and walk away from the computer. Stand up, stretch, drink water. Do a few sit-ups, squats, push-ups, jumping jacks, arm circles, stress ball squeezes, or whatever else moves your body.


Object Oriented Programming

Object oriented programming, or OOP for short, is an approach to programming (or a programmming paradigm) where programs are organized as a series of objects.

OOP is very similar to how the world actually works. Objects are created from templates that we call classes.

A class defines attributes (or properties) and methods (or actions). An object is a very specific instance of a class. For example, if the class were Car, two objects might be 2007 Blue Nissan Versa and 2014 Silver Nissan Juke.

Attributes contain data about a specific object. The information format should be one of the basic data types from Session 2 (string, integer, float, boolean, array, hash).

The names of attributes are generally nouns.

Two good questions to ask when you’re determining what should be classified as an attribute are:

  • “Is this piece of data something that could potentially stay the same over the course of an object’s lifetime?” (you want the answer to be yes)
  • “Is there any other data that underlies this piece of data?” (you want the answer to be no)

CAUTION: Sometimes, methods will feel like they should be attributes. For example: age, years_employed, percent_full.

Try It Together: Bottle Class Attributes

Follow along as we walk through defining a bottle class with three different bottle objects.

NOTE: For consistency in this lesson, we're going to stick to the naming convention of snake_case. This will look very Ruby-esque. However, in JavaScript land, you'll see camelCase.

Methods define behavior of an object, actions that can be performed on that object, or calculations that generally use . Methods are generally verbs (action words or short action phrases).

Methods generally answer the question “What can this thing do?” or “What can be done to this thing?”

Try It Together: Bottle Class Methods

Follow along as we walk through defining a bottle class with three different bottle objects.

Key Points

  • A method performs some kind of work and will almost always use or modify an attribute
  • Anything that does work (calculations) should be a method, not an attribute
  • Attributes are generally nouns (99.9% of time)
  • Methods are generally verbs (90% of time – can also be questions OR nouns that are the result of calculations)
  • One quick side note: accessor methods are outside the scope of today’s lesson.

Can You Spot the Problem?

What would be wrong with…

  • a class called Turing
  • an attribute called current_time
  • having attributes for a Review class called one_star, two_stars, three_stars, etc.
  • a Senator class having an array attribute called senator_names
  • a class called California
  • having attributes on a ShoppingCart class called item_one, item_two, item_three, etc.
  • a method on GroceryStore called clean_aisle_seven
  • a Bottle class having an attribute called water
  • a Chair class having an attribute called number_of_chairs
  • a MenuItem class with a method called CustomerSurvey

Try It (Big Breakout Rooms) (~15 minutes)

Make a copy of this document which your group will use to practice.

Person whose first name starts closest to Q will share their screen and choose one of the following classes:

  • Vehicle
  • Book
  • Playlist
  • GroceryStore

As a team, brainstorm at least five attributes (and data types) and five methods (and descriptions) for your chosen class. Each person should be keeping their own copy up to date to use as a reference.

Person whose first name starts closest to the letter A will suggest an object that is an instance of the class. This is Object #1.

Brainstorm the values for each attribute of that object.

Brainstorm the results of each method called on that object.

Person whose first name starts closest to the letter E will suggest a second object that is an instance of the class. This is Object #2.

Repeat the brainstorm process for attributes and methods for object #2.

Notating Classes, Objects, Attributes, and Methods for the Mod 0 Assessment

On your Mod 0 Assessment, you will be asked to identify classes, objects, attributes, and methods. We’ll demonstrate the format we want you to use.

Try It: Notating Classes, Objects, Attributes, and Methods

Create two gists. In the first gist, use the notation we demonstrated to write out your class. In the second gist, use the notation we demonstrated to write out your first object.

Post the links to both of your gists in the chat.

Example Files