FOUNDATIONS OF PROGRAMMING : CODES AND OBJECTS : 05.03 PROCEDURAL AND OBJECT-ORIENTED PROGRAMMING

STUDY
PLAY
Procedural vs. Object-Oriented Programming
The term "design approach" refers to the way a programmer thinks about his or her code. This choice of approach can vary by project, or by programming language. There are two main design approaches used in programming.

First up is procedural programming. When you're learning to program, you often start with a high-level description of what you want your program to do, and then break it down, step-by-step, into the functions needed to complete your actions. You do this when you write programmer-defined functions. You take it one step at a time, first deciding what your function will do; next, what it will be called; and then, how it will work. As you go, you write the code needed to execute your program. Manufacturing vehicles from scratch would also be like procedural programming.

The other design approach is known as object-oriented programming (OOP). In OOP, the programmer thinks in terms of objects rather than functions. Objects are defined using a class and are parts of the program that can carry out certain actions and interact with each other. Manufacturing vehicles from a template is similar to OOP.
Procedural Programming
Description An approach to programming that uses a list of functions, or procedures, that tells a computer what to do step-by-step.

Application Also known as "Top Down Design," programmers will start at the beginning and work through every step.

Languages Early programming languages, including Fortran, COBOL, C, and BASIC, are mostly procedural.

Example If you were planning a party the "procedural way," you'd list all the steps that need to be done:
Send invitations
Buy food
Decorate
Make a playlist
Prepare games
Object-Oriented Programming
An approach to programming that uses objects to carry out different actions.

Uses classes to define and create objects for a program. Programmers will write code and have objects interact and perform actions.

Most of today's newer languages, such as Python, Java, and C++, are object-oriented.

If you were planning a party the "OOP" way, you would call [the object] partyPlanner that you used for your last party and have it do everything you need.
OOP Vocabulary
Most things you encounter daily are objects. Cars, furniture, even your phone. For each object, you can customize the way it looks, its different features, and what it can do. We define objects by their different attributes and behaviors.

These real-life objects can be simulated with program code. Take a look the following image to learn about some important terminology related to object-oriented programming.


Class: This refers to a template used to create new objects. Think of it as a blueprint that has existing characteristics you can use when creating new, but similar objects. Each time a new contact is added to your phone, it uses the contact class.

Instance: Many instances of a class can be created. For example, an object is an instance of a class. One individual contact in your phone is an instance of a contact object.

Constructor: This refers to the code that allows you to create a new object. It is a special method used when a new instance of a class is created. The initial values of the attributes are set. This is like adding (or constructing) new contacts in your phone.

Attributes: This refers to the features of an object. The attributes of a contact might include a phone number, email address, or picture. Attributes are often nouns and adjectives that define the properties or characteristics of the object.

Initializing: This refers to what the constructor will do when you use it to create a new object. It will initialize the object in the program. When you create a new contact, all of the attributes will have initial values.

Behaviors: Also known as methods, this refers to the actions an object performs. You can set specific ringtones or vibration patterns to a specific contact so that when that person calls, that specific behavior is performed.
Advantages of OOP
REUSABLE CODE
Once programmers write a useful class for one programming project, they can often reuse that class in another program instead of starting from scratch. Example: If you create a class for a deck of cards, you can reuse it for any program needing a deck of cards, like Solitaire or Go Fish.

INHERITED TRAITS
Programmers can create new objects that inherit traits from existing classes. This means they can add new features to an object without having to recreate it from scratch, saving tons of time and effort. Example: If you create a vehicle class that has all the basic functionality of a vehicle (e.g. moves, carries people or things, starts and stops), you could build on that class to create more specific types of vehicles, like convertibles, bicycles or a semi-truck.

FLEXIBILITY
Programs created using OOP are easier to develop because of their flexibility. Objects can be used in a variety of ways and have different behaviors for different purposes, depending on the program. Example: If you create a class for your bank account, many bits of information (e.g. name, social security number, birthdate) are needed to open an account or get a loan. But for simple transactions like making a deposit, only basic information like your name and account number are needed. Your bank account class can work for all of these.

EASY TO MAINTAIN
Object-oriented programs are written modularly (in objects), making them easier to maintain and understand once developed. Example: If you're planning a meal and you burn the cupcakes, you only have to fix the cupcakes, not the whole meal. In OOP, objects can be maintained separately, making finding and fixing problems much easier.
YOU MIGHT ALSO LIKE...