iDevGames Forums
Why C++ … - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Why C++ … (/thread-3778.html)

Pages: 1 2 3

Why C++ … - DoG - Oct 27, 2006 04:13 AM

I just came across this quite insightful article while browsing through old bookmarks:

Why C++ … - OneSadCookie - Oct 27, 2006 04:30 AM

Most of it is fairly uncontroversial, but I don't buy his argument that C++ is better than C for the core engine functionality. Yes, you end up writing virtual function tables in C, yes you end up re-implementing generic data structures, and yes, you need a good grasp of how to use the C preprocessor. I don't think that those outweigh the downsides of using C++, though.

He's already said that if you value productivity, you use a productive language, which C++ is not. Why, then, turn around and say that you should use C++ at this low level because it's marginally more productive than C?

He also says that the STL is almost always faster than code the average programmer will write by hand. This may be true, but isn't really the point -- if the STL is your bottleneck, you still need a talented programmer to write the special-case code to solve the performance problem. What's more, they've got to fight with trying to understand *why* the STL is the performance problem before they can begin... at least C code is easy to analyze!

And if you're having problems with programmers who don't know about asymptotic complexity, or cache coherency, or whatever, in a professional environment, it's time to hire some new programmers. Those ones'll probably be fine programming Java or .NET for some big corporate, and probably earning more than you and putting in fewer work hours -- don't feel bad about letting them go Rasp . You want the programmers who know how to program at all levels, from the machine up to the mathematics. Fortunately, many of them want to program games Wink

Why C++ … - icklefrelp - Oct 27, 2006 07:32 AM

Back in the murky depths of time, when I started in the games industry (mid 1996) it was very different. I was a die hard 68000 assembly programmer although C was my high level language of choice, Almost everyone coded in straight C, apart from the times when we needed to go really low level, when we'd do MIPS assembly (PS1). C++ was ok for tools, but noone used it for game code.

Since then I always considered myself a die hard C coder, I hated C++ (probably for no real reason other than I considered myself a die hard C coder.) Over the years I have been dragged kicking and screaming into the C++ camp, in that I will rarely write straight C code anymore even in my own projects at home.

However in many ways I long to go back to writing in C and ditching the things I dislike about C++, but use them because they are convienient, rather than efficient (operator overloading being something that comes to mind). But I have such an investment in C++ now (in terms of a body of code), that it's an awful lot of work moving away from C++. On many occasions I have started a project saying C only or perhaps Objective-C where I can use C for the bits that really do need to be fast, but so far I have always gone back to my C++ codebase.

I can't see the situation changing, too many people like the things that C++ gives them, that makes their jobs easier. Even though I may be more productive now using C++, I know that I'm not as happy programming as I used to be when it was straight C and Assembly language.

Why C++ … - akb825 - Oct 27, 2006 10:50 AM

I do enjoy using C, and for the core of my current project, I'm doing everything in straight C, since it will have better portability. However, when I use the framework I'm making now, I'll be using C++. I just don't want to have to go through all the trouble of getting all the inheritance etc. to work in straight C.

For me, I think C++ is pretty much what I want in a language for complex projects. It's still somewhat low level, but provides simple and powerful tools for things like polymorphism and templates. I don't need, or even want, über high level language constructs. (hell, I've never even heard of closing) I like coding in C, but I want better object oriented tools for polymorphism, and C++ provides that. Things like templates turn out to be very useful because of the convenience and you end up getting the efficiency as if you created the class for each object. (though at the cost of code size) Other things like operator overloading are gravy, IMO. They aren't really necessary, but can be very convenient, both for math and data structures. I think it's pretty cool that you can use any data structure like an array, assuming you have a way to look up an index. Or with an iterator, you could do something like
object = *itter++;
element = itter++->getWhatever();
Also, for things like data structures, it makes it very easy to have a compare that everybody can agree with. (ex: would you use compare, compareTo, compare_to etc.? Instead, you can just have operator >, operator <, and operator ==, or any 2 of those) It also makes it so you can use primitive types just as easily as objects.

Why C++ … - AnotherJake - Oct 27, 2006 12:00 PM

icklefrelp Wrote:On many occasions I have started a project saying C only or perhaps Objective-C where I can use C for the bits that really do need to be fast, but so far I have always gone back to my C++ codebase.
That's funny, once in a while I will start a project and say that `I am using C++ only this time', and I wind up going back to my C codebase. I think it has to do with the fact that my mind tends to think in terms of how I can solve problems using C most of the time. I have programmed so little C++ in the last few years that good techniques are all but forgotten.

That is a good little article, and I liked reading it until it seemed a little rambling near the end. It seems to me that there are good and bad coding styles in both C and C++. I have seen lots and lots of really poorly done C++, and that has helped turn me away from it. OTOH, I've seen some damn clean C++ that made lots of sense and has enticed me back to it as well.

I take issue with two of the points he was saying about C though, because they both have more to do with coding style than any deficiency of the language itself:

First, data structures being packed with function pointers is not accurate. All you will likely ever need is one pointer each for: init, update, render, destroy. And even those are not required. A better way to do it that I have learned is to create a dispatch table in my scene graph manager that simply issues callbacks based upon a simple type variable in the data structure instead of pointers, which saves more space -- the equivalent of three less pointers in each entity. It is a super simple and powerful technique that I prefer over function pointers stuffed in structs.

Second, macros generally suck. I hardly use them at all and have found that where there is a macro there is usually a better way to code the solution in the first place.

However, I entirely agree with the argument that he makes for C++ by essentially pointing out that even if you know for a fact that C is better, the industry disagrees with you and if you get into the big-leagues you will have no choice but to program in C++. That is entirely true. Indeed, even Carmack was forced to switch!

Why C++ … - OneSadCookie - Oct 27, 2006 03:26 PM

akb825 Wrote:I don't need, or even want, über high level language constructs. (hell, I've never even heard of closing)

