Concepts / Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming

Procedure-oriented programming organizes code around reusable functions; object-oriented programming organizes code around objects that combine data and functionality.

  • Programming

Two Ways to Organize a Program

Imagine a program that manages student information. It must store a student's name, ID, and grades, calculate an average, display the student's information, and update a grade. There are two broad ways to organize this work. A procedure-oriented design starts with the actions the program performs. An object-oriented design starts with the things that exist in the program and the behavior connected to those things.

What do you think happens?

A design stores a student's name, ID, and grades separately, while several reusable functions receive that information as input. Which programming approach does this describe?

  • Procedure-oriented programming
  • Object-oriented programming
Reveal answer

Answer: Procedure-oriented programming

Procedure-oriented programming organizes code around reusable procedures or functions. The data is separate from the functions and flows between them.

Following Data Through Procedures

In procedure-oriented programming, you think of the program as a collection of reusable procedures or functions. A function takes data as input, transforms it, and returns a result. The data itself remains separate from the functions. The focus is on what actions the program performs and how data moves from one function to another.

data passed todata passed todata passed tocontains data and behaviorStudent dataname, ID, gradescalculate_averagefunctionStudent objectname, ID, gradesStudent methodsaverage, display, updateprint_student_infofunctionupdate_gradefunction
How does the same student-information problem organize its data and functionality differently in procedure-oriented and object-oriented programming?

Student information as procedures

Organize operations for a student's name, ID, and grades using a procedure-oriented design.

Store the data: Keep the student's name, ID, and grades as data separate from the functions.

Calculate: A function such as calculate_average receives the grades and produces an average.

Display: A function such as print_student_info receives the student's name, ID, and grades and displays the information.

Update: A function such as update_grade receives the grades and a new grade, then processes the grade data.

The program is organized around reusable actions. The student's data flows between separate functions.

Bundling Data with Behavior

In object-oriented programming, you think of the program as a collection of objects. Each object bundles together the data it cares about and the functions that operate on that data. The data is called attributes, and the functions inside the object are called methods. This makes the object a cohesive unit: the student's information and the operations that use that information belong together.

has attributehas attributehas attributehas methodhas methodhas methodStudent objectone studentnameattributecalculate averagemethodIDattributedisplay informationmethodgradesattributeupdate grademethod
What data does a student object contain, and which methods operate on that data as one cohesive unit?

Student information as an object

Organize the same student's information using an object-oriented design.

Create the object: Represent the student as an object that contains the student's name, ID, and grades as attributes.

Attach behavior: Place operations such as calculating an average, displaying information, and updating a grade with the student object's data as methods.

Use the cohesive unit: Work with the student object and its methods instead of passing the student's separate pieces of data through every operation.

Represent similar things: The same object-based idea can represent multiple similar students, with each object containing its own student data and related methods.

The program is organized around student objects that combine the data they care about with the functions that operate on that data.

Choosing the Program's Center

The two approaches emphasize different questions. Procedure-oriented programming asks what actions the program needs to perform and organizes reusable functions around those actions. Object-oriented programming asks what things exist in the program, what data each thing cares about, and how those things interact.

Procedure-oriented programmingObject-oriented programming
Organizes code around reusable procedures or functions.Organizes code around objects.
Focuses on what actions the program performs.Focuses on what things exist and how they interact.
Data is separate from functions and flows between them.Data and the functions that operate on it are bundled in an object.
A function receives, transforms, and returns data.An object's methods operate on the data held by that object.

Python's Flexible Approach

Python supports both procedure-oriented and object-oriented programming. You can write pure functions and organize an entire program around them. You can also organize a program around objects whose attributes and methods stay together. Python therefore lets you choose an approach or mix both approaches according to the needs of the problem.

For example, a Python program could use separate reusable functions for a simple sequence of data transformations, while using objects to represent multiple entities that have their own data and related behavior. The key idea is not that one paradigm replaces the other; Python allows both styles.

Common Design Mistakes

  • Treating object-oriented programming as merely putting every function inside an object.

    The defining idea is that an object bundles the data it cares about with the functions that operate on that data.

    Fix: Ask which data belongs to the object and which methods meaningfully operate on that data.

  • Assuming procedure-oriented programming cannot use reusable functions.

    Procedure-oriented programming is organized around reusable procedures or functions.

    Fix: Remember that both approaches can use reusable functionality; they organize the relationship between data and functionality differently.

  • Assuming Python requires object-oriented programming for every problem.

    Python fully supports procedure-oriented programming and allows programs to be organized around pure functions.

    Fix: Choose or mix paradigms based on the problem.

  • Confusing an object with only its data.

    In OOP, the object also contains the methods that operate on its data.

    Fix: Describe both the object's attributes and its related methods.

Practice the Distinction

MEDIUM

A library program represents many books. Each book has information about itself, and operations are needed that work with that book's information. Explain how you would describe this design in procedure-oriented terms and in object-oriented terms. Then state which approach more directly models the books as entities and why.

Hints
  • For the procedure-oriented description, focus on reusable functions and data passed between them.
  • For the object-oriented description, identify the data belonging to a book and the methods that operate on it.
  • Use the idea of multiple similar things interacting when explaining your choice.
  1. Procedure-oriented programming organizes a program around reusable functions, with data flowing between those functions. Object-oriented programming organizes a program around objects that combine attributes and methods. Objects are useful for modeling real-world entities and multiple similar things that interact. Python supports both approaches, so you can choose one or mix them according to the problem.

Key Takeaways

  • Procedure-oriented programming centers on reusable procedures or functions and the actions they perform.
  • Object-oriented programming centers on objects that bundle attributes with methods.
  • The main difference is how the program organizes the relationship between data and functionality.
  • Objects are useful for modeling entities and multiple similar things that interact.
  • Python supports procedure-oriented programming, object-oriented programming, and combinations of both.