4 ms·
I actually very rarely create class hierarchies in C++. I do, however, define and implement interfaces and this is a simple, no-brainer technique that eliminate
by cageface 12y ago
I actually very rarely create class hierarchies in C++. I do, however, define and implement interfaces and this is a simple, no-brainer technique that eliminates a whole class of gotchas when I do it.
The total linecount isn't significantly increased by using C over C++. The codebase complexity isn't increased.
This I can't agree with at all. If I had to rewrite my current codebase in C it would be substantially larger and buggier and much less flexible. I know because I've tried this experiment already. This is even more the case with the C++11 syntax enhancements, which reduce the verbosity of C++ a lot.
My only real complaint with C++ is that it lacks a proper ABI so I have to wrap it in C if I want to use it from another language. This wasn't a big deal when I could rely on Obj-C++ but Apple's new Swift language has thrown a bit of a wrench in this scheme.
- sillysaurus3 12y agoThis is even more the case with the C++11 syntax enhancements, which reduce the verbosity of C++ a lot. Maybe true. I bailed from the C++ world before C++11 became pervasive. But if you use C++11 you risk leaving VS2010 users (and below) in a situation where they can't use your code at all: http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-language-features-in-vc10-the-table.aspx http://blogs.msdn.com/b/vcblog/archive/2010/04/06/c-0x-core-... And C++14? No idea what version of VS supports it. But using C++14 will definitely mess with people who have older compilers. And those people don't always have the luxury of just buying the latest version of VS. Who cares about Windows? Well, anyone who's into the open source movement should be sympathetic to people who are forced to use a certain operating system but still want to get into programming. The spark for people's desire to become programmers often starts by wanting to customize their environment. That leads them to shell scripting, which is a stepping off point into more advanced programming. The main point is simply that the C++ ecosystem has so many "gotchas" and timesinks that when forced to choose between C++ and C, an experienced programmer will save time with C, mainly because their years-old patterns will work regardless of what environment they're in. There are still some gotchas, of course; you can't use unix signals and expect them to be cross-platform. But it's far more straightforward than trying to get your codebase compiling on five different compilers. Of course, choosing Python or some higher level language is almost always the best cost/benefit tradeoff.
- cageface 12y agoC++ standards compliance is better now than its ever been. You can't have a nice modern language and expect to support a four-year old IDE too. C++14 is mostly a minor refinement of C++11. You get a much nicer language already in C++11 that you can use pretty freely on modern platforms. Sure, use the highest level language you can afford to use. But I can't agree that experienced programmers will in general choose C or be more productive in it. The counter-examples are almost too numerous to even know where to begin. Surely the people behind Photoshop, Chrome, Renderman and Cubase etc. are aware of C and made a conscious decision not to use it.
- sillysaurus3 12y agoIf you set your own standards based on what everyone else is doing, you'll never hit the high notes. When you're a single programmer, you shouldn't look at what large groups of people are doing. Large groups often have no idea what they're doing, too, so that mindset is unproductive in general. Many of the reasons are political, not technical: suggesting that everyone be forced to program in Python to the exclusion of C++ denies your company the expertise of all C++ programmers. For some industries, like Renderman's or Photoshop's, that would be suicide. There are many reasons Tarsnap is written in C and not C++. Colin was aware of C++, yet chose not to use it. If one of the smartest and most experienced programmers choose not to do something, it's worth understanding why. Perhaps they were avoiding pitfalls that you may be in danger of falling into. On the other hand, if Colin were forced to program in C++, I'm sure it would be wonderful C++. So the point of C vs C++ may be moot anyway. What matters is that a programmer is experienced and competent, not necessarily what tools they're forced to use.
- cageface 12y agoI choose C++ precisely because I am a single programmer. I can't afford to waste any time writing code that doesn't translate to a user feature. I write a lot less boilerplate and spend less time chasing bugs using C++ than I would in C. If you're happy coding in C then by all means continue. I'm just a little tired of all the C++ critics that insist that only naive or inexperienced programmers use it and they'd all be equally productive if they'd just man up and use C for a while. Down votes are not coming from me, by the way.
- Peaker 12y agoYou can easily create interface vtables in C. These vtable (ptrs) don't have to be put into the objects themselves, which is a historic mistake that got thrown into C++ and other OO languages. The vtables can be passed as arguments to any operation, making them closer in power to type-classes than OO interfaces (i.e: much more powerful).
- mtdewcmu 12y ago>>I actually very rarely create class hierarchies in C++. The book I learned C++ from must have weighed 5 pounds, and about 4 of them were devoted to inheritance. I assumed that inheritance was the central point of OOP. I never quite finished that C++ book; the details became overwhelmingly tedious, and I'd already learned to write programs. When the Gang of Four book came out, and in its opening pages declared that inheritance was a bad practice, and you should prefer composition wherever possible, I kind of laughed, thinking it had finally dawned on people that this was a bad idea, and OOP would go away soon. I could not have been more wrong about that. I still don't quite get how a person can be for OOP and profess a dislike for class inheritance; or believe software engineering metrics that consider things like call-tree depth and class counts indicators of excessive complexity and still believe objects are the right model. It seems like all the features of OOP are tied up with inheritance, and all of the recommended practices around classes and objects imply convoluted code paths and architectural complexity by their very nature. OOP seems to thrive on contradictions.
- SamReidHughes 12y agoDeep complicated inheritance hierarchies are usually misdesigned. OOP is just run-time polymorphism and you don't need anything besides abstract base classes for that. There is nothing contradictory about that GoF stance.
- mtdewcmu 12y agoDeep, complicated inheritance hierarchies loaded with virtual functions is among my least favorite kinds of code to have to debug.