Hacker News new | past | comments | ask | show | jobs | submit login
OOP vs Procedural Code (ircmaxell.com)
53 points by dave1010uk on July 12, 2012 | hide | past | web | favorite | 43 comments



    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



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.


This.

Even declarative and procedural program can have some interactions. It is very hard to do anything with just declarations ;-), and there is some logic to making the procedures of the code clear and explicit.

A lot of stuff comes back to 'be simple, be clear.' And if mixing paradigms lets you do it, go for it!


> 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


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


"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."


And in this case, I meant it to mean the concept you'll read about if you google "programming paradigm." As clearly indicated by my use of the phrase "programming paradigm."

You can find out about a different concept if you google "philosophical paradigm". That'll give lots of links that talk about Thomas Kuhn.

TL;DR: It may be called a drum, but that doesn't mean you can use a bongo to repair your car's brakes.


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.


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


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.


In my experience, in software, it's often used loosely. However, I was responding primarily to the use of the quote itself.


"Make the following experiment: say 'It's cold in here' and mean 'It's warm in here.' Can you do it?"

Wittgenstein, Philosophical Investigations §510


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.


From the article: "You can completely implement data abstraction, encapsulation, messaging, modularity, polymorphism and (to a limited extent) inheritance without using an object structure."

So the author agrees with you. So please, no "No, please."


Yes but he wrote that to support his view that abstraction+polymorphism+encapsulation=OOP even when you are not using object structures: "The first sentence says that you must use object data structures to write OOP. That's blatantly wrong. You can completely implement data abstraction, encapsulation, messaging, modularity, polymorphism and (to a limited extent) inheritance without using an object structure." I don't much enjoy arguments about semantics but I personally feel his definition of OOP is way too broad to be useful. I am tempted to believe it was called tempted to believe it was called Object Oriented Programming for a reason.


Someone downvoted you, so I think we should elaborate that further. I think you mean the right thing, but cited the sentence which only strangely says that OOP can be without objects (which alone doesn't mean that the focus on on "state transformations and encapsulated abstractions" is an OOP-thing). He wrote later on:

>But if you're focusing on state transformations and encapsulated abstractions, you're writing OOP.

So he is indeed saying that you can write OOP without objects, but also that the focus on "state transformations and encapsulated abstractions" is OOP.

I think that this is a really strange way to think about OOP.

edit: that was prior to an edit of the comment i respond to.


Thanks. I edited my comment to try making it more clear. Another very important point that was mentioned here is that programming paradigms are not mutually exclusive. The author of this post seems to create a false dichotomy between procedural and OO programming.


That wasn't my intention at all. I'm sorry if that's how it came across. It's just that I got tired of seeing people refer to code as procedural because it uses functions, and refer to code as OOP because it uses classes. I was just trying to point out that the approach is different from the tools that you use to implement the approach...


I think it's true that you can encode OOP objects in any number of languages, but OOP really is about the objects. If your encoding isn't little bundles of methods, it's not OOP, even if it uses the other general techniques you've listed: abstraction, modularity, etc.


I agree, but I would state that it's easier to do encapsulation and modularity in OOP than procedural style.

I prefer the functional method, though. In my opinion it's much easier to understand and much more flexible, but many programmers dislike that flexibility.


"Procedural style" is a strawman though, corroborated by years of people thinking C and FORTRAN are the only procedural languages out there.

Most OOP languages are still procedural and many "traditional" procedural languages provide support for data abstraction.


The syntactical sugar provided by languages that support objects is the only real improvement over something like ADTs, but I think it's a noteworthy improvement.


> 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.


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 (?)


SICP walks through a number of paradigms, including OOP. OOP is a pattern than allows abstraction, encapsulation and modularity in specific ways. Other patterns or paradigms may provide these features in different ways. You would create different abstractions in an OO pragram than in FP, even if you were solving the same problem. So abstraction is definitely not orthogonal to programming paradigm.


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.


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.


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.


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?


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 ;).


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.


That's all well and good, but that's not what wikipedia says.


Are you implying that wikipedia is the authority on all topics? Given it can be edited by anyone at anytime I would suggest that comments on HN or elsewhere are no more or less valid than what wikipedia says


> comments on HN or elsewhere are no more or less valid than what wikipedia says

I'm not going to be dragged into a discussion based on such a ridiculous premise.


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.


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.


>> 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.


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.


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.


> 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... ) ... 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.


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)


> 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.)




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: