Organizing Code with Objects: An Introduction to Object-Oriented Programming
All programs are built from four fundamental patterns: sequential code (executing instructions in order), conditional code (making decisions with if statements), repetitive code (using loops to repeat blocks), and store and reuse (packaging code into functions).
Four Building Blocks
Every program is built from a small set of fundamental patterns. These patterns solve four recurring problems: carrying out instructions in order, choosing between alternatives, repeating actions, and organizing code so it can be used again. The four patterns are sequential code, conditional code, repetitive code, and store and reuse.
Object-oriented programming builds on these patterns by organizing related code and data into objects and classes. The four patterns are the foundation; object-oriented programming is a way to organize and scale them.
Tracing a Small Program
Consider a program that processes a collection of values. It may first carry out setup instructions in order. It may then check a condition to decide what to do with a value. A loop can repeat that work for several values, and a function can package the work so another part of the program can call it when needed. This is not four unrelated techniques. It is one program using four patterns together.
The Four Patterns
| Pattern | Problem it solves | What it does |
|---|---|---|
| Sequential code | How do instructions happen in order? | Executes instructions one after another |
| Conditional code | How does the program choose between possibilities? | Uses a condition and follows a path based on whether it is true or false |
| Repetitive code | How can the program perform an action multiple times? | Uses a loop to repeat a block of instructions |
| Store and reuse | How can code be packaged for use in multiple places? | Packages code into a function that can be called when needed |
Sequential code is the basic flow of instructions in order. Conditional code introduces a decision: an if statement allows the program to follow different paths depending on whether a condition is true or false. Repetitive code uses loops to execute a block more than once. Store and reuse packages code into functions, allowing that code to be called whenever it is needed instead of being duplicated.
Following a Decision
A conditional pattern changes the path through a program. The program evaluates a condition. When the condition is true, it follows the instructions associated with that branch. When the condition is false, it follows the other available path or skips the conditional action. The important feature is not the particular condition; it is the choice between paths.
What do you think happens?
A program checks a condition inside a loop. If the condition is false for the current value, does the program still use the true branch for that value?
Reveal answer
Answer: No, the false path is followed.
Conditional code makes a decision based on whether its condition is true or false. A loop may continue separately, but the conditional branch for the current value follows the condition's result.
Repeating with Loops
Repetitive code addresses the need to perform the same block of instructions multiple times without rewriting that block each time. A loop repeats its block for the values or situations covered by the loop. A condition inside the loop can decide how each repetition is handled.
A loop is the repetition pattern. If the repeated block also contains an if statement, the program is using repetitive and conditional patterns together.
Packaging Reusable Work
Store and reuse organizes instructions into a function. Instead of duplicating the same instructions in several places, a programmer packages them into a reusable unit and calls that function whenever the work is needed. A function can contain sequential instructions, conditions, loops, or combinations of these patterns.
Patterns and Data Structures
The four patterns describe how a program flows and organizes its code. Data structures describe how a program stores and organizes data. Simple variables store single values. Lists, tuples, and dictionaries store multiple values together and allow access by position or key. These structures are often used inside the four patterns.
A loop can process the values in a list. Conditional code can check whether a dictionary contains a particular key. These examples show the difference between the program's flow pattern and the structure holding its data.
Recognizing Patterns in Code
- Look for instructions that simply happen one after another. Identify this as sequential code.
- Look for an if statement or another condition that selects between paths. Identify this as conditional code.
- Look for a loop or a repeated block of instructions. Identify this as repetitive code.
- Look for a named function that packages instructions for later calls. Identify this as store and reuse.
- Check whether one pattern appears inside another. Real programs commonly combine and nest the patterns.
Treating the four patterns as isolated techniques
The patterns combine and nest inside real programs.
Fix:
Identify every pattern present, including patterns inside a function or loop.Calling every repeated-looking instruction a function
Repetition describes repetitive code, while store and reuse describes packaging code into a function.
Fix:
Look for both features separately: a loop indicates repetition, and a function indicates packaged reusable code.Confusing data storage with program flow
A list stores and organizes data; a loop is the pattern that can process the list repeatedly.
Fix:
Separate the structure holding data from the pattern controlling the program.
Practice the Identification
Identify the main programming pattern in each scenario. Then name any additional pattern that could be nested inside it: a program executes setup instructions in order; a program chooses one action when a condition is true and another when it is false; a program processes every item in a collection; a programmer packages a group of instructions so it can be called from several places.
Hints
- Ask whether the scenario is about order, choice, repetition, or reuse.
- A collection can be used with a loop, but the collection itself is a data structure rather than one of the four flow patterns.
- A function may contain any combination of the other patterns.
Classifying a Combined Scenario
A function examines each item in a list. For every item, it checks a condition and performs one of two actions. What patterns are present?
Function: The instructions are packaged so they can be called when needed. This is store and reuse.
Each item: The function processes multiple items by repeating a block of work. This is repetitive code.
Condition: The function chooses between two actions based on whether a condition is true or false. This is conditional code.
Actions: The instructions within each branch occur in order. This is sequential code.
All four patterns are present. They are nested: sequential instructions and a condition occur inside a repeated block, and the repeated block is packaged inside a function.
Foundation for Objects
Understanding these patterns prepares you for object-oriented programming. In object-oriented programming, classes organize related code and data together, and methods inside classes use combinations of the four patterns. Instances of classes can be processed with loops, and conditional code can help decide which method to call. The patterns remain the underlying building blocks even when the program is organized around objects.
Key Takeaways
- Sequential code executes instructions in order.
- Conditional code chooses between paths based on whether a condition is true or false.
- Repetitive code uses loops to repeat blocks of instructions.
- Store and reuse packages code into functions that can be called when needed.
- Complete programs combine and nest these patterns, while object-oriented programming organizes related code and data around objects and classes.