I constantly hear about a relatively popular idea that "C++ sucks" from some very influential people.
"OOP has lost a lot of shine as I have embraced more functional values. (Atomically created const objects are still good)" - John Carmack
"It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do nothing but keep the C++ programmers out, that in itself would be a huge reason to use C." - Linus Torvalds
"I read a book about Java, and found it an elegant further development from C. But I have never used it. I did write some code in Java once, but that was the island in Indonesia; the code was in C and lisp. By contrast, I find C++ quite ugly." - RMS
is RMS joking? C++ is ugly but he thinks Java is ... eugh, elegant?! As for John Carmack's complaint, it sounds reasonable, but I say the jury is still out on whether a real-world useful program can actually consist of nothing but pure atomic functions.
I'm primarily a C++ programmer, and I've read a handful of articles about why "C++ sucks" and yet all of them seem to prefer C and have nothing in particular to say about C++ that is so bad that the language breaks as a whole. Most of it is ad hominem comments about how most C++ programmers are terrible (I'd say most programmers in general are terrible) or various little nit picking about C++ like "there's too much typing."
What's all the hubub? Are these just whiny old men that are stuck in their habits, or is there any real weight to these complaints?
I scanned the first link and it just seems like another java fan boy knocking on C and C++, though granted he does make a few decent points but the overall feel of it is JAVA IS KING ALL OTHER LANGUAGES SUCK.
Elegance = simplicity. Java, albeit quite ugly in some places (eg. primitive types), is much more elegant than C++. In C++ there are too many features that were added only in order to fix problems with previous features.
if Elegance is a garbage collector that is untameable, then sure. Java is my favorite language to bash. Any language can be used to make terrible programs, but in Java, memory management is something you're not allowed to do. Maybe I'd like to construct a large array and delete it after performing some data on it - but instead, you have to let the garbage collector decide when it is safe to delete. And that could be .... whenever, you just don't know. not to mention the performance cost. GC isn't free...
edit: I once tried to read the Unix Haters Handbook. long story short, I couldn't stomach it.
edit2: template classes can certainly be a pain, particularly with the 'header hell' they create since implementations must generally be outlined in the header files, and god help you if two template classes contain methods that take references to objects of one another's type... but there's still the option to use void* for generic types. I'm sure you're frowning about it, but I've been seriously considering using it over templates for certain classes. After all, C++ wasn't made to be backward compatible with C for nothing, right?
Maybe I'd like to construct a large array and delete it after performing some data on it - but instead, you have to let the garbage collector decide when it is safe to delete. And that could be .... whenever, you just don't know. not to mention the performance cost. GC isn't free...
You can actually do that. I can't remember how to do it in Java, but it's almost the same as C#'s using statement.
Using native code. The object that you're using to manage your native memory is still subject to garbage collection though. Additionally, you'll need to also load the library that contains the native implementation before you can use it, otherwise you'll get an UnsatisfiedLink(er?)Error.
Since the object wrapping the native memory is still subject to garbage collection, often the "Closable" or "AutoClosable" interface is implemented to clean up the native memory. Novices will sometimes also use finalizers to deallocate the native memory, which almost always ends up hindering performance more than the native memory helped it. Still others might use a reference queue from to keep track of when the object is finally collected, but in all cases it costs; and in my experience, the cost isn't worth it when you profile it.
In short, it's very easy to mess up and often hard to get any performance gain out of it. If performance is your concern, why not go all native from the get-go? </gasoline>
* Probably the best example of when native memory can be used to boost performance is through off-heap caching. This helps the garbage collector out more than anything, and often it is the garbage collector that turns out to be the bottleneck if you're dealing in real-time big-data.
** If John Carmack says not to use C++, then I guess I'm out of here as soon as he says what to use. Respect.
No, Java has something similar to C#'s using statement for destroying objects deterministically, but I can't remember how to do it, since I don't really know Java.
In C#, you would do this:
1 2 3
using (StreamReader file = new StreamReader(filename)) {
Console.WriteLine("{0}", file.ReadLine());
} // 'file' gets destroyed here.
try (FileInputStream file = new FileInputStream(new File("C:/")))
{
// do things with file that might throw an exception
}
catch(IOException ioe)
{
//error
}
alternatively (pre-java7)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
FileInputStream file = null;
try
{
file = new FileInputStream(new File("C:/"));
// do things with file that might throw an exception
}
catch(IOException ioe)
{
//error
}
finally
{
if(file != null) file.close();
}
To be eligible for try-with-resource, the class needs to implement the AutoCloseable interface. Basically, the VM calls *.close() at the end of the scope and then the garbage collector grabs it whenever.
I've been using java for about 7 years and haven't heard of anything that can side-step the garbage collector short of native code.
I've been using java for about 7 years and haven't heard of anything that can side-step the garbage collector short of native code
...But why would you even want to? Use a tool the way it's meant to be used. If java was designed with a built in garbage collector then it should be used with the built in garbage collector. If the task at hand calls for direct handling of memory then for christ's sake use a language that allows that. If you really need to use Java for the bulk of your project then there are ways to tie your native code into it. (read: JNA and JNI)
edit: That all wasn't targeted directly at you, Luc. Just thought I'd clear that up before potential misunderstandings came to pass.
Every programmer that favors a certain language will claim any others suck. The only reason anyone takes notice of them is because they have become somebody in their field. If they all said BASIC or VB was the best things since slice bread you would see tons of idiots blindly using that language thinking they will be the next big thing. That is the biggest issue with any language, people get into programming wanting to be something and just blindly use the popular languages instead of learning them for the fun of learning them.
I speculate the following. Because C++ has too many features and, in particular, because it is trying to be both high and low level language, writing awful, hideous code in it is easier than in most languages. Thus there is a lot of ugly C++ code and this C++ can be considered ugly itself. Note, this is a speculation (least negative I could come up with), rather than an observation. I haven't seen that much C++ code...
About the quotes of Linus and Stallman, I'm unsure. Are the two of them somehow particularly great programmers for their opinions to matter on this subject?