
OOP vs Procedural Code - dave1010uk
http://blog.ircmaxell.com/2012/07/oop-vs-procedural-code.html
======
raganwald

        He [my father] taught me: "See that bird? It’s a Spencer's
        warbler." (I knew he didn't know the real name.)
        "Well, in Italian it's Chutto Lapittitda. In
        Portuguese, it is Bom Da Peida. ... You can know the
        name of the bird in all the languages of the world,
        but when you're finished, you'll know nothing
        whatsoever about the world. You'll know about the
        humans in different places, and what they call the
        bird. So let's look at the bird and see what it is
        doing -- that's what counts." I learned very early
        from my father the difference between knowing the
        name of something and knowing something.”
    

—Richard Feynman

~~~
kanak
In video form: <http://www.youtube.com/watch?v=05WS0WN7zMQ>

------
bunderbunder
The bit about these kinds of articles that always nags at me is the assumption
that different programming paradigms are somehow mutually exclusive. Essays
talk as if object-oriented code cannot also be functional, or code isn't
procedural if it involves classes, or whatever.

In some cases that might be true - declarative programming and procedural
programming, for example, are diametric opposites in many respects. But others
can be quite comfortable bedfellows. In particular, object-oriented
programming and its older cousin modular programming tend to play quite nicely
with other paradigms. Consider CLOS or Objective-C.

And even when they're opposed to each other that really just means you're
limited in how you can mix them on a per-line basis. You can still write a
multi-paradigm language that lets you support features from different styles.

Consider C#, which supports object-oriented, procedural, functional, and
declarative programming (with the last admittedly being somewhat constrained),
and where it's standard practice to use elements of each style all within a
single short procedure. Sometimes elements of different styles end up
depending intimately on each other. For example, to provide optimal support
for (declarative) query expressions, one will probably need to implement the
(object-oriented) IQueryable interface, and many of the methods in that
interface are expected to take (functional) anonymous procedures as arguments
and/or are used to build up (functional) monads representing the query that's
being declared, which are in turn represented as (object-oriented) instances
of classes that implement specific interfaces.

~~~
ExpiredLink
> The bit about these kinds of articles that always nags at me is the
> assumption that different programming paradigms are somehow mutually
> exclusive.

Paradigms are by definition mutually exclusive[1]. I'd argue that neither OOP
nor FP nor procedural programming ... are paradigms but merely styles or
idioms. Combining idioms is quite usual in our culture.

[1] <http://plato.stanford.edu/entries/thomas-kuhn/#3>

~~~
bunderbunder
I appreciate that you're trying to be pedantic, but taking a word that was
used correctly according to the jargon of the subject being discussed, and
claiming that it was actually used incorrectly because of this other
definition in an completely unrelated field isn't really pedantry. It's more a
failure to understand polysemy[1].

1: <http://en.wiktionary.org/wiki/polysemy>

~~~
ExpiredLink
"When I use a word," Humpty Dumpty said in a rather a scornful tone, "it means
just what I choose it to mean --- neither more nor less."

~~~
dllthomas
But if you expect other people to understand you, you need to be aware of how
they expect the word to be used and either use it that way or clarify at the
outset that you're using it differently.

~~~
ExpiredLink
'Paradigm' isn't used ambiguously. See also Wikipedia:
<http://en.wikipedia.org/wiki/Paradigm>

~~~
bunderbunder
At the top of that page, you'll see the following text:

> For other uses, see Paradigm (disambiguation).

Click on the blue, and you'll see a list of five bullet points. Click on the
blue in the third one.

------
shasta
No, please. OOP does not have a monopoly on abstraction, encapsulation,
modularity, etc. If you are focused on "state transformations and encapsulated
abstractions", you're not necessarily writing OOP.

~~~
TeMPOraL
> No, please. OOP does not have a monopoly on abstraction, encapsulation,
> modularity, etc.

Exactly. One of the best resources on those things that I've read/watched is
MIT's SICP (both books and video lectures). And it is about everything else
than OOP.

Abstraction, encapsulation and modularity are bigger than any programming
paradigm. They are orthogonal to OOP, et al., you think about them separately.
Moreover, they transcend the programming itself, and serve as one of the most
basic mental tools humans can use for everything in their lives.

