OOP vs Procedural Code
blog.ircmaxell.com
blog.ircmaxell.com
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 FeynmanIn 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.
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!
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.
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.
> 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.
Wittgenstein, Philosophical Investigations §510
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.
Most OOP languages are still procedural and many "traditional" procedural languages provide support for data abstraction.
So the author agrees with you. So please, no "No, please."
>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.
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.
"Objects are a poor man's closures." -- Norman Adams (?)
"Closures are a poor man's objects." -- Christian Queinnec (?)
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.
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.
I'm not going to be dragged into a discussion based on such a ridiculous premise.
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?
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 ;).
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.
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.
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.
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.
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.
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.
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)
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.)