Skip to main content
GameDev.net gamedev.net
🔒 Locked

OOP and Procedural Programming Dilemma

Started by DrZoidberg Dec 29, 2004 at 2:05 PM 59 replies 11.2k views
Original Post
DrZoidberg
DrZoidberg
hello, i would just like to give my opinion and ask everyone elses opinion on something iv'e just sorta realized. first off i've been coding in c++ for only about 3 years and before that 3 years in qbasic, and some in pascal. after the first 2 years learning c++ i became completly obsessed with coding everything object oriented, maybe it's my obsessive compulsive over organizing self but everything i did, i had to do it the OO way. just recently i picked up the book "game programming all in one: SE", although when i first opened it up it seemed a little under my level, as i have already coded the basics like pong, pacmac, breakout, a mario clone, etc. but it 'teaches' the allegro library which i have never used and even though now i'm trying to get a project up in irrlicht i realized i have never made a full 2d game, with all the bells and whistles. so being the person i am, i start from page 1 and do everything (regardless how trivial it is sometimes), and made a seocnd discovery, the code is all in C. which means that there will be no OOP. well i just continued on and realized something ... OOP ruined progamming for me, and ruined making games. i remember in qbasic it wasnt a chore to make games, but when i started c++ and trying to do everything OOP it was a chore. for the time i spend trying to code an OO game framework i forget why i am even making a game, i forget why i started progamming in the first place. for instance say i'm making pong, now if i wanted to do it the OOP way maybe i'd do something along these lines. class CPaddle { private: int position; int speed; int color; public: CPaddle(); void move(); // ... blah blah }; class CBall { private: int x; int y; // blah blah }; now if i did this the 'procedural' way i would do this: struct { int position; int speed; int color; } paddle[2]; // i only need 2 paddles. struct { int x; int y; int speedx; int speedy; int color; } ball; // i only need 1 ball void movePaddle(int index) { paddle[index].position += paddle[index].speed; if (paddle[index].position >= // blah } actually i dont even see the point to have the ball a struct, i only have 1 ball, other than organization its pointless to have the ball a struct at all. this brings me to another point ... the singleton class, what's the point, i admit i am a little ignorant on this particular subject, but why make a class with only 1 instance? it would seem to me it would be a hell of a lot easier to just have some global functions and pass things around through the parameters. why even bother with the encapsulation? if i label my variables well im not going to mix them up. i understand that yes some things are way easier and more practical the OOP way (container objects, obiviously everything in the STL is OOP) and how many games are programed OO compared to procedural ... seems to me ID software still uses C (people have said carmack uses 'hacks' etc to get the code to work, and that its messy ... but why does it matter? the game works, i can play it, it doesnt crash, probably runs faster too) but really what's the big deal with OOP anyways, other than the code looking all nice and pretty what does the progammer gain? what does the actual program (game) gain? is it so wrong to mix OOP with 'classic' procedural programming? maybe it's that i mix the wrong levels of abstration (which i've been told), maybe i'm just not that good progarmming OO. but to me, coding OO took a longer time, it seemed like a chore, and ultimatly was more difficult and less rewarding than if i'd just done it the old fashioned way. thanks for taking the time to read this, and please post some feedback. thanks DISCLAIMER: and no flame wars :)
Woop woop woop woop!
dhartles
dhartles
One major benefit in OOP is code reuse. IMHO, it is much easier to reuse an engine coded OOP style for game upon game than an engine written in procedural.

Also, you have to remember commercial games can use hundreds of developers. It is much easier to develop in that type of enviroment when using OOP than procedural.

If it's just you, developing 10K LOC games, then it doesn't really matter. If procedural seems easier/faster for you go for it.
Spoonbender
Spoonbender
Quote:
Original post by DrZoidberg
actually i dont even see the point to have the ball a struct, i only have 1 ball, other than organization its pointless to have the ball a struct at all.

True. But isn't the organization a good point? If you have 2 million lines of code, I think you'd appreciate a bit of organization. ;)
Organization is really the main point about OO in general. It doesn't magically provide you with anything new, it's just a tool for organizing your code.

