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
| Choice | Gain | Cost |
|---|---|---|
| Store final total | Simple display | Hard to audit |
| Store source state | Explainable behavior | More data modeling |
| Event log | Full history | More 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
| Choice | Gain | Cost |
|---|---|---|
| Mixed I/O and logic | Quick script | Hard to test |
| Pure function | Easy tests | Requires separation |
| Object model | Room to grow | Overkill 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
| Choice | Gain | Cost |
|---|---|---|
| Strings everywhere | Easy input handling | Arithmetic bugs |
| Numbers internally | Correct operations | Needs parsing |
| Unit wrapper | Safer API | More 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.