C vs C++

Not about OpenMW? Just about Morrowind in general? Have some random babble? Kindly direct it here.
Post Reply
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

C vs C++

Post by raevol »

I remember a while ago we had a pretty lively discussion on here about why this project is a "C++" project and not a "C/C++" project. At the time all I really knew about C was that it is a "subset" of C++, so I did not understand why it would be incorrect to refer to this project as being programmed in "C/C++".

Boy have I learned a lot since then. I've been through a C++ course, a data structures course taught in Java which highly focused on OOP, an Assembly course, and recently I've been doing quite a bit of C programming.

The C program I wrote recently needed to be optimized as much as possible for speed. I'm pretty proud of it and will probably share it here after I turn it in. But through my programming, due to the optimization and the constraints of the project, my C code evolved into something that, to me, was very apparently glorified assembly code. I was treating memory as a malleable resource, instead of an abstract one. Now, if I was writing a program that would need to be a more general solution and that would need to be maintained in the future by other people, I certainly wouldn't write like this even in C, but the difference between the C and the C++ approach became very clear to me. C was the right solution for my tiny project that needed to run as fast and tight as possible.

C would never work for a project as large as this one, in my view. I'm sure there's amazing C programmers out there who could do it without breaking a sweat, but C++ is obviously the right solution when you need to handle memory in an abstract way, and to enforce permissions and object-orientedness in your code. Without incredibly skilled C programmers, a project of this size would devolve into an unstable mess very quickly if it was written with the C approach.

Anyway, this is kind of a rant, but I wanted to share. My schooling is making me fall in love with C++ and now C more and more! I'm curious to hear what our devs here who are more experienced than me think about my personal revelations.
User avatar
psi29a
Posts: 5355
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: C vs C++

Post by psi29a »

Have a look at Nethack: https://en.wikipedia.org/wiki/NetHack
Written in C :)

Then there are the GUI's that people put on top like Vulture's Eye.

Image
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: C vs C++

Post by raevol »

I mean, Nethack is certainly not as complex as this project...
tarsis
Posts: 9
Joined: 27 Oct 2015, 21:10

Re: C vs C++

Post by tarsis »

an example for a complex c project is the linux kernel. so you don't need c++ for big projects.

paul graham sums up this topic quite nicely:
Object-oriented programming is popular in big companies, because it suits the way they write software. At big companies, software tends to be written by large (and frequently changing) teams of mediocre programmers. Object-oriented programming imposes a discipline on these programmers that prevents any one of them from doing too much damage. The price is that the resulting code is bloated with protocols and full of duplication. This is not too high a price for big companies, because their software is probably going to be bloated and full of duplication anyway.
my views on c++ are unprintable (you find some funny and insightful quotes about c++ here). one reason people like c++ is because people like to overcomplicate things.

if you are a beginner read the suckless philosophy. an interesting read is of course the classical oop oversold. http://harmful.cat-v.org/software/ is a treasure box too.

btw.: for my day-to-day work i use go, which has a different approach to oop.
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: C vs C++

Post by raevol »

So... if you do OOP correctly, it will reduce your code duplication. It will certainly add some overhead, but that overhead will only be "bloat" if you're not using the language features correctly.

I'm certainly not an expert, but 99% of the arguments I see against OOP programming are directed at OOP done wrong. If you do it right, it's very helpful, there's a reason why it exists.
raven
Posts: 66
Joined: 26 May 2016, 09:54

Re: C vs C++

Post by raven »

Sadly OOP is abused more often then not, and people end up with beautifully complex abstractions for no apparent reason. Following the motto "If all you have is a hammer..."
User avatar
psi29a
Posts: 5355
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: C vs C++

Post by psi29a »

raven wrote:Sadly OOP is abused more often then not, and people end up with beautifully complex abstractions for no apparent reason. Following the motto "If all you have is a hammer..."
Oh god, this this this... look at Java.

C++ encourages OOP but doesn't enforce it.

The right tool for the right job. C and C++ have their places.
Naugrim
Posts: 172
Joined: 08 Jan 2016, 01:32
Location: Spain

Re: C vs C++

Post by Naugrim »

psi29a wrote: Oh god, this this this... look at Java.
I guess, that explains why everything extends Cube in Minecraft.
User avatar
sirherrbatka
Posts: 2159
Joined: 07 Aug 2011, 17:21

Re: C vs C++

Post by sirherrbatka »

To be fair, both C and C++ are quite horrible languages. The fact that systems written in those languages work at all is a testiment to effort and skill of programmers that build those systems.

As for java…

… just no.
User avatar
afritz1
Posts: 47
Joined: 05 Sep 2016, 01:18
Contact:

Re: C vs C++

Post by afritz1 »

I started my Arena re-implementation from scratch in C++11. I never have to worry about memory management at all because of std::unique_ptr, which is a big deal for a language biased towards systems programming. Restricting yourself to the "modern" subset of C++ is very worthwhile if you can do it. It eliminates an entire class of bugs. I also frequently use bounds-checking (i.e., std::vector::at) because I'd rather have an out-of-range exception than a segmentation fault/access violation.

As Jonathan Blow (creator of Braid and The Witness) once said, he rarely needs complex data structures and class hierarchies for his game programming, contrary to how much focus is put on it in academics. In other words, don't create abstractions where they aren't needed. I find myself returning to this idea fairly often as I write code.
Post Reply