Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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.
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.
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()>
<!--- And on another page that reports the number of current sessions. --->
<cfset numSessions = application.sessionCounter.getCurrentNumberOfSessions()>
<cfoutput>#numSessions#</cfoutput>
<!--- At this point, they don't know anything about each other,
so let's introduce them. --->
<cfset objectA.sayHelloTo(objectB)>