Você está na página 1de 7

1.

Object Oriented Programming v/s


Procedural Programming
The focus of procedural programming is to break down a programming task into a
collection of variables, data structures, and subroutines, whereas in object-oriented
programming it is to break down a programming task into objects with each "object"
encapsulating its own data and methods (subroutines). The most important
distinction is whereas procedural programming uses procedures to operate on data
structures, object-oriented programming bundles the two together so an "object"
operates on its "own" data structure.

Procedural and Object Oriented are the two most popular programming paradigms.
Procedure is another name for a routine, method or function. Procedural
programming refers to calling of a function whenever required. Functions are
separated from variables and data structures. Object oriented programming
paradigm, on the other hand, merges the data with the function and this function
changes the data that is bound to it in an entity called an object. Various useful
features such as abstraction, encapsulation and inheritance can be achieved while
working with Object Oriented approach

Nomenclature varies between the two, although they have similar semantics:

object-oriented procedural
method function
object module
message function call
attribute variable

In a procedural-based programming language, a programmer writes out instructions


that are followed by a computer from start to finish. This kind of programming had
its advantages, but an object-oriented language makes programming clearer and
easier to understand. Object-oriented software is all about using objects. An object
actually contains code (member functions) and data (data members). Traditionally,
code and data have been kept apart. For example, in the C language, units of code
are called functions, while units of data are called structures. Functions and
structures are not formally connected in C. A C function can operate on more than
one type of structure, and more than one function can operate on the same
structure.

Paradigm
A procedure is a sequence of instructions that are grouped together. These are executed one by
one when procedure is called. In such a technique the programmer divides his program in three
parts viz. Variables, data structures and routines. In a procedural approach, the entity called a
variable is termed as an attribute of the object in an object oriented paradigm. The idea is to
encapsulate data and method into a class. Class is a prototype. One can make any number of
objects hence code becomes reusable.

Procedural programming
In procedural programming our code is organized into small "procedures" that use and change
our data. These functions typically take some input, do something, then produce some output.
Ideally your functions would behave as "black boxes" where input data goes in and output data
comes out.
The key idea here is that our functions have no intrinsic relationship with the data they operate
on. As long as you provide the correct number and type of arguments, the function will do its
work and faithfully return its output.
Sometimes our functions need to access data that is not provided as a parameter, i.e., we need
access data that is outside the function. Data accessed in this way is considered "global" or
"shared" data.

So in a procedural
system our functions use data they are "given" (as parameters) but also directly
access any shared data they need.

Object oriented programming


In object oriented programming, the data and related functions are bundled together into an
"object". Ideally, the data inside an object can only be manipulated by calling the object's
functions. This means that your data is locked away inside your objects and your functions
provide the only means of doing something with that data. In a well designed object oriented
system objects never access shared or global data, they are only permitted to use the data they
have, or data they are given.
Global and shared data
We can see that one of the principle differences is that procedural systems make use of shared
and global data, while object oriented systems lock their data privately away in objects.
Let's consider a scenario where you need to change a shared variable in a procedural system.
Perhaps you need to rename it, change it from a string to a numeric, change it from a struct to an
array, or even remove it completely.
In a procedural application you would need to find and change each place in the code where that
variable is referenced. In a large system this can be a widespread and difficult change to make.

In an object oriented system we know that all variables are inside objects and that
only functions within those objects can access or change those variables. When a
variable needs to be changed then we only need to change the functions that
access those variables. As long as we take care that the functions' input arguments
and output types are not changed, then we don't need to change any other part of
the system.

Performance
Object oriented programming has been observed to give a lower quality performance as
compared to procedural programming. But this is not enough to conclude that procedural
approach is the best one to follow. As in case of C++ and C, although C is faster, still more
effective effort in needed to build program in C as compared to using C++. Sometimes the
performance increases due to features in object approach such as encapsulation, abstraction,
polymorphism and inheritance. LOC (line of code) in case of object oriented turn out to be much
less due to code reusability.

Perhaps the most serious limitation of procedural programming is the


