3 ms·
Yes, some of C++ is indeed arcane. But what sets C++ apart from other mainstream languages like Java is that you can optimize for reading to a much greater exte
by fauigerzigerk 14y ago
Yes, some of C++ is indeed arcane. But what sets C++ apart from other mainstream languages like Java is that you can optimize for reading to a much greater extent than with other mainstream languages like Java or JavaScript. You can create entire DSLs all within the language. I'm not saying this is always a good idea, but if you want to radically pioritize readability, C++ is a better language than most.
Some of the boost libraries actually demonstrate this very well. If you look at the implementation of the library, you may be intimidated by its complexity (as you have shown with bind), but if you look at examples of its usage, you will often find it a lot cleaner than other languages.
Imagine what you would have to do in Java in order to do what bind does:
int diff(int a, int b) { return a - b; }
auto reverse_diff = bind(diff, _2, _1);
assert(diff(5, 2) == 3);
assert(reverse_diff(5, 2) == -3);
(Obviously, this particular example is trivial in any dynamic language, but that's a different debate)
I'm not saying C++ is the only or even the best language for creating statically typed DSLs. But it is a language in which you can optimize for readability in a much more radical fashion than in some other mainstream languages.
- cousin_it 14y agoIf C++ lets you optimize the readability of callers at the expense of callees, that doesn't mean you can optimize a whole system for readability, because it will contain both callers and callees. In Java it's common to have systems that are pretty uniformly readable.
- fauigerzigerk 14y agoI agree, C++ encourages a seperation between library writers and library users. That's not good. In Java, however, lots of things are done outside of the language to solve similar meta-programming issues. For instance, many of the new annotations are only available within a suitable container that interprets them at runtime. Some of these things even need byte code manipulation. The resulting system is not generally simpler than C++ in my opinion and you have the same separation between library/container/runtime makers and mere users.
- pmr_ 14y agoAnd now consider the confusion of your users when diff is picked up by ADL, but reverse_diff isn't. A lot of the code that tries to showcase new C++ features often falls short on some of the intricacies of the language or doesn't embed it into a larger scheme of a library. If my library offers customization via ADL across the board that kind of coding goes right out the window. I'm aware of the proposals to make function objects through ADL, but this is but one example where the complexity of C++ comes to the surface.
- fauigerzigerk 14y agoC++ is undoubtedly complex. I'm not a huge fan of advanced template programming either. But specifically about the ADL problem, I think that bind is used primarily at a call site to create a predicate. Something that can now be done with lambda expressions. That said, I don't like the fact that each and every feature of C++ comes with a really long list of weird special cases that are impossible to keep in your head.
- cmccabe 14y agoMore importantly, consider the hell that debugging that mass of templates, callbacks, overloaded functions, etc, will be. And you WILL have to debug it. Even if your code is perfect, one day someone will stomp on your memory and you'll be looking at this stuff in gdb, cursing the day you ever heard of Boost.