Quote:

this brings me to another point ... the singleton class, what's the point, i admit i am a little ignorant on this particular subject, but why make a class with only 1 instance? it would seem to me it would be a hell of a lot easier to just have some global functions and pass things around through the parameters.

Could be your renderer? Or sound subsystem. It should be obvious why you only want one of those. Next up, why bother to make it a class?
To prevent you from doing silly things, basically. You don't want to spend 2 days debugging your code, only to find out that you tried to render a triangle before initializing your renderer. And this leads nicely to your next question :)
Quote:

why even bother with the encapsulation? if i label my variables well im not going to mix them up.

First of all, you might not mix them up now. But what if you were 5-10 programmers on the project? Are you sure you wouldn't get one of their variables mixed up if you had to interact with their code?
Or what if your project is so big that you can't remember all variable names?

But second, and much more importantly, encapsulation ensures that you do things the intended way. As a simple example brought up by the last person here who asked about this, imagine you're adjusting the health of the player's character. True, who needs encapsulation for that? "health -= 10" is just as good as "player.health -= -10", or "player.adjustHealth(-10)", isn't it? But what if you want the health to affect other things? What if health also affects your movement speed? Then it's a whole lot easier to just add a line to adjustHealth(), making that update the players movement speed when the health is changed, than it is to *remember* to adjust the movement speed manually *every time* you change the player's health.

Quote:

and how many games are programed OO compared to procedural ... seems to me ID software still uses C (people have said carmack uses 'hacks' etc to get the code to work, and that its messy ... but why does it matter? the game works, i can play it, it doesnt crash, probably runs faster too)

I heard that they've started using C++ and OO lately. But the main point is that as long as they can keep track of their own code, then there's no problem.

Quote:
but really what's the big deal with OOP anyways, other than the code looking all nice and pretty what does the progammer gain?

That's not enough for you? Sounds damn important to me. If I can spend one less hour per day looking through my code trying to find some stupid little error, then I'm happy.

Quote:

what does the actual program (game) gain?

Nothing. It might have fewer bugs because the programmers have an easier time keeping an overview of the code, but that's not neccesarily true.

Quote:
is it so wrong to mix OOP with 'classic' procedural programming? maybe it's that i mix the wrong levels of abstration (which i've been told), maybe i'm just not that good progarmming OO. but to me, coding OO took a longer time, it seemed like a chore, and ultimatly was more difficult and less rewarding than if i'd just done it the old fashioned way.

You'll probably get lots of different answers on this, but I'd say it's ridiculous *not* to mix OOP and non-OOP. For some trivial tasks, OOP is not just pointless, but a huge amount of extra work. OOP is one tool among many. Use it when it makes sense.
steven katic
steven katic
Yeh, It's that simple, if you feel you can benefit from applying OOP do it, otherwise don't bother.

From a Pragmatic view: The critical advantages of OOP is that it provides a way of managing larger programs. The idea of OOP was invented long before it became popular to use; When it became possible to right "huge" programs, a way of managing so much code needed a solution: OOP type techniques became a popular way of doing so.

Here is an analogy:

Lets say you plan on writing a one page essay that gives the definition of an axe: You may use a few paragraphs. Then - type it out, print it out and whatever.

Now, lets say you plan on writing a whole set of encyclopedias: You may divide all your info into separte books, then chapters/sections and paragraphs;table of contents; and index. And probably use a team of people to create it if you plan on publishing it before you die.

Applying OOP would be a bit like applying the organisation techniques of books/sections/paragraphs/table of contents and index to the information you have. A way of managing higher complexity.

I do not doubt that there are many more nuances and subtleties associated with applying "OOP" as opposed to "Procedural" organization to a hunk of software that are not addressed in my little analogy (that is the advantage of using the analogy) - They are left to the OOP purists and zealots to debate.

You mentioned Carmack and ID still using C: I thought they started using C++/OOP some time ago?

