News for hackers. Stuff that matters.
Re:Zed Shaw on C++ by tptacek on Thursday July 15, 2010 @11:40PM (#1520166)
I agree totally with Zed Shaw on this, but some quick observations:

* C++ circa 2000 (before mainline g++ could handle Alexandrescuisms) is significantly different from C++ circa 2010, albeit in ways that probably upset Shaw even more (the more central role boost has taken, the more "expressive" templates have gotten, don't call me on any of this stuff).

* C++ std::string is an abomination, but you can always just do what I've done and what lots of other C++ programmers whose code I've read have done: just use char* and a simple string library (or a custom string class).

* Ditto for stream IO, which is a huge C++ misfeature but which is also pretty much irrelevant (I know of no part of the standard C++ library outside of things that explicitly support stream I/O that rely on you using it).

* I don't get the POSIX argument at all; just call the POSIX functions, they work fine. Nobody mandates that you use (say) ACE_wrappers to do systems programming.

* const-correctness may be another misfeature (I know I make fun of it), but the point isn't hard to see: if you take the time to const-correct your code, the compiler will spit out errors that would have otherwise been runtime faults.

Re:Zed Shaw on C++ by phaedrus on Friday July 16, 2010 @12:00AM (#1520225)
The problem with std::string is that when I type a quoted string in my code, it isn't natively a std::string, it's natively a char* and gets converted to strings. C++'s biggest mistake was not standardizing early on an official object-oriented string datatype. It should have been in the first version of C++, it should have been treated like a primitive type (even if it isn't), and they should have said "thou shalt use this type for your strings". It makes me sick every time I see some goofy custom string class.

One problem with const-correctness is C++ uses "const&" to mean two different things: it could mean either a reference to a persistent object you're not allowed to change, or it could refer to a temporary object which you're prevented from changing because the changes will disappear. The problem is it conflated access control with lifetime. It would have been better if there were a "temp" keyword - although the last thing C++ needs is more combinatorics of type decorators.

On the subject of consts, though, C++ simply enforces const correctness more, but doesn't the same criticism of confusing const apply to pointers in C as well? You can just as easily declare a "const * const" in C as in C++. So I fail to see why Zed Shaw writing something in C gets him away from that "problem", unless he doesn't write const correct C.

Re:Zed Shaw on C++ by barrkel on Friday July 16, 2010 @04:27AM (#1520647)
That would have required a decision, and if there's something committees are uniquely unqualified to make, it's decisions.

The fact that it's C++, where the prototypical user believes that they shouldn't pay for what they don't use just makes things worse. Everything being optional means there can be very few synergies in language or standard library design, because if feature A needs feature B to work, people who want A but not B will complain. C++ "chooses" either by omitting both A and B, or creating some low-level error-prone features and hoping users won't chop off too many fingers trying to build their own incompatible versions of A and B. An ill-designed misfit language is all but required by the dynamics of the situation.

Re:Zed Shaw on C++ by stoney on Friday July 16, 2010 @12:27AM (#1520281)
The great thing about const is that if you don't like it you can pretty much ignore it. The only time it might be necessary to deal with it is if you are using a library that returns const objects.

If you do like const (which I do) then you can put it in pretty much everywhere, except if you are dealing with a library that isn't "const correct", but then a bit of type casting will save the day.

That's a big advantage of lots of the C++ features - if you don't like them you can often just ignore them.

Re:Zed Shaw on C++ by jimbokun on Friday July 16, 2010 @12:04PM (#1521532)
"If you do like const (which I do) then you can put it in pretty much everywhere"

In retrospect, it might have been better to have a keyword for things that can change. This goes equally for Java and "final". Making immutability the default and mutability opt-in makes for fewer bugs and more reliable programs.

Arguably, this is what Clojure does where you have functional, immutable constructs by default with ways to declare things that can change (refs, etc.).

Re:Zed Shaw on C++ by ramchip on Saturday July 17, 2010 @08:44AM (#1523689)
const by default may not be a bad idea, but immutability of structures/objects in C++ sounds like it would be a huge performance hit to me. I've used an image processing library (CIMG) which lets you do things like `img = img1 * 2 + img2 * 0.5`, instead of editing the image in-place. It's pretty, but with regular 800x600 images the performance was around 2 orders of magnitude below a simple for loop... it made a lot of extra copying and allocation instead of mutating the image.

The thing is, this kind of code isn't an exception in the C++ world. C++ is made for problems where performance matters: image processing, compilers, browsers... immutability works for Clojure, but Clojure is a lot slower than C++. It's more of an "enterprise software" language.

Re:Zed Shaw on C++ by billswift on Friday July 16, 2010 @12:42AM (#1520305)
That's usually considered a drawback of C++; it's a huge, complicated language and everybody tends to use a different subset for their own work.
Re:Zed Shaw on C++ by ethaneade on Friday July 16, 2010 @02:34AM (#1520475)
The same can be said of English. And in both cases, there are sundry tasks of limited scope where the language is probably not the most natural choice (e.g. operas). However, when one is simultaneously dealing with multiple, disparate domains/constraints simultaneously (e.g. real-time computer vision), such a language might be the least unattractive option thanks to its inclusiveness.
Re:Zed Shaw on C++ by jbooth on Friday July 16, 2010 @02:05PM (#1521922)
Except program don't express natural language, and in fact the required context awareness of natural language is exactly what kills many languages and frameworks. I shouldn't have to know all of these things to edit some stranger's code, it should be obvious how it works.
Re:Zed Shaw on C++ by coliveira on Friday July 16, 2010 @10:10AM (#1521187)
That's not really true. If you need to work with libraries that use const everywhere you have only two options:

* Use const everywhere

* Use casts everywhere

The same thing happens if you use const and your libraries don't...

Re:Zed Shaw on C++ by ethaneade on Friday July 16, 2010 @01:56AM (#1520416)
Regarding const references for temporaries, the advent of rvalue references allows separation of access control and lifetime: http://www.artima.com/cppsource/rvalue.html
Re:Zed Shaw on C++ by emerson_clarke on Sunday July 18, 2010 @12:45PM (#1526094)
If your currently in an apoplectic fit as to why people don't love C++, const correctness, boost, and the standard library, then read no further.

For those with a slightly broader perspective and an inquisitive mind, perhaps you should take a look at at the Reason C++ framwork, specially if you have never seen C++ in its most pure uncomplicated OO form.

http://reasoning.info

Reason is a C++ framework that ive been writing for about 8 years, which aleviates many of the sources of pain highlighted in this discussion. Writing code with Reason is much more like using a dynamic language, with a full library like Java or .Net.

It supports raii, but doesnt use exception handling and doesnt bother with const nonsense or over the top inheritance restrictions. In fact pretty much everything in Reason is public and designed to be derived, modified or enhanced.

It has generic programming features, but doesnt force you to write everything as templates (a classic mistake that the standard library makes). For example, you can have an iterator of int's without caring what the underlying container type is, so your code and algorithms can actually be generic, not just infectious templates.

Unlike a lot of C++ frameworks and libraries, its not specifically focused on networking, or a few esoteric template classes or collection libraries.

Reason is a fully featured systems programming framework with a complete interface to all the usual posix api's and everything else you would expect from a modern language. It has strings, regexes, streams, parsers, sockets, threads, filesystem, encryption, encoding, xml, xpath, collections, time and date, sql (mysql, postgress, sqlite), http (client/server), smart pointers, formatting, logging, and much more.

But the features are perhaps not as important as how it is written. It is simple, and very object oriented, everything is designed to work togeather cleanly and obey the principle of least surprise. Just like Python and Ruby, Reason allows you to do a lot in a very small amount of code.

So if your wondering how you can have the simplicity of C, with just the good parts of C++, heres your answer.

http://reasoning.info

Re:Zed Shaw on C++ by joe_the_user on Friday July 16, 2010 @12:05AM (#1520235)
I like the QT library. It is an alternative to all this absurdity. QT's strings, streams and containers have infinitely better syntax than the bondage and discipline of STL. And QT may not be standard but it's going to be more widely used and supported than any string class I might roll myself.
Re:Zed Shaw on C++ by coliveira on Friday July 16, 2010 @10:04AM (#1521166)
> const-correctness may be another misfeature (I know I make fun of it), but the point isn't hard to see: if you take the time to const-correct your code, the compiler will spit out errors that would have otherwise been runtime faults.

I think it is so interesting the mentality of static typing people. I would much prefer handling the few resulting run time faults than having to stuff with my code with const.

Anyway, there is no guarantee in C++ that something that compiles with all consts in the world will not throw a segfault at run time in the least expected moment...

Re:Zed Shaw on C++ by amalcon on Friday July 16, 2010 @12:02PM (#1521525)
I think it's interesting the mentality of dynamic typing die-hards. I find it much easier (for complex projects) to just type "const" where appropriate and let the compiler think about mutability for me, than to think about mutability myself. The mental overhead is definitely superlinear with the complexity of the project, and typing "const" a few times lets me use that resource for more subtle correctness issues.

Of course, one problem with C++ (and, to a lesser extent, C) is that many of those subtle correctness issues are direct results of the type system in the first place. Go figure.

Re:Zed Shaw on C++ by tptacek on Friday July 16, 2010 @10:20AM (#1521206)
No compiler error ever ruined a NASA mission.
Re:Zed Shaw on C++ by edanm on Friday July 16, 2010 @03:36PM (#1522260)
Funny, but imo misses the point.

The beauty of dynamic typing is that it makes the writing of the code easier, which makes development shorter, which gives you more time to thoroughly test your code. After years leading a QA team, I'm convinced that more time testing is always superior to more time writing code, even if the code you're writing is there to stop bugs!

Re:Zed Shaw on C++ by jemfinch on Saturday July 17, 2010 @11:25AM (#1523916)
The beauty of static typing is that it consists of a proof that entire classes of errors cannot occur when your program runs. Like Dijkstra said, tests can only prove the presence of bugs, never their absence. Static typing proves their absence and is thus extremely useful in writing highly reliable software.
Re:Zed Shaw on C++ by alanh on Thursday July 15, 2010 @11:50PM (#1520194)
Alexandrescuisms has 2 hits on google including this page. To what are you referring?
Re:Zed Shaw on C++ by wglb on Friday July 16, 2010 @07:41AM (#1520850)
His book "Modern C++ design" is about the fascinating beautiful and downright frightening things that you can do with C++ templates. It is worth a read.

However, to support what Zed is saying, Scott Myers (of Effective C++) was astonished about some of the things done there.

C++ doesn't follow the principle of Least Astonishment in any way that I can think of. And that is not good.

Re:Zed Shaw on C++ by lanstein on Thursday July 15, 2010 @11:52PM (#1520203)
Re:Zed Shaw on C++ by tptacek on Friday July 16, 2010 @12:42AM (#1520307)
Wow, had no idea he was at Facebook now.

Loki is neat, even if it does represent everything that is evil about C++.

Re:Zed Shaw on C++ by steveklabnik on Friday July 16, 2010 @02:23PM (#1521985)
He's also writing (I think it's almost done?) "The D Programming Language", which is pretty highly anticipated.
Re:Zed Shaw on C++ by incomputable on Thursday July 22, 2010 @04:45PM (#1539499)
Re:Zed Shaw on C++ by starkfist on Thursday July 15, 2010 @11:51PM (#1520200)