C vs C++, Objected Oriented Programming vs Data Oriented Programming

Started by
13 comments, last by Hodgman 6 years, 1 month ago

As someone new and planning on getting into Graphics Programming and possibly Game Development, the following topics/paths always leave me wondering as to which route I should take. Now, I know some of these subjects are a little controversial and might create much debate, but still, I would love to hear about your personal view and experience in the industry and hopefully get a better perspective.

As someone who is planning on getting into Graphics Programming or Game Development, would it be better to :

1. C or C++

2. Object Oriented Programming or Data Oriented Programming

After researching around on the internet, there seems to be two schools of thoughts. One that prefer to use C with perhaps a few features from C++ such as operator overloading, etc. (Casey Muratori, Mike Acton....) Who value performance and the Data Oriented approach and see Object Oriented as an unnecessary evil.  And, those who use C++ and Object Oriented Programming (responsibly).

Now, as a relatively new programmer with only python experience, I'm really having a hard time deciding which road to take; especially when it comes to the initial decision of deciding which language to learn.

Both sides seem to make a good case, yet the undeniable fact is that majority of the people do seem to use C++ (but perhaps someone in the industry could shed some light on this subject), while some use a Cish C++, and relatively few using only pure C. Even John Carmack -- Quake's developer who used to code in pure C have recently decided to use C++ for their new and recent installment of Quake 3.

After years in the industry, what would you recommend to newcomers who are interested in Graphics Programming, making their own engine, or Game Development in terms of C, C++, OOP, DOD?

Thanks in advance :)

 

Advertisement

In terms of getting into the traditional industry, you need to be comfortable with object-oriented programming in C++. There's no serious debate there. The major engines are object-oriented, most in-house engines are object-oriented. There's a whole continuum of just how much inheritance you might see, but on the whole you are not likely to run into C-style data-first engines.

This does not mean that you have to make a binary choice. You will need to be comfortable with object oriented programming but a good knowledge of data-oriented programming can help you if you want to optimise very low-level things, or if you end up working for a company that favours that sort of approach.

A lot of data-oriented design is typically based around the reductionist view that programs are essentially just processes that transform data, and therefore you should just structure the functionality around the data, both to better represent the process and to help the computer perform this process more quickly. This view holds very true for certain low-level operations that operate on batches of similar objects, such as rendering or physics, but is barely workable for many high-level operations, such as AI or game logic. An effective engine will therefore often feature a bit of both; heavily data-oriented code at the low level for maximum performance, and heavily object-oriented code at the high level for maximum expressiveness. There's no need to throw out object-orientation entirely just because it may not be best route to top performance for certain tasks.

 

5 minutes ago, uptick said:

recently decided to use C++ for their new and recent installment of Quake 3

That was 18 years ago. Probably 20 years if you are counting when development started! Ignore this particular part of your research.

These questions feel like asking whether one should use a hammer or a screwdriver.  Really each solves a different problem.

As far as OOP vs DOP... I'm gonna say something a bit controversial here, but I feel C++ is actually more of a DOP than an OOP language.  Now hold off on the flames, I do have good reasons.  Sure you can create objects in C++, and you have inheritance... to a degree.  But most of what people consider OOP, is not really OOP (classes, encapsulation, RIAA, are all awesome tools, but are not really OOP).  C++ lacks things like virtual constructors, multi-dispatch, and a slew of other minor things that are really useful for OOP, but you don't really know is missing until you've stepped away from C++ OOP.  Now I'm not saying that C++ isn't OOP (there are a myriad of OOP definitions, and you can do OOP in assembly if you like...) just that C++ OOP can be awkward in some situations (like trying to force a square peg into a round hole).

In fact, modern C++ has stepped away from OOP in favor of meta-programming/templates.  Where-as most of the classes in a C++ program are just glorified structs with a few member functions attached (no inheritance, no virtual dispatch, etc...), you will find extensive use of template/meta programming in nearly every modern C++ program.  From containers to iterators, and all the algorithms that use them, these are all DOP in function if not form.  Despite their terrible syntax, they are an amazing tool, and IMHO are what separates C++ from many of the other languages out there (or at least at one time, many languages now have or are getting generics or something similar, which I think is a testament to how effect/useful they are).  I really do think that Stepanov was a genius when he brought DOP into C++ with the STL.

But in the end, you should learn and use both techniques, OOP and DOP.  Then when you have a problem to be solved you can be confident in your choice of algorithms, patterns, and design decisions.