Oh, another reason forusing OOP->
You can be trendy and not so trendy. And the Software Dev Industry is not immune: it is trendy to use OOP or probably just standard practice depending on the circles you travel in. So probably the other reason that you would use OOP is....to be trendy. Its not a bad reason: You don't want to end up like the shop keeper Appoo in the Simpons do you? Well not unless you want to be a Shopkeeper of course: He has a PHD in computer science
and keeps his little program in a box under the counter on thousands of little punch cards (obviously written in the software 'stone age' when punch cards were trendy or probably the only way to store a program?).
JuNC
JuNC
Really there haven't been any convincing cases of where OOP is better than properly modular procedural code for code reuse or encapsulation (please, demonstrate them if you've got them). Unfortunately the most popular procedural language (i.e. C) simply doesn't have an adequate module system. Object orientation is a tool like any other, you could equally use a functional style, procedural style, whatever. The key is breaking the problem up into properly segregated components.

Object orientation certainly does have advantages *for some application domains*, just as logic programming does in others, or functional does, or procedural does etc. etc.

Know all of the tools at your disposal (you *do* know what functional programming is, don't you?) and pick the appropriate one for the task. (May I suggest OCaml if you want to see a language that combines procedural, functional and object orientated styles with a powerful module system - you'll see that it is the modules and not the objects which provide the structuring power).

Of course in industrial settings it's nowhere near as easy as all that, you probably don't get to choose your language and library sets so you'll be limited. That doesn't mean that exposure to other paradigms will hurt you though (esp. if you will eventually become management level!).
Spoonbender
Spoonbender
Quote:
Original post by JuNC
Really there haven't been any convincing cases of where OOP is better than properly modular procedural code for code reuse or encapsulation (please, demonstrate them if you've got them).

Well, I demonstrated a very simple example above. Without OO mechanisms, there'd be nothing to stop you from creating some inconsistent state in your game (For example by adjusting health without adjusting the things that depend on it.)
With OO, the thing wouldn't compile if you tried to access the health variable directly.

But you're right, it's one tool among many. Just make sure you know as many of them as possible, and use what makes sense.
amag
amag
Quote:

Really there haven't been any convincing cases of where OOP is better than properly modular procedural code for code reuse or encapsulation (please, demonstrate them if you've got them).


COM (or XPCOM for mozilla)? Even though COM can be used with C I hear it's a BIG pain.

Some things do come for free when using OOP, for instance when your domain is very hierarchial, ie a parser or a scene-graph. Then it's so much easier to use OOP.

You just have to learn when to apply what tool to get the job done. That's what takes time when you learn to program. A programming genius can manifest him/herself at a young age but a software engineer takes time to mature, time exposed to code. You don't become a SE over a night.

For instance I designed the text-drawing system for an editor I'm developing in an hierarchial OO-way. This allows me to add features by just adding a class. It's much more powerful and easy to maintain than if I had written a series of switch/case.
JuNC
JuNC
Quote:

Original post by Spoonster
Well, I demonstrated a very simple example above. Without OO mechanisms, there'd be nothing to stop you from creating some inconsistent state in your game (For example by adjusting health without adjusting the things that depend on it.)
With OO, the thing wouldn't compile if you tried to access the health variable directly.


You missed the part where I said 'with an adequate module system'. I probably didn't phrase that first sentence very well. Please don't think about C as being a model of procedural programming here. If anything, Pascal is closer but nowhere near the power of OCaml's (or Scheme + modules) procedural side.

Quote:

Original post by amag
COM (or XPCOM for mozilla)? Even though COM can be used with C I hear it's a BIG pain.


This isn't what I meant at all. I want to hear evidence of large systems which have been made 'better' by being object orientated over procedural. There is 'evidence' on both sides but I've yet to hear convincing evidence from the OO side, some interesting reading: clicky. You may not agree with conclusions of course, I'm extremely interested in finding out if OO is actually beneficial or is just a fad so please provide evidence.

Quote:

Some things do come for free when using OOP, for instance when your domain is very hierarchial, ie a parser or a scene-graph. Then it's so much easier to use OOP.


