Skip to main content

Module 2: CS Fundamentals: Case Studies

These cases connect early CS vocabulary to concrete behavior learners can inspect.


Case Study 1: State in a Shopping Cart

Scenario: A cart total changes after items are added, removed, discounted, and taxed. A learner stores only the displayed total, so the cart cannot explain how the number was produced.

Source anchor: MIT Mathematics for Computer Science.

Module concepts:

  • state
  • variables
  • derived values
  • invariants

Wrong Approach

Store only the final number and mutate it directly.

Better Approach

Store the source state: items, quantities, discounts, and tax rule. Compute totals from state and assert invariants such as nonnegative quantities.

Tradeoff Table

ChoiceGainCost
Store final totalSimple displayHard to audit
Store source stateExplainable behaviorMore data modeling
Event logFull historyMore complexity

Failure Mode

The cart total becomes impossible to debug after multiple updates.

Required Artifact

Draw a state table showing each operation, stored state, derived total, and invariant checked.

Project / Capstone Connection

Use state tables like this later when explaining domain workflows, API behavior, and debugging traces in project documentation.


Case Study 2: Function Inputs and Outputs in a Grade Calculator

Scenario: A grade calculator reads global variables and prints results directly. Tests are hard because the calculation is mixed with input and output.

Source anchor: Google Python Style Guide.

Module concepts:

  • functions
  • inputs and outputs
  • side effects
  • testability

Wrong Approach

Keep adding prompts and print statements inside the calculation.

Better Approach

Separate pure calculation from I/O. A function like calculate_grade(scores, weights) returns a value; another layer reads input and displays output.

Tradeoff Table

ChoiceGainCost
Mixed I/O and logicQuick scriptHard to test
Pure functionEasy testsRequires separation
Object modelRoom to growOverkill early

Failure Mode

A small grading rule change breaks interactive prompts and test setup together.

Required Artifact

Write a function contract with parameters, return value, invalid inputs, and five tests.

Project / Capstone Connection

Treat this contract format as the minimum standard for later project services, utility functions, and adapter boundaries.


Case Study 3: Data Types in a Temperature Converter

Scenario: A learner stores temperatures as strings and concatenates them by accident: "20" + "5" becomes "205" instead of 25.

Source anchor: Google Python Style Guide.

Module concepts:

  • data types
  • conversion
  • validation
  • representation

Wrong Approach

Convert values whenever an error appears, without deciding representation.

Better Approach

Choose internal numeric representation at the boundary, validate units, and format strings only when displaying results.

Tradeoff Table

ChoiceGainCost
Strings everywhereEasy input handlingArithmetic bugs
Numbers internallyCorrect operationsNeeds parsing
Unit wrapperSafer APIMore code

Failure Mode

The program passes visual checks but calculates with the wrong type.

Required Artifact

Create a data-flow diagram from input string to parsed value to converted result to display string.

Project / Capstone Connection

Reuse data-flow diagrams later when you need to explain serialization, validation, parsing, or pipeline boundaries in larger systems.


Case Study 4: Shared Mutation In A To-Do List

Scenario: A learner assigns one list to another variable, updates one of them, and is surprised that both appear to change.

Source anchor: The Python documentation is a practical anchor for explaining data model basics such as objects, references, and mutable containers in beginner programs.

Module concepts:

  • references
  • mutability
  • aliasing
  • state changes

Wrong Approach

Assume assigning a variable always creates an independent copy.

Better Approach

Model the value and the reference separately. When two names point at the same mutable object, a write through either name affects shared state.

Tradeoff Table

ChoiceGainCost
shared mutable listeasy updateshidden aliasing bugs
explicit copylocal safetymore memory and copy cost
immutable datapredictable behaviordifferent update style

Failure Mode

One function mutates shared state and another part of the program changes in ways the learner cannot explain.

Required Artifact

Draw a reference diagram for two variables and one list before and after an append operation.

Project / Capstone Connection

Use reference diagrams later when debugging shared caches, request objects, or mutable configuration state in larger projects.


Case Study 5: Counting With A Loop That Quietly Breaks Its Rule

Scenario: A learner writes a loop to count completed tasks but adds a branch that skips some items and double-counts others. The final result looks plausible, so the bug survives.

Source anchor: Python tutorial: more control flow tools is a useful concrete reference for loop and function behavior in beginner programs.

Module concepts:

  • iteration
  • invariants
  • conditional logic
  • trace-based debugging

Wrong Approach

Inspect only the final count and guess where the loop went wrong.

Better Approach

Define the rule the loop should preserve after each iteration, then trace a small example by hand to verify that every branch maintains it.

Tradeoff Table

ChoiceGainCost
output-only checkingfast to tryweak debugging signal
loop tracereveals broken steptakes manual effort
explicit invariantstronger reasoningnew concept for beginners

Failure Mode

The learner trusts a plausible final answer even though the loop logic violates its counting rule midway through execution.

Required Artifact

Create a loop trace table with input item, branch taken, running count, and the invariant being checked.

Project / Capstone Connection

Use the same trace-table habit later when debugging parsers, state machines, and data-processing loops in larger codebases.


Source Map

SourceUse it for
MIT Mathematics for Computer ScienceSupplying early language for state, invariants, and structured reasoning about program behavior.
Google Python Style GuideReinforcing clarity around function boundaries, type handling, and readable beginner examples.
Python tutorial: more control flow toolsConcrete reference for functions, loops, branching, and return behavior in small programs.
Python built-in types documentationAnchoring discussions of objects, references, and mutable containers to the actual language data model.