tendency for large procedural-based programs to turn into "spaghetti-code".
Spaghetti code is code that has been modified so many times that the logical
flow shown in the figures above becomes so convoluted that any new
programmer coming onto the project needs a two month prep-course in
order to even begin to understand the software innards. Thus, an object-
oriented approach is taken to limit this so called "spaghetti-code." In object-
oriented programming, you build small, self-contained bits of code, which
correspond more closely to how you think about your program. Thus object-
oriented programming offers a new and powerful model for writing computer
software due to objects, which send and receive messages. This approach
speeds the development of new programs, and, if properly used, improves
the maintenance, reusability, and modifiability of software.

Features
Procedural programming provides modularity. Input values for a function to work with are
provided in the form of arguments and the returned value is the output. A procedure can only
access the variables in its scope hence coming up with the feature called scoping. A procedure is
an independent entity and can be put to use by different programmers in their entirely different
codes. Object oriented programming provides encapsulation making a class whose objects can
be created dynamically. It provides object inheritance. Again class provides modularity – a much
desired feature. Some other features that might interest you as a programmer are polymorphism,
abstraction and open recursion.
Summary
Procedural Programming
• Procedural programming refers to calling of a function whenever required.
• A procedure is a sequence of instructions that are grouped together.
• Program is divided into three parts: Variables, data structures and routines.
• Procedural programming provides modularity and scoping.
• Although programs written with procedural approach are fast and consume less space but
object oriented approach provides important features that make programming efficient.
Object Oriented Programming
• Object oriented programming paradigm merges the data with the function and this
function changes the data that is bound to it in an entity called an object.
• This property of combining attributes, data structures and methods in a class is called
encapsulation.
• Class is a prototype whose objects can be created dynamically.
• Polymorphism, abstraction, inheritance, open recursion, encapsulation and modularity are
some important features of object oriented programming.

What is an Object?
In object oriented programming, an object represents a bundling of data and functions that use
the data.
This is simply a description of the structure of an object, but we need to think about objects as
much more than this simple bundling. We can think of objects as though they were very little
living entities; they have a lifespan, they can do things, and they can communicate with each
other.
Objects have a lifetime
Some objects may have a very short lifespan and perhaps only exist for the duration of a single
page request. Other objects may live much longer and may exist throughout the lifetime of your
application.
Objects that have a short lifespan are those that you create on the fly during a page request. You
might use it a few times during that page request but it is discarded when the page request
finishes. Objects that have a longer lifespan are those you create and store in the session or
application scope. These survive for as long as session or as long as the application is running.
In ColdFusion code this is most easily done using the createObject() function. createObject()
returns a reference to an object so we can keep it around for a while.
<cfset application.importantObject =
createObject("component","ImportantApplicationObject").init()>
Because objects have a lifetime, this also means that they maintain state. The variables inside
them can, and typically will, change during their lifetime. A simple example might be an
application object that counts the number of visitors on a site. Whenever a new session starts up
this counter is increased. When a session ends this counter is decreased.
<!--- Create the counter. --->
<cfset application.sessionCounter =
createObject("component","SessionCounter").init()>

<!--- Some time later when a session starts up. --->


<cfset application.sessionCounter.newSessionStarted()>

<!--- And later when the session ends. --->


<cfset application.sessionCounter.sessionEnded()>

<!--- And on another page that reports the number of current sessions. --->
<cfset numSessions = application.sessionCounter.getCurrentNumberOfSessions()>
<cfoutput>#numSessions#</cfoutput>

Objects are your own personal little workers.


Once you have brought an object to life you need to tell it to do things for you. Objects don't get
tired or bored and are there make you happy so they will stick around as long as you need them
so you can keep on asking them to do things for you.
This is a fundamental idea to get; objects are there to do work for you. You need to think of them
as little workers who only know how to perform very specific tasks and you can ask them to do
your bidding. They are much more than a simple bundle of data or a wrapper for some functions.
<cfset worker.doSomething()>

Objects can communicate with each other


This simply means that one object call another object's functions. When one object calls another
object's function it might be to ask the object to do something, or to ask the object for some
information.
However, objects can't just communicate with any object anywhere in the system. Objects can
only communicate with other objects that it has been "introduced to". This simply means that an
object can only communicate with other objects that have been passed via a function call.
<!--- Create a couple of objects --->

<cfset objectA = createObject("component","ObjectA")>


<cfset objectB = createObject("component","ObjectB")>

<!--- At this point, they don't know anything about each other,
so let's introduce them. --->

<cfset objectA.sayHelloTo(objectB)>

<!--- This passes a reference of object B into object A,

Você também pode gostar