This is true (as I said), but if you haven't tried writing a parser in a functional language (can I mention OCaml again?) then don't talk about things 'for free' because you have no idea ;)

Scene-graphs I would say 50-50, they are very OO in nature, although it depends very much on what you mean by scenegraph and what your intended operations are. If you want to do a lot of introspection then OO probably is preferable over FP.

Quote:

For instance I designed the text-drawing system for an editor I'm developing in an hierarchial OO-way. This allows me to add features by just adding a class. It's much more powerful and easy to maintain than if I had written a series of switch/case.


Sure, as I said OO is a tool like any other, you can always find problem domains to which it is suited. I don't know your particular domain so I wouldn't even try to provide a counter example of how to do this in OCaml or Scheme.
Arild Fines
Arild Fines
I'm getting a bit tired of hearing this "all paradigms are created equal" crap. Give it a rest.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
JuNC
JuNC
Quote:

Original post by Arild Fines
I'm getting a bit tired of hearing this "all paradigms are created equal" crap. Give it a rest.


Support your opinion or shut up. Of course all paradigms weren't created equal, OO was way down the pecking order but just happened to occur at the right time with the right proponents to capture management imagination with helpful slogans like 'code reuse' (yeah, this didn't exist before) and 'encapsulation' (yeah, this didn't exist before) and 'polymorphism' (ha! most OO nuts wouldn't know polymorphism if it bit them in the ass).

(I'm far more moderate than that usually but I agree with Zahlman, this needs more heat :P)
hplus0603
hplus0603
Quote:
One major benefit in OOP is code reuse.


There's research that shows this to be a myth, or at least a poorly supported claim by certain OOP software vendors.

Proper factoring, and proper modularization, and proper abstractions result in re-usable code. A willingness to re-use code results in code re-use. OOP doesn't add or subtract much to/from that.

Some of the most re-used code in the world is the GNU C library.
enum Bool { True, False, FileNotFound };
Kylotan
Kylotan
Quote:
Original post by Arild Fines
I'm getting a bit tired of hearing this "all paradigms are created equal" crap. Give it a rest.


Ah, I think that paradigms are created equal. However, language support for them clearly is not. Implementing an inheritance pattern in C is a little more long-winded than in C++, which obviously means more typing, more opportunities for error, and so on.

I think the key is to make sure you use the tools available to you appropriately. I rarely waste time on big up-front object models and so on. Instead I tend to code the first version of anything using globals and one big function, so I am testing pure logic and not wrestling with any sort of object system. Then I'll refactor things so the globals become parameters, and the function gets split up to reduce repetition and increase coherence. Eventually I tend to move the related functions into a class and move any former globals into class members.
RipTorn
RipTorn
This is one of those topics that never goes away..

simply,

in OO, you write more to write less.
Abstracting the representation of objects from data.

make your choice I guess. If you don't like it, don't do it.
Mr Lane
Mr Lane
This is something I have often argued with many of my friends about at university. Everyone is a OO fan but me, but i figure its simply cause they are told by the departmnent academics that OO is better, especially when it comes to the demands of industry.

I can see this when you have very large software teams say 10-50 people, but for a single programmer to up to about 9 like most small game dev teams would be, I really think there is nothing wrong with proceedual.

I choose to use C whenever I can, because I can plan what I am going to do on paper, think of how I am going to implement it, and where I am going to in my app, and then just sit down and start doing it. I dont have to spend time planning what should be what class, how everything is going to be inherited from where, and OO forces you to plan the design around future features which you are not even close to getting around to implement. I find with OO you have to spend time planning the big picture of how your code will come together first rather then taking things a part at a time.

In short using C lets me think about my engine and the features I want and how to do them, and I dont have to think about how to *code* them. With C++ in true OO style, you have this extra complicating factor. Aside from this I also think alot of things dont make sence in OO anyway. I started C before any OO, but Java was my first OO. I hated the fact that everything in strict OO *had* to be inside a class...some things, like very general and reusable methods that can be used for any number of unrelated parts of your program should not be members of a class. I dont know, maybe I am just a bad OO programmer, or I need to get more practice with design.

When I do use C++ because I have to for whatever reason (I will be writing my editor in C++ as wxWidgets uses C++ libs) I still write in C style most of the time, tho the extra C++ features do come in handy.

The best example of OO I have seen in a game is Unreals Actor Class system. My engine aint Unreal though :) Maybe in the future if i ever make it big I will regret not doing things in true OO.
Winograd
Winograd
Quote:
Original post by Spoonster
Quote:
Original post by DrZoidberg
actually i dont even see the point to have the ball a struct, i only have 1 ball, other than organization its pointless to have the ball a struct at all.

