3 ms·
Why is C++ so complicated? My initial hypothesis was that the language was evolving to simplify how a someone can stitch together multiple libraries to create
by dilippkumar 8y ago
Why is C++ so complicated?
My initial hypothesis was that the language was evolving to simplify how a someone can stitch together multiple libraries to create an application. If the complexity can be reduced (by say, adding garbage collection), then the quality of programmers a software company would need to hire can drop from the Donald Knuths of the world to someone who is more likely to submit their resume at an average software firm.
However, at this point, it is safe to claim that the complexity of keeping up with C++ far exceeds the complexity of the original problems it was trying to solve.
Does anyone have a good argument for making C++ so convoluted?
- pjmlp 8y agoBackwards compatibility with C89 copy-paste semantics, which is only simple for those that never read ISO C documents, and the goal to design a powerfull expressive language, that allows any developer to create libraries, which in other programming languages require compiler and runtime support instead. Plus even other languages that appear simple, are only so for those that don't really know the complete language standard. Python is my favourite example, as it is demeed as simple language for begginers, yet the language + library reference PDFs are also a couple of thousand pages long, and there are so many PEPs that hardly anyone can pinpoint which is the minimum Python version to execute a random piece of sample code.
- gumby 8y agoC++ is a large toolbox. You don't have to use all of it, and probably shouldn't use all of it in one program. There are other approaches: e.g. js + npm, which can get you super rapidly to something functioning. It has different risk points than C++, and different benefits. If it works for your use case you'd likely be a fool to use C++, especially since its "standard" library is so much larger. But likewise there are complicated problems for which you'd be wrong to use node. Transportation runs from bicycles to mack trucks to tanks to spacecraft, each with different complexity and performance issues. What it means is that you can quickly write a powerful program or a team can build a large, maintainable system. Some people on that team might use some of the more abstruse tools to make stuff (specific macros (aka templates), other complex objects) that create common resources used across the team. You can choose a simpler language but then for some cases every user needs to repeat certain boilerplate and/or risks forgetting some important corner cases. You can let everyone free to use any arbitrary tool in the toolbox but then you equally end up with an unmaintainable mess. This really isn't any different from building, say, a power plant. Some people have gone to a lot of work to develop the steam schedules (regulatory code, such as schedule 60 piping) and you can buy them and have people who know what they are doing weld them up as you need them, but you don't have to design the safety code, simply be sure you're within its constraints.
- ncmncm 8y agoIt was Jonathan Shopiro (author or iostreams) who interrupted a comparison equating Language X with a seaplane, interjecting that by such a standard, C++ would be a boat anchor with feathers. It remains the funniest thing I have ever heard said about C++. It completely derailed what had been becoming a tedious conversation. I don't recall what Language X was.
- Koshkin 8y agoI have to disagree. From where I stand, complexity of C++ merely reflects that of practice of programming in general (modulo the language's historical origin and evolution). C++ remains a pragmatic language that helps solving practical problems. Memory management is one aspect, and the RAII paradigm and smart pointers are an excellent way to go about it; garbage collection libraries are also available.
- munchbunny 8y agoI don't think C++ is actually that complicated. However, I do think C++ tries to use way too many different permutations of the ()[]{}<> characters. Once you try to stuff too many of them into the same piece of code, you end up having to read code twice, once to figure out what the braces are for, again to figure out what the code is doing. As an example, the < and > characters may indicate comparison (< and > in boolean expressions), a template declaration (< and > with a comma separated list following a function/class name declaration), a template specialization, an operator overload, an object access via pointer (->), a bit shift, (<< or >>), and possibly writing to a stream (<<). A less egregious example, [] might be used to indicate indexing into an array, indexing into an object via overloaded [], declaring an array type, or declaring the captures in a lambda. While you can figure it out contextually, it's often irritating to have to figure out context first and then interpret the code.
- gumby 8y agoThey consciously try not to add many keywords or reserved characters. That has the downside but so does the alternative. I do agree that the stream operators << and >> was a bad experiment in retrospect.
- jokoon 8y agoBackward compatibility is a hard problem. Look at rust, or python, those are great languages, but obviously you need a new compiler and you need to write new code. With C++, you can just add new code to an existing code base, and share that code across projects. What is nice is that the language is evolving, but previous code still works. So the problem with C++, is adding new cool features, on an existing language, while not breaking existing code bases. That's how you improve a language, by keeping its users while adding new better ways of doing things. I honestly don't think rust or go will really survive over the decades. C++ already has momentum, and honestly, its close-to-C syntax is still a good thing because it's intuitive. How down-to-earth simple do you think go or rust are for students? How do you wrap your head around having to explain students the purpose of a garbage collector, not to mention how it will behave and how predictable it will be?
- KayEss 8y ago> If the complexity can be reduced (by say, adding garbage collection) Adding a garbage collector would make code more complex, not simpler, as it would break RAII. I spend a lot more time dealing with memory and resource allocation issues in GC languages than I do in C++. In languages like Python we have to use `with`, and this is harder to use in the client code, and the class is much harder to write, than anything in C++ where we just add a destructor to clean up. std::vector<char> data(10 << 20); std::ifstream("/dev/urandom", std::ios::binary).read(data.data(), data.size()); The above code can never leak (either memory or file handles) under any circumstances and the implementations of the types involved push zero overhead/boilerplate on me as a user of them. I think if you want to reduce complexity of C the place that everybody else is looking is at how to allow opting in and out of legacy support.