Depending on the face of project, I see myself more often write C-Style global functions than diving into the OOP (if correct or not may be at any onse choice) syntax. But on the other side OOP(ish) style code makes life a little easier over plain C code. Classes as data containers with extra functions are in some way better to handle than the good old plain data with argument overloaded API calls have been for there encapsulation of proeprties.

I think it is the possibility of both, C_Style mixed with OOP(ish) code that makes C++ (dont forgetting about the power of templates and preprocessor macros)

You can do OOP with C (check out gobject as an (over engineered) approach).

Likewise you can do procedural development with C++ but still benefit from safety and convenience (i.e smart pointers, STL / vectors)

Most of my middleware work is in C (eases binding to other languages and the hardware) but I would classify more than 90% of my code to be Object Oriented in design.

i.e C:


struct Player *p = PlayerCreate();
PlayerDamage(p, 13);
PlayerDestroy(p);

vs (non-exception safe) C++


Player* p = new Player();
p->damage(13);
delete p;

As for which one is better... people generally agree that breaking things up into objects makes a project easier to navigate because you have a common design that you can make assumptions upon.

http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

Both.

It's trendy for bad programmers who think that OO=inheritance to post unfortunately-influential rants against OO these days... Don't listen to them and don't throw the baby out with the bathwater. 

Look up SOLID - this is the modern core of OO. Those rules, along with encapsulation and invariant-enforcement let you write large scale projects that are performant but more importantly, are easy to maintain into the future.

DOD and OOP can be good friends. OO doesn't really care about data layouts, but OOP languages can default to some bad layouts. C++ is flexible enough to let you apply DOD and come up with better layouts while still getting the maintainability benefits of OO.

OO can also encourage bad data access patterns (thinking per object instead of thinking per batch of objects), which can be bad for performance on modern CPU's (where cycles are cheap by memory access is death). This is where you can use a hybrid of OOP and DOD. Often this manifests as creating "systems" (e.g. "particle system" class, but no "particle" class) and representing objects by some form of opaque handle. C++'s flexibility again gives endless options here. Sometimes you still have individual objects as classes as well as a "system", but move certain methods to the system from the object to force users into better data access patterns. Other times you still have traditional objects, but you just use DOD thinking to arrange them into better layouts, or restructure the algorithms that operate on them to make better use of the real hardware.

C++ vs C is interesting because you can use 99% of C syntax from C++ anyway, and also no project ever uses all of C++ (almost every project coding guidelines / style guide will specify the acceptable subset of C++ that you're allowed to use). The big problem with C++ is a lack of a stable ABI -- this means that DLL's created with one compiler do not play nice with projects being created by another compiler... which means that C++ middleware is pretty much useless without source code access. If you're writing a closed-source middleware library, you should almost certainly write your public API in plain C, even if the internals are written in C++ :o

It can also be useful to enforce these kinds of constraints within the submodules of your own project, even if you don't need to. For example, see this blog post from The Machinery (ex devs of Stingray / Bitsquid engine and, ex Grin engine team). Keeping the interfaces between the different parts of your code simple can be extremely useful for ensuring your codebase remains maintainable, which is what OO is all about.

Ooh good timing for this thread - Albrecht just posted some new slides!

The original 2009 talk (still relevant!): http://harmful.cat-v.org/software/OO_programming/_pdf/Pitfalls_of_Object_Oriented_Programming_GCAP_09.pdf

And the 2017 version:

https://docs.google.com/presentation/d/1ST3mZgxmxqlpCFkdDhtgw116MQdCr2Fax2yjd8Az6zM/edit#slide=id.g2398a4e2af_0_118

https://docs.google.com/presentation/d/1305-i5JZ98cLqXgVwzTyB1NQUjjBJ0_0diwU2diS_TI/edit#slide=id.p

tl;dr: 3rgbCYF.png

Thanks so much for all the wonderful replies. I'm currently learning C++ and planning to read Effective C++ once I am done with the first book.

Are there any good books you guys would recommend? on algorithms, data structures, etc? or any other related subjects that might be helpful beyond the basics of C++ ? :)

9 minutes ago, uptick said:

Are there any good books you guys would recommend?

My own recommended book list for C++, many people and groups maintain their own lists. 

The original objectmentor PDFs about SOLID are now hosted here: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

Definitely read them.

Understanding SOLID is the beginning of a journey away from just being able to write code in C++ and towards being able to architect software and software components. Then to do so on a larger scale without even getting hung up on the specifics of any particular language.

SOLID is often touted as modern pillars of OO and the PDFs are certainly presented from an OO perspective. In actuality you will find that a number of the lessons there can and should be applied to other programming paradigms too!

This topic is closed to new replies.

Advertisement