True. But isn't the organization a good point? If you have 2 million lines of code, I think you'd appreciate a bit of organization. ;)
Organization is really the main point about OO in general. It doesn't magically provide you with anything new, it's just a tool for organizing your code.


Well, you can organize pure C code as well. For example, my opinion is that linux kernels source tree is pretty well organized. It's (atleast partly) written in OOP manner, despite the fact that it is written in C and assembly, but I don't think it's the OOP part making the code well organized (it helps though).
amag
amag
Quote:

Quote:

Original post by amag
COM (or XPCOM for mozilla)? Even though COM can be used with C I hear it's a BIG pain.


This isn't what I meant at all. I want to hear evidence of large systems which have been made 'better' by being object orientated over procedural.



Yeah, and that's what I mean, you just didn't see that my point was not just a point but a line...
Very many of todays win-applications use COM for their benefit. This is code-reuse where applications can grow more complex by incorporating more COM-objects.

Anyway my point wasn't all for OO (if you read it again I argue that it's a tool and it's up to the programmer to apply the right tool for the job).

Quote:

Quote:

Some things do come for free when using OOP, for instance when your domain is very hierarchial, ie a parser or a scene-graph. Then it's so much easier to use OOP.


This is true (as I said), but if you haven't tried writing a parser in a functional language (can I mention OCaml again?) then don't talk about things 'for free' because you have no idea ;)


Actually I have an idea. I've written several parsers in Haskell. I have also written a simple (faked)multi-threaded webserver and a Tetris-clone. The point of this is that with a functional programming language some things are tremendously easy to write, but other things are harder than they need to be (could of course be said about any programming paradigm). But in fact I have listen to long complex talks about how to be able to do things in a functional language that one takes for granted in a procedural or OO-language.

And about that article, I have scimmed it and I'm afraid I think it's ridiculous. First off, I'm all for other programming paradigms, the programmer must learn to choose what tool is the right for the job.
That said, I think it's ridiculous to compare how many bugs or how long time some thing takes to implement in a specific language. It's all biased towards the developers preferences. I mean if you're a great C-programmer, but knows little or nothing about polymorphism, then of course it will take a longer time and you will introduce a lot more bugs when forced to use a tool you cannot properly handle.
JuNC
JuNC
Quote:

Original post by amag
Yeah, and that's what I mean, you just didn't see that my point was not just a point but a line...
Very many of todays win-applications use COM for their benefit. This is code-reuse where applications can grow more complex by incorporating more COM-objects.

Anyway my point wasn't all for OO (if you read it again I argue that it's a tool and it's up to the programmer to apply the right tool for the job).


I don't deny that many applications are using COM, but the real question is can we provide objective evidence that using those OO concepts are actually providing benefits? Not really because the alternatives *do not exist*. Calling COM OO is stretching things anyway IMO, the line between object orientated and properly modular procedural code in most languages is thin at best. Part of the confusion (not yours, but in general) is that OOP is so ill defined, the bulk of OO code written in C++ is just procedural wrapped in a greasy class wrapper. Ruby is pretty close to fully object oriented (more so than Python for ex.), how easy is it to use COM from Ruby? (just out of interest, I don't know)

I got your main point though, sorry if you thought I was picking on you, I get a bit vitriolic at times :)

Quote:

