You are on page 1of 4

Asignnment # 1: Difference between OOP & procedural programming Submitted by: Nadeem Ijaz Registration #: FA11-BCS-130-2D Submitted to:

pMaam Zara Hamid

Difference between OOP and procedural programming

Object-oriented programming (OOP) is a programming paradigm using "objects" data structures consisting of data fieldsand methods together with their interactions to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many modern programming languages now support OOP, at least as an option. Procedural programming can sometimes be used as a synonym for imperative programming (specifying the steps the program must take to reach the desired state), but can also refer (as in this article) to a programming paradigm, derived from structured programming, based upon the concept of the procedure call. Procedures, also known as routines, subroutines, methods, or functions (not to be confused with mathematical functions, but similar to those used in functional programming), simply contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program's execution, including by other procedures or itself. If you're just starting out, chances are that you are programming in a procedural manner. In a way, procedural programming could also be called linear programming. One thing happens and then the next. Code is executed from the top of the file to the bottom. OOP isn't as linear as procedural programming. Code is often broken up and distributed across multiple files, each one with a single purpose. OOP is also more abstract than procedural programming because it looks for patterns and reusability. The same code can be loaded and executed many times to accomplish a task without having to retype it. Instead of going into specific patterns, I'm going to share two helpful (and somewhat overused) acronyms you might keep in mind when trying to get yourself into an OOP mindset: DRY and KISS. DRY stands for Don't Repeat Yourself and it means just that. If you write some code, you shouldn't have to repeat that particular code ever again. In practical terms, it means thinking more abstractly and planning a little better at the outset. I'll give an example shortly.

KISS stands for Keep It Simple, Stupid and means that you should try to write code that accomplishes its goal in the simplest manner possible. Simpler means fewer possibilities for errors and easier maintenance. In the context of OOP, this usually means making sure that each method has only one task. If you find that a method does more than one thing, it usually means that it can be refactored into several smaller methods, each dedicated to a specific task.

A Real-World Example Okay, that's enough theory. We're going to put both types of programming to the test with a real-world example. Let's say that you are working for a vehicle parts manufacturer that needs to update it's online inventory system. Your boss tells you to program two similar but separate forms for a website, one form that processes information about cars and one that does the same for trucks. For cars, we will need to record the following information:

Color Engine Size Transmission Type Number of doors

For trucks, the information will be similar, but slightly different. We need:

Color Engine Size Transmission Type Cab Size Towing Capacity

In procedural programming, you would write the code first to process the car form and then the code for the truck form.

With object-oriented programming, you would write a base class called vehicle that would record the common characteristics what we need from both trucks and cars. In this case, the vehicle class will record:

Color Engine Size Transmission Type

We'll make each one of those characteristics into a separate method. The color method, for example, could take the color of the vehicle as a parameter and do something with it, like storing it in a database. Next, we will create two more classes: truck and car, both of which will inherit all of the methods of the vehicle class and extend it with methods that are unique to them. The car class will have a method called numberOfDoors and the truck class will have the methods cabSize and towingCapacity. Okay, so let's assume that we have a working example for both procedural and OO programming. Now, let's run through a few scenarios that we could come across in a normal working environment. You know the type of scenario because it always begins with the thought: I really wish my boss didn't send this in an email request at 4pm on a Friday afternoon. _____________________________

You might also like