Closures, not closing.

How do you know you don't want them, if you don't know what they are? Because believe me, you want them Wink

Why C++ … - akb825 - Oct 27, 2006 05:31 PM

After looking it up in Wikipedia, it looks like it's used with inner functions, which I have no desire of using. So there. Rasp

Why C++ … - Skorche - Oct 27, 2006 05:58 PM

akb825 Wrote:After looking it up in Wikipedia, it looks like it's used with inner functions, which I have no desire of using. So there. Rasp

Not really. They are more like code blocks that have a persistent scope and can be passed around as values. They are often the killer feature of languages that support them. Way, way more useful than inner functions.

Why C++ … - OneSadCookie - Oct 27, 2006 06:01 PM

If you've ever used an anonymous inner class in Java, an anonymous delegate in C#, a block in Ruby, a lambda function in Python, or many other similar features in many other languages, you're using a closure (or perhaps a slightly more restrictive cousin).

And if you've used those languages and haven't used those features, you weren't using the language right... and if you haven't used those languages, you owe yourself an education Rasp Wink

It's the one thing I miss most moving back to C, C++ or ObjC from just about any other language.

Why C++ … - akb825 - Oct 27, 2006 07:58 PM

I've used inner classes with Java, so I think I know what you're talking about. I will admit that I've not had that much experience with different languages: I've used C, C++, ObjectiveC, and Java. (the latter being limited to school, since I very much dislike it; I liken it to C++ without the C) In later classes I will use other languages, however. I would say, though, that my coding style seems to fit very well into the C and C++ paradigm. (where I use the one that makes the most sense for what I'm doing; I limit my ObjectiveC use to things I must use with a Cocoa interface) My preferences may change, but I certainly don't feel "trapped" when using those languages.

Why C++ … - Skorche - Oct 27, 2006 09:31 PM

Ah, anonymous inner functions. How to count the ways I hate thee. The syntax is so utterly gross.

On the other hand, you could take the advice that the software design professor at my school teaches and never ever use them. They of course are the spawn of the devil (along with their evil cousins, the inner classes) because they promote bad OO design. (or some nonsense like that) Clearly, if you were a diligent Java Swing programmer, you would write a million classes with a one line action method. Rolleyes

Why C++ … - Steven - Oct 28, 2006 01:27 AM

At Berkeley in CS61A we use Scheme. Being a LISP-esque language, we have lambda functions. Before I started using them, I never would have though I would need anonymous functions. But now that I've seen the power of higher-order functions like map and accumulate, I've started to realize just how amazing they are...

Learn Scheme! You'll understand programming so much better... and you'll realize that C++ really sucks :-p

Why C++ … - akb825 - Oct 28, 2006 02:42 AM

Can you give me a real-world example of how they would be used/what's really great about them? (just out of curiosity, since I really haven't heard much about lambda calculus)

Why C++ … - DoG - Oct 28, 2006 03:20 AM

I think the problem with closures and other concepts in higher-level languages is that they are sort of taught as an addon to OO, and that is really not true. People tend to start with Java, C, C++, and Objective C, maybe some flavour of BASIC, and have in mind only the object metaphor. And this is where bad coding style comes from.

Closures, lambda functions, etc are actually completely separate concepts that are compatible with basic OO. In the toolbox metaphor, if the basic OO concepts are the screwdrivers, hammers, and wrenches, then closures and friends are the power tools Smile

Note how the article says that it's not a good idea to stick to pure C++, but rather to use it in combination with some more dynamic and powerful scripting language.

Why C++ … - OneSadCookie - Oct 28, 2006 04:05 AM

I'm not sure anyone was advocating adding closures to C++, rather trying to explain to akb825 why C++ isn't the be all and end all of computer languages Smile