Actually I have an idea. I've written several parsers in Haskell. I have also written a simple (faked)multi-threaded webserver and a Tetris-clone. The point of this is that with a functional programming language some things are tremendously easy to write, but other things are harder than they need to be (could of course be said about any programming paradigm). But in fact I have listen to long complex talks about how to be able to do things in a functional language that one takes for granted in a procedural or OO-language.


Exactly, you and I broadly agree I believe. I don't know what you mean by 'long complex talks' though, I find FP highly intuitive, perhaps it's just Haskell is pretty damn weird if coming from a procedural mindset which is why I prefer OCaml most of the time. I never ever advocate single paradigm programming, sticking strictly to FP is a bit silly IMO (just as sticking totally to OO is).

Quote:

And about that article, I have scimmed it and I'm afraid I think it's ridiculous. First off, I'm all for other programming paradigms, the programmer must learn to choose what tool is the right for the job.
That said, I think it's ridiculous to compare how many bugs or how long time some thing takes to implement in a specific language. It's all biased towards the developers preferences. I mean if you're a great C-programmer, but knows little or nothing about polymorphism, then of course it will take a longer time and you will introduce a lot more bugs when forced to use a tool you cannot properly handle.


You know what? That's what I thought when reading it and I think the conclusions are tenuous at best. However, the point of the article (as I see it) is that there is virtually no objective evidence (at least, I haven't found any and I've been looking for quite a few years now) as to why OO is advocated as the 'best' paradigm for all of the benefits it is touted as having. Simply having systems that work which were written in OO style is not actually enough to provide that evidence, we also have huge systems (e.g. OS kernels) written in modular procedural style which work too.

Really (as you say), it's all about knowing what tools are available and choosing the appropriate ones, without worrying about 'proper' style or massaging (read munging) your code into a style just because everyone else is.

Extrarius
Extrarius
Quote:
Original post by JuNC
Really there haven't been any convincing cases of where OOP is better than properly modular procedural code for code reuse or encapsulation (please, demonstrate them if you've got them). Unfortunately the most popular procedural language (i.e. C) simply doesn't have an adequate module system. Object orientation is a tool like any other, you could equally use a functional style, procedural style, whatever. The key is breaking the problem up into properly segregated components. [...]
It seems to me that when you say 'properly modular procedural code' you're realling saying "OOP without classes" which is entirely possible to do.

You can write extremely OOP code in C using structures and functions that operate on them. The only real difference from C++ is that you don't have compiler-enforced member-access rules, but the same is true of Lisp and it handles OOP at least as well (many would say far batter).

If you mean something different, could you please clarify what you mean? I don't know Pascal, OCaml, or Scheme, so I can't really relate to those examples of languages that demonstrate what you mean.

I understand there are other paradigms such as logic programming and functional programming etc, but IME such things are really good at only a very very narrow set of problems (and are very bad at most others) while OO and procedural are good at a few things and at least mediocre for everything else.

A good example of OO being helpful is a common little structure often called a Vector that holds an n-dimensional(maybe variable, probably fixed at 3 for ex) coordinate. There are many operations a vector can participate in, such as addition/subtraction with a scalar or vector, multiplication by a scalar, the cross or dot product operations, etc. I'm not sure of any way to implement a vector besides simple OO that doesn't invovle writing the same code hundreds of times. The fact that the coodinate components are often exposed does mean it isn't encapsulated as well as it could be, but in this case encapsulation of that level is rarely needed.

This kind of OOP is used in just about every programming paradigm for all 'complex' data types.

Perhaps I do not understand what OO is..?
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Basiror
Basiror
the concept of oop is to hide the work you do with a object inside memberfunctions of this object

of course this get and set functions are a bit dump and nobody will use this if hes programming a game engine

but take a look

you write a little demo engine and what to implement a console although you haven t implemented fonts yet so what are you going to do now?

you code it in oop manner in a console application and just insert it into your engine once you are ready to


sometimes you simply don t feel like going on with what you have started a few days ago you are sick of coding the renderer, well do something else and due to reuseablility you simple reuse the code of what you have written and spare a lot of time in the long term

in other words don t reinvent the wheel everytime you start a new project

http://www.8ung.at/basiror/theironcross.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.