~~~
Xurinos
As I recall in my reading of SICP, one of the first big chapters actually
hints towards OOP but has you designing it from the ground up. They never call
it OOP, but it is heavily leaning towards it; they just happened to set it up
using closures (see relevant humorous quotes below). I am referring to the
stuff about setting up a virtual function table that switches to the
appropriate method based on the type of the data so that multiple developers
can write their own way while honoring agreed interfaces/protocols.

"Objects are a poor man's closures." -- Norman Adams (?)

"Closures are a poor man's objects." -- Christian Queinnec (?)

------
pixie_
Even without OOP you're carrying state information one way or another. This is
a confusing article because his OOP examples are procedural, and his
procedural examples carry state in a global object.

------
Zenst
Procedural has a static state of flow working from the program controling the
data and OOP has a lose state of flow working with the data controlling the
program. Would that be a easier true definition?

Another way to look at this is if you have say a physical button that the user
can toggle on or off and it is lit up by code. Doing this is processed by
procedural code then the code will know what state the button is and if any
processing limitations will be slow in processing if the button is on or off
as could be indicated by slow transition to respond to the light going on or
off.

Now using OOP code the code that controls the light being on or off is would
in general isolated from the processing of the state of the button and can
lead to the the button showing on with the light on yet the processing code
still thinking it is off. IE the code has no fixed static processing path and
as such can lose state of if the button is on or off. This can lead to the
button being off and the underliying code thinking it is on as they desync.
Sorry if not the best explanation but is based upon a example I've seen in
real life. The example was a phone busy/not busy button and the lovely C++
object oriented code got into a kefufle thinking the butotn was on when it was
not after the operator had played track and feild on the button tapping away
at it real fast out of bordem. The next result was the call processing system
thought the phone was not busy when it was and tried to route a call which it
was unable to do and the upshot was the entire phone system crashed.

OOP has it's uses, but it also has it's downsides, like all approaches it is
the right tool for the job. Just that error handerling in OOP can get more
complex than boring old procedural code which is alot easier to deal with,
when it comes to exceptions as the processing state is static.

Another way to look at it is physical gears - procedural code is a fixed gear
and OOP is a gear cog that can change in size and whilst you will see the end
result you have no idea what size the dynamic OOP cog is.

Realy can be a hard one to explain without examples and it does lack a classic
Hello World example.

~~~
Xurinos
_Procedural has a static state of flow working from the program controling the
data and OOP has a lose state of flow working with the data controlling the
program. Would that be a easier true definition?_

No. (What is "static" vs "loose"? :)) Procedural programming is a control flow
pattern. OOP is a data arrangement pattern. You may be thinking of GUI systems
with single-threaded control loops and callbacks, which is another kind of
control flow pattern, but it is orthogonal to OOP. People who use OOP can,
within the methods or in calls to their objects, use a procedural approach, a
functional approach, and/or a callback approach. For different control flows,
there are sometimes more appropriate/maintainable data arrangements.

------
debacle
I disagree with this somewhat. The biggest gain for large pieces of software
from OOP is encapsulation, and in that regard writing loosely-coupled
procedural code within classes will provide you will proper encapsulation.

You can't get away from writing procedural code completely, because chances
are you're writing a _procedure_ , so even if you're thinking about state
transforms rather than a list of actions to take, you're still writing
procedurally.

~~~
danieldk
_The biggest gain for large pieces of software from OOP is encapsulation,_

But before OO languages became popular, people encapsulated data by using
opaque pointers. It can be argued that OO-programming promotes encapsulation,
but in practice it is not hard to accidentally leak the internal state of data
(especially in languages that only provide boxed data types).

Also, in (some) functional languages, encapsulation is comparably obtained by
not exporting the constructors of a data type.

So, I am not convinced that encapsulation is in any way an exclusive or even
defining property of OOP.

~~~
seanmcdirmid
OOP is defined primarily by its ability to abstract and encapsulate data (and
state) in objects. See William Cook's paper on "On Understanding Data
Abstraction, Revisited." Of course, there are other ways to encapsulate state;
e.g., as monads; OOP doesn't promote encapsulation so much as it promotes
encapsulation using objects. The second defining aspect of OOP is messaging
(goes along with encapsulation); Alan Kay has more to say about that (just
check out Message Passign on wiki). The third is probably dynamic dispatch and
polymorphism; not all OOP languages have that.

I've dealt with plenty of weird non-procedural OO languages (including my own)
with no recognizable procedure construct, but this is a much more
controversial position as there are no mainstream non-procedural OOP languages
out there (Erlang?). However, procedural programming is mostly orthogonal to
OOP, which is what I think the blog post is trying to get across?

