9 ms·
Why do C++ folks make things so complicated?
- moonboots 15y agoTop C++ sounds complicated to me.
- lallysingh 15y agoIgnoring the Top C++ ideas, to answer the question of this article: C++ wants too much (high abstraction with high efficiency in the general domain) with too little to work with (a minimal type system, a minimal generics system). Classic work from a committee -- it's easy to agree to lofty ideals, but difficult to agree on how to get there. So, everything built atop this doubly-pronged-mistake ends up paying the price, in complexity.
- lurker17 15y agoWhat does a committee have to do with it? Most of C++ was there when Bjarne Stroustrup created the original versions himself.
- qdog 15y agoStroustrup apparently listened to a lot of people at Bell Labs while creating c++, essentially creating it by committee. I think there is a quote by someone about this, Rob Pike? Can't find it at the moment.
- fleitz 15y agoI prefer this quote: "Bjarne Stroustrup bolts everything he's ever heard of onto C to create C++" from http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html http://james-iry.blogspot.com/2009/05/brief-incomplete-and-m...
- dexen 15y agoCan't find the quote either, but there's a good take by Linus on cat-v.org: http://harmful.cat-v.org/software/c++/linus http://harmful.cat-v.org/software/c++/linus ...and by others, including PG: http://harmful.cat-v.org/software/c++/ http://harmful.cat-v.org/software/c++/
- nosefrog 15y agoAre you talking about the quote ten paragraphs down? http://harmful.cat-v.org/software/c++/coders-at-work http://harmful.cat-v.org/software/c++/coders-at-work
- qdog 15y agoAh yes, that was it, from Ken Thompson, thanks.
- ntkachov 15y agoI like to use the following flowchart when deciding if I'm going to be using C++. Do i need to do alot ----Yes-----> Java[script] of string parsing? | No | v C++
- CJefferson 15y agoThe biggest problem with C++ (in my opinion) is a lack of a good library manager. As installing C++ libraries is a pain, and testing them on a large range of compilers is a pain too, the only library of note anyone ever uses is boost. Most of Boost is a horrible templated mess. I consider myself fairly expert on C++, have contributed code to both clang and g++, and worked on C++11. I still don't use boost, and am slowly taking it out of projects which it has infected. Boost is interesting as seeing how the bounds of C++ can be pushed, but as a library for day-to-day use it is horrible. It is just about usable if you limit yourself to a few small pieces. For example at first glance boost::spirit is a really nice parser library. However the error messages give me shivers to this day and a parser I wrote using it takes over 1GB of memory and 40 seconds to compile.
- makmanalp 15y agoAren't horrible template errors the compiler's fault and not the language's (or the library's as you suggest)?
- Symmetry 15y agoThey are if its due to compiler laziness, but in C++'s case it seems to be the fact that its genuinely hard to compile.
- stonemetal 15y agoI would say the fault lands on all three. The language could have been designed such that good error messages where easier to output. The library authors could have made an attempt to force better error messages. Finally compilers could get their act together and output good error messages.
- rubashov 15y ago> Most of Boost is a horrible templated mess. Are you just saying you find the implementation distasteful? I don't find using the templated boost stuff problematic. Most of the time you just cargo cult examples from the documentation and all's good, but then you have great flexibility should you need it.
- jcfrei 15y agoI tend to think that applications developped in C++ are usually more complex and hence you get the false notion that the complexity stems from the language. While in fact you're just comparing apples to oranges.
- makecheck 15y agoI will go as far as to say (from experience) that releasing a library in C++ to others is completely foolish due to the major problems it can create when design changes are inevitably needed. Other languages do APIs better. C++ is useful for implementing the guts of things. And when implementing, having your own library of shared C++ code is useful, as long as you completely control it and always compile it with your project from scratch (a library of templates, for instance). The last thing you want is to link to someone else's compiled C++ library because that's where the pain begins. To expose a C++ implementation, use a plain C interface; the C application binary interface works very well across compilers. If you feel it absolutely necessary to expose something like the inheritance scheme of an object-oriented design, you had better do it in a language like Objective-C that can at least implement objects in a very forward-compatible way (or use tricks like exposing pointer parameters in C functions).
- joe_the_user 15y agoI'm using both QT and some gnome/gtk C libraries. Both work but the c libraries have a more inconsistent object model and require multi-stage processes for the construction of their c-objects. Once a c-based project is large enough that it needs objects in user space, not have a c++ interface is counter-productive and backporting to c++ project to c would doubly counter-productive here.
- asher 15y agoI agree. Also, the pure-C interface is easier to wrap from scripting languages.
- cbs 15y agoIf you haven't heard of it before, swig (swig.org) is pretty nice for generating code to bridge C and C++ code to a scripting language for you.
- cakoose 15y agoI think he's taking a widely-accepted idea -- that the C++ language is a complicated language -- and making the unfounded leap that most C++ code is just as complicated. For starters, the standard C++ string type is reference-counted, something that leans more toward convenience and simplicity than maximum performance. Well-designed C++ libraries that don't need to push the performance envelope are often pleasant to use. Maybe the author wants things to be even simpler, but he's gotta provide examples. Or at least describe what his ideal Top C++ would consist of. That's the hard part. Simply saying "we should make a simple version of C++ that still lets you do low-level stuff when you want" seems vacuous.
- SamReidHughes 15y ago> For starters, the standard C++ string type is reference-counted, The GNU implementation of it is reference counted. I don't think every implementation is.
- ramchip 15y agoIndeed, recent versions of VC++ don't use reference counting.
- frou_dh 15y agoThe endless comments on that post arguing about C-vs-C++-vs-C#-vs-ObjC make me want to jump in to a volcano. Those stories from the last few days about reducing time spent reading the internet might be on to something.
- mattgreenrocks 15y agoIt's all bikeshedding, and thus should fail HN's test of interesting content. Junior programmers obsess about this crap. I don't know why these discussions continually pop up. It really makes HN look like /r/programming, and that isn't saying much. Of particular note are comments in the vein of, "I haven't used Language X much, but it is terrible." I'm baffled why they're not called out for their obvious lack of intellectual honesty.
- jrockway 15y agoWhy do you have to use something to know it's terrible?
- mattgreenrocks 15y agoBecause, all too often, those comments are not prefixed with, "I haven't used Language X, but I hear that". Instead, they masquerade as comments by someone with actual experience. The poster knows that if they were to add that disclaimer to their statement, it would lower the credibility of what they're saying. And we all know you gotta be Right On The Internet, not intellectually honest.
- deleted 15y ago[deleted]
- radicalbyte 15y ago> Why do ____ folks make things so complicated. The answer is often "job security" or, in the case of consultants "to maximise billable hours".
- fab13n 15y agoHere's an idea: why don't we put distinctive marks at the boundaries between Top C++ and Bottom C++? Say, square brackets. If the Top C++ superficial syntax happens to be a little bit smalltalkish, we could call the resulting language Objective C! Kidding apart, one of the anti-patterns the most specific to C++ I've met is inability to decide whether some code is high level or low level. C should have been embedded in C++, with clear segregation marks, not extended/inflated into C++.
- dicroce 15y agoEarly in C++'s existence, C++ took a lot of crap from C programmers regarding performance. In those days, C programmers complained of the "overhead" of virtual function calls... and the "bloat" of objects.. As a result, the C++ community became super focused on performance... Most of the features in C++ are completely opt in (I.E, if virtual functions are too expensive, you are free to not use them and not pay any penalty)... This attitude bled into the STL... Another factor that complicates the STL is it's approach to generic programming... In my opinion C++ took the purists approach (instead of the pragmatic one)... So, std::list doesn't have a .sort() method... Instead, you can use the sort algorithm on any container that implements the right kind of iterators... IMHO, C++ needs 2 standard libraries... One for purists, and one for pragmatists... (and the pragmatists will use the STL to implement their standard library, but the pragmatist::list will definitely just have a .sort() method!)...
- kingkilr 15y agoActually, std::list does have a sort() method, std::vector doesn't though. std::vector makes you use the other algorithms one, since it's made up of contiguous memory, while std::list isn't.
- kstenerud 15y agoThus the following quote: "Whenever the C++ language designers had two competing ideas as to how they should solve some problem, they said, "OK, we'll do them both". So the language is too baroque for my taste." -- Donald E Knuth
- mkramlich 15y agoThe C++ standard library for true pragmatists is Python. :) (Not technically correct, but I was making a point.) I'm pragmatic. Or try to be. So in a world where Python and C++ exist I'm going to try very hard to not touch C++ ever again. Life's too short.
- seanp2k2 15y agoI don't know C++, but I know a few other langs and can figure stuff out quickly. I was reading through Chromium sources yesterday trying to track down the conditions that must be met for a specific error to be thrown. It was all callback spaghetti and I gave up after tracing calls back through 10+ files.
- abcd_f 15y agoThis has little to do with C++.
- deleted 15y ago[deleted]
- sbmassey 15y agoThere are some C++ libraries that are perhaps more complex than the equivalents in other languages, but tr1::regex's are pretty much the same as regexes in any other language, if you discount the static typing stuff.
- steve8918 15y agoI disagree that it has anything to do with the language. For example, I just recently started using Boost. Holy smokes, it is BEAUTIFUL, at least the parts that I've used. It just works. I've never worked with a library where I look at the example, make a guess as to how it should work, and it just works. On the other hand, the company I work at uses Boost in a completely illegible, unmaintainable way. It has nothing to do with Boost, it's whoever uses it. To say a language such as C++ is responsible for overly complicated interfaces is just plain dumb. It's not the language it's the implementer. I can take the same $200,000 violin as Joshua Bell and make horrendous sounds from it. It doesn't mean that the violin is a piece of crap, it just means that I have no idea how to play a violin.
- ZenPsycho 15y agothe title of the article is "why do C++ folks make things so complicated", not "Why does C++ make things so complicated"
- steve8918 15y agoThen the title should have been "Why do bad C++ programmers make things so complicated?" Good C++ programmers can make very complicated things extremely simple. For example, I'm working on a program where I was trying to do a mass comparison of file names between 2 directories. Essentially, I wanted to get the intersection between two sets of strings. I wondered if there was something that already did this, and after a quick google search, lo and behold, there's an STL algo that handles this, set_intersection. So in a single line of code, I was able to solve my program that would have taken me a lot more. It wasn't complicated at all, it was extremely simple.
- ZenPsycho 15y agoand now you have two problems?
- sausagefeet 15y agoI'm not sure what your anecdote is really meant to prove? Doing set intersection is fairly trivial to implement, and the C++ std library comes with an implementation. The argument is that this claim doesn't scale up, which your anecdote does not address.
- mmphosis 15y agoApple’s Mac OS X, Adobe Illustrator, Facebook, Google’s Chrome browser, the Apache MapReduce clustered data-processing architecture, Microsoft Windows 7 and Internet Explorer, Firefox, and MySQL — to name just a handful — are written in part or in their entirety with C++. The key phrases are "to name just a handful" and "written in part." It's very simple because most of this handful of a particular type of software are written in C, not C++.
- buff-a 15y agohttp://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-mapreduce-project/src/ http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-mapr... Hadoop is in C++? Those five files in the C++ directory?
- tptacek 15y agoNit: OS X is not written in C++. Small parts of it are, but most of xnu is C. C is simple. C++ is not. Having said that: TR1 regexes seem like an uncommonly simple C++ API.
- chj 15y agoit is called object hell. we want to get things done, but they give us a lot of objects.
- deleted 15y ago[deleted]
- agentultra 15y agoC++ code isn't inherently complicated. The people who write C++ code might make their programs unnecessarily complicated. I've read at least one program that was written that way. A web application written over 12 years ago without the aid of standard libraries or common sense. However, I've also read some really great code in C++. It seems to me to be very polarizing because it lets you do what you want without forcing any conventions on you, the programmer who knows what they're doing. I suspect the reason why people who make programs with C++ make those programs unnecessarily complex may have more to do with their psychological makeup than the limitations of the language (which I believe to be rather few).
- pnathan 15y agoIn my experience writing C++ as a hobbyist & minor professional work, it leads to complexity for a couple reasons. - Lack of higher order functions generates boilerplate classes for fairly simplistic closure-istic operations - Using templates with any sophistication infects gobblygooky templating syntax around your codebase. Since templates are actually a really nifty concept and worth using, this happens pretty quickly and now your codebase has more angle brackets than braces. ;) - Inability to add syntactic abstraction means everything is written out, all the time. I spent a lot of time learning C++98. After I got exposed to other languages, I don't think I'm going back to C++ without a good reason. There's too much ceremony for too little gain. Arguably with sufficient blood, sweat, and tears libraries can be written to make C++ do or fake what other languages do easily, e.g., regex, closures or continuations. I think that taking the dynamic language approach of C in the performance parts and dynamic language for the rest is a better way to architect your system. Or, select a dynamic language that compiles to native code. :-)
- Leynos 15y ago"Lack of higher order functions generates boilerplate classes for fairly simplistic closure-istic operations" Do you think this will change now that C++ has closures?
- pnathan 15y agoIMO, it depends on how 'progressive' the team is and how open they are to using the features.
- fauigerzigerk 15y agoInability to add syntactic abstraction means everything is written out, all the time. I disagree with that one. Of all popular languages, only Python and Ruby (maybe C#) come anywhere close to the level of syntactic abstraction that C++ supports.
- pnathan 15y agoWhen I say syntactic abstraction, I mean Lisp-style macros. Python's decorators do a weaker version of the same thing.
- yason 15y agoC++ is the engineer's wet dream vision of what C could be. C is the wise man's careful vision of what C should be.
- known 15y agoIt is complicated because a sophisticated C++ application is written by more than one programmer.
- shin_lao 15y agoC++ is an expert's language perfect for solving very difficult problems. Used properly it leads to an extremely elegant, efficient and agile code. Now there's not "one way" of doing things and anybody is doing it "it's own way". There's probably more than ten C++ dialect. When you're used to one dialect you tend to believe the other is "complicated". When you're not used to generic programming or templates you tend to think it's "over complicated" and "useless". And if ever you mix the dialects, you end up pretty quickly with something horrible.
- supar 15y agoI often hear these arguments about C++, but after having used C++ for almost my entire career (and looking forward to use C++11) let me tell you why people continue to use C++: because C++ is performant, and the alternatives don't give you enough control over the internals. Yes, C++ is incredibly complex. Inherently much more complex than, say, Haskell; due to the fact that C++ is far less consistent, has an hairy syntax, and non-uniform libraries (a problem which is also shared by perl and ruby, due to the language flexibility and rhetorical "best practices"). But C++ delivers! And while not better than Python, it still allows higher-level programming than C, while attaining the same level of performance. While you probably don't care that python 3 (a language I also love) is overall roughly 10% less efficient than the old 2.7, I need to figure out how to crunch a dataset which is doubling each month, while CPU power is not increasing in frequency any more. While your favourite language du-jour still needs to figure out how to do basic multithreading, I'm actually exploiting it since decades, including SSE and CUDA while I'm at it. Sure, the result is not pretty, but it seems today that thrashing cycles "in the cloud" is not an issue anymore. Thank god I don't work in that industry.
- zvrba 15y agoWhy do we make things so complicated? Because C++ is a low-level abomination pretending to be a high-level language. Let's start. - Allocators in the STL are a joke, so you can't use them for implementing different memory models. So you end up implementing, e.g., linked lists from scratch. - You can't "steal" underlying storage from a vector, which means you can't use it as a simple memory pool. So you end up implementing a vector from scratch. - size() of containers returns an unsigned type, which is a perpetual source of code ugliness and cursing in may day-to-day programming. (The most trivial example: make a backward loop through a vector. No, don't tell me to use iterators -- index has also semantical meaning in my use-cases.) - There is static_cast, reinterpret_cast, dynamic_cast, const_cast, but no cast which would actually check that what you're trying to do makes sense. (C and C++ standards stipulate that casting from A to B is well-defined A is a "prefix" of B. This could be extended to check for layout compatibility, so it would be legal to cast a linearly triply-derived struct with 3 floats to a float array with 3 elements. Technically, it's UB, but "it works in practice", and that "it works" could have been formalized. The compiler would be allowed to reject non-working cases.) - Writing flexible, generic (template) code is all nice in theory, but, in practice, it goes against the idea of separate compilation, with corresponding increased compilation times. - Debugging heavily templated code is a nightmare. - Writing templated code is arduous. Templates being turing-complete, we can never expect as good IDE support as for Java and C#. (I toyed with some simple Java programs in NetBeans and I almost cried when I had to back to my day-job of writing C++. IntelliSense of Visual Studio, like "state of art" IDE for C++, is CRAP compared to what you get from NetBeans/Eclipse for Java.) I like more and more the idea of using a "restricted" language augmented with a powerful IDE. - The idea of constructors establishing class invariants is all well and nice until you have to deserialize an object graph with cycles. (For this you must temporarily have an invalid object that is to be filled in later.) Constructors/destructors make sense if you actually have to manage resources. - etc, etc. In short, C++ has a history of incorporating half-baked features into the language and/or standard library. Things get complicated because it IS complicated to get a half-baked feature to do what you actually need to accomplish. What half-baked features? I've already mentioned allocators and casting. Then there's RTTI, whose implementation is complex, but I don't know whether any serious project uses it for something more than dynamic_cast. (In reality, you would like to have full reflection, but you have to implement it yourself. Yes, it's complicated.) When you try to implement reflection yourself, you will, almost invariably, use offsetof at some point, which, technically, yields UB in most cases, but also it WORKS in the most common cases (no MI, no virtual inheritance). There is auto, there is declspec, but there is no complete type-deduction, so you often have to spell out things twice to the compiler, even though it already has all information it needs. There is no structural polymorphism [this is related to lacking casts] -- you're supposed to write template code with corresponding bloat of object code and other, above-mentioned downsides of templates. iostreams are barely useful: when opening a stream fails, there is no standard-defined way of finding out WHY it failed (non-existent file, permission denied, etc.. This is a KEY feature for any program that interacts with a human user.) In C, IO functions set the errno variable, but not C++ streams. If you're lucky, errno will be set by the underlying system call and won't be changed further, but you can't count on that. Yet another fail (i.e., half-baked feature). Another iostreams misfeature: overloaded operators << and >> : the program code leads you to believe that things execute sequentially, where, in fact, the code is converted to function calls with unspecified evaluation order of arguments. So if f() has side-effects, you don't know what cout << f() << f() will output. == Maybe the new C++ standard fixes some of the above issues, but I'm so disappointed by the overviews of what's new, that I won't even bother checking. Many things are just cosmetic, some are outright stupid (e.g., user-defined literals), but fundamental problems of piled-up, half-baked features remain.