~~~
danieldk
_OOP is defined primarily by its ability to abstract and encapsulate data (and
state) in objects._

I cannot help but thinking this is a tautology: 'Programming with objects is
defined by putting data in an objects.' What is an object? How does it defer
from other data structures where its members are hidden to the outside world?

I am not arguing that OOP does not exist, just that one aspect that has always
been touted as one of its greatest advantages, also exists in most non-OO
languages.

 _The second defining aspect of OOP is messaging (goes along with
encapsulation); Alan Kay has more to say about that (just check out Message
Passign on wiki). The third is probably dynamic dispatch and polymorphism; not
all OOP languages have that._

Is message passing without dynamic dispatch and polymorphism really message
passing? (In contrast to a function call that has a structure as an implicit
argument?)

 _there are other ways to encapsulate state; e.g., as monads_

A data type is a monad if a function is provided that defines how computations
of that data type can be combined[1], and that function obeys the monad laws.
A monad can be used to combine effectful computations (IO, ST) or threading
state through function calls (State). But e.g. the list, Maybe, or Either
monads have little to do with state.

[1] Excluding 'return' for brevity ;).

~~~
seanmcdirmid
Monads can be used to encapsulate state, they are used for other things. You
can use objects to encapsulate state, or you can use another style of data
abstraction, whatever floats your boat. OOP has no unique capabilities, you
just get to design your programs around objects, which is good enough for me.
Its all about design.

------
Knighty
>> The first sentence says that you must use object data structures to write
OOP.

Actually I don't think it does. It says that: "objects are data structures
consisting of data fields and methods together with their interactions". There
is no obligation.

True it would be easier implemented in a language provided data-structure. But
if you were crazy enough you could place the data-structure all in global
variables and try to tie them up using reflection.

------
eeeeaaii
Maybe oversimplified, but, these being the 5 programming paradigms listed in
Wikipedia:

Aspect-oriented (? not an expert on this)

Imperative/procedural (most work is done by statements)

Functional (most work is done by functions)

Logic (most work is done by declarations)

Object Oriented (most work is done by objects)

~~~
anamax
> Object Oriented (most work is done by objects)

Objects don't do work - methods do. Objects organize state.

Methods are functions with implicit state that may be shared with other
methods. These functions may be functional (as in Actors) or imperative.

Objects help organize state.

You can "OOP" in any language; it doesn't actually require any language-level
constructs. (Yup, you can do OOP in assembly language, Fortran 66, etc.)

------
beagle3
Obligatory <http://www.paulgraham.com/reesoo.html> -

OOP is not well defined. State your definition (use the above link as a
legend) before arguing about what OOP is and isn't, and there will be much
less miscommunication.

~~~
bunderbunder
It's an interesting observation, but I can't help but notice that none of the
nine members of that list includes the single essential feature that's shared
by all languages that claim to be object-oriented: The presence of a language-
level construct (an "object") which bundles data together with operations on
that data. The nine in the list are instead just a bunch of optional features
that an object-oriented language may or may not have in addition to that one
essential feature. Some of them aren't even particularly emblematic of OOP in
the first place. The most well-known poster child for #6, Erlang, is a (non-
OO) functional language that implements it among processes instead of objects.

In other words, while the essay is correct in observing that OOP can be a
frustratingly fragmented paradigm, the claim that it is "not a well-defined
concept" is a glaring case of failing to see the forest for the trees.

~~~
beagle3
> The presence of a language-level construct (an "object") which bundles data
> together with operations on that data.

And yet, many people argue that objects don't even need to have data to be
objects ( e.g. [http://stackoverflow.com/questions/6500127/whats-the-
overhea...](http://stackoverflow.com/questions/6500127/whats-the-overhead-of-
a-data-less-type) ) ... you can google "stateless objects" or "dataless
objects" and see that they were common enough at some point to be "considered
harmful"....

> The most well-known poster child for #6, Erlang, is a (non-OO) functional
> language that implements it among processes instead of objects.

Are processes not objects? I think they very much are.

> In other words, while the essay is correct in observing that OOP can be a
> frustratingly fragmented paradigm, the claim that it is "not a well-defined
> concept" is a glaring case of failing to see the forest for the trees.

If you have at least 4 incompatible definitions (as given by Rees) then it is,
by definition, NOT a well defined concept.

