Live data from Hacker News

Why do C++ folks make things so complicated?

johndcook.com

21–30 of 34 posts

Re: Why do C++ folks make things so complicated?

#21
post #16
post #12

Earlier quoted context omitted.

Performance is a cross-cutting concern, you should factor it into the design of the application and not remember it when doing field tests and noticing that the software is too slow. This doesn't mean that you must use C or C++, but assuming that you can always rewrite the slow parts in C or C++ is inviting disaster.

Doesn't that go against all we have learned in CS? Early optimization being the root of all evil. And don't optimize what you think will be slow, but what you have measured to be slow?

If we had learned something, we'd have learned the FULL quote, wouldn't we?

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

Note, "small" and "97%".

That means that you should make the right large-scale decisions before you code. Or measure.

Re: Why do C++ folks make things so complicated?

#22
post #16
post #12

Earlier quoted context omitted.

Performance is a cross-cutting concern, you should factor it into the design of the application and not remember it when doing field tests and noticing that the software is too slow. This doesn't mean that you must use C or C++, but assuming that you can always rewrite the slow parts in C or C++ is inviting disaster.

Doesn't that go against all we have learned in CS? Early optimization being the root of all evil. And don't optimize what you think will be slow, but what you have measured to be slow?

Not really. There are two sorts of optimizations:

1. Structural - designing the top-level system to perform its work with minimal activity.

2. Local - Calling conventions, component-local algorithms, etc.

Now this is fuzzy for the same reason that one man's strategy is another man's tactics. But the gist is simple: you don't tighten the bolts on the system until you have it in the right shape.

Local optimizations are easy to do b/c you can usually pull them out of a textbook, or figure it out by looking at one or to parts of code. Structural optimizations require an understanding of the system performance as a whole -- possibly allowing some suboptimal local behavior. Sadly the way I've mostly seen is super-tightening the bolts and having the system slowly distort from something suboptimal but flexible into a contorted, performs-ok-but-dont-touch-it terrible beast. That's the sort of optimization that's the root of all evil.

Example: I have a webserver, with a table of regexs mapping to handlers. The handlers can be static assets in files or dynamically generated in code.

Local optimization: I notice that after URL pattern match, my static assets are read through normal stdio file I/O. I use sendfile or whatever's the new hotness on my platform to make it fast.

Structural optimization: I notice that the majority of my hits are static for a small number of files. I write a new webserver (say static.foo.com) for them specifically, that looks up the URL in a hash table for an in-memory copy of the file. I just ping it with SIGHUP when I want to invalidate the hashtable's values.

Re: Why do C++ folks make things so complicated?

#23
post #2

C# comes to mind when he is talking about top/bottom C++. They got this right, plus added some neat high-level features.

Actually, the commenters are arguing about what languages are best used in those roles; e.g. C as "bottom C++", Python as "top C++" - or, indeed, C# as "Top C++". C# is nice, but I wouldn't want to write a kernel in it.

C# is nice, but I wouldn't want to write a kernel in it.

Why not? :)

http://singularity.codeplex.com/

When C# is combined with Code Contracts / static verification, you get fairly clean/performant code and strong safety guarantees. I think that'd be quite nice for kernel programming.

Re: Why do C++ folks make things so complicated?

#24
post #12

If you want to make such a distinction, Qt is probably what would come close to top-C++. It offers dynamic binding via signals/slots, Java-like iterators, simple multi-threading/parallelization, signal/slot-driven networking, database access, and some automatic memory management (deletion via parent widgets, and auto-pointers). It only misses the proposed compiler warnings to indicate that 'bottom-C++' is used. Still…

Performance is a cross-cutting concern, you should factor it into the design of the application and not remember it when doing field tests and noticing that the software is too slow. This doesn't mean that you must use C or C++, but assuming that you can always rewrite the slow parts in C or C++ is inviting disaster.

We all know the caveats on http://shootout.alioth.debian.org/u32/which-programming-lang... , but I would point out over the last few years, more and more languages are creeping up into the still IO-bound nowadays, and how much not-IO-bound code is moving to the GPU anyhow, the age of "We have to use C++ for the performance" should be rapidly coming to a close. The last bit missing is just the people bit, now.

I don't know what the first non-C practical OS will look like, but I hope I live to see it. I can't believe in 2011 my OS is still getting buffer overflows and memory mismanagement and all of the foibles that despite everyone loudly declaring is all the programmer's fault and not the langoage's fault, still seems to follow the language around like dog poo stuck on its shoe.

Re: Why do C++ folks make things so complicated?

#25
post #21
post #16

Earlier quoted context omitted.

Doesn't that go against all we have learned in CS? Early optimization being the root of all evil. And don't optimize what you think will be slow, but what you have measured to be slow?

If we had learned something, we'd have learned the FULL quote, wouldn't we? "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" Note, "small" and "97%". That means that you should make the right large-scale decisions before you code. Or measure.

Yes. Another way to put this is that there's a difference between "designing to be sufficiently performant" and "optimization".

Re: Why do C++ folks make things so complicated?

#26
post #24
post #12

Earlier quoted context omitted.

Performance is a cross-cutting concern, you should factor it into the design of the application and not remember it when doing field tests and noticing that the software is too slow. This doesn't mean that you must use C or C++, but assuming that you can always rewrite the slow parts in C or C++ is inviting disaster.

We all know the caveats on http://shootout.alioth.debian.org/u32/which-programming-lang... , but I would point out over the last few years, more and more languages are creeping up into the still IO-bound nowadays, and how much not-IO-bound code is moving to the GPU anyhow, the age of "We have to use C++ for the performance" should be rapidly coming to a close. The last bit missing is just the people bit, now. I don't…

As long as memory management is not performed by the hardware, a C-like language will be needed to write operating systems. What I think is possible is to write more of the operating system in a higher level language, with only a small part of it in C. Or, even better, have a smaller operating system with everything else written in user space. This is the original idea of the UNIX designers, of course, so nothing really new here.

Re: Why do C++ folks make things so complicated?

#27
post #12

If you want to make such a distinction, Qt is probably what would come close to top-C++. It offers dynamic binding via signals/slots, Java-like iterators, simple multi-threading/parallelization, signal/slot-driven networking, database access, and some automatic memory management (deletion via parent widgets, and auto-pointers). It only misses the proposed compiler warnings to indicate that 'bottom-C++' is used. Still…

Performance is a cross-cutting concern, you should factor it into the design of the application and not remember it when doing field tests and noticing that the software is too slow. This doesn't mean that you must use C or C++, but assuming that you can always rewrite the slow parts in C or C++ is inviting disaster.

With most modern languages having the easy capability of writing an extension in C, this is poor advice. You can 100% quickly mock up whatever in ruby, python etc, and when you need to, write a module in C that is plug and play. I have done this several times for calculating metrics on geodata. I wrote a ruby class to do the work, used it in production until we got a good enough amount of users (load), then rewrote it in C. My main code wasn't even touched since I had already abstracted the calculations into their own class. All this requires is you think ahead and pull out any heavy-lifting code into its own class, and you can just swap that out down the road.

Re: Why do C++ folks make things so complicated?

#28
post #24

Earlier quoted context omitted.

We all know the caveats on http://shootout.alioth.debian.org/u32/which-programming-lang... , but I would point out over the last few years, more and more languages are creeping up into the still IO-bound nowadays, and how much not-IO-bound code is moving to the GPU anyhow, the age of "We have to use C++ for the performance" should be rapidly coming to a close. The last bit missing is just the people bit, now. I don't…

As long as memory management is not performed by the hardware, a C-like language will be needed to write operating systems. What I think is possible is to write more of the operating system in a higher level language, with only a small part of it in C. Or, even better, have a smaller operating system with everything else written in user space. This is the original idea of the UNIX designers, of course, so nothing rea…

I remember an attempt to write operating system in Python primarily, it was called "unununium", apparently the project died, but the site is still up http://unununium.org, though it now talks about ASM, not Python.

A quick google finds this post mentioning a release http://objectmix.com/python/179041-unununium-os-0-1-rc2-rele...

"implemented features include a fully functional Python interpreter, floppy, ATA, and ext2 drivers written entirely in Python."

Would love to find source code for that actually.

Re: Why do C++ folks make things so complicated?

#29

C++ already contains "top" and "bottom" C++. You can write boost-style code, with heavy use of smart_ptr and STL algorithms & functors, or you can stay pretty close to the "C with classes" layer.

That's the thing I like most about C++. I can do any type of programming in it (OOP, functional, procedural, templates, etc.). I can roll my own data structures or use the built-in STL containers while working with low-level C-like code. I'm not sure why so many people complain about it. Once I began using it, it quickly became my primary programing tool on Unix, Windows and Macs.

Re: Why do C++ folks make things so complicated?

#30
post #21

Earlier quoted context omitted.

If we had learned something, we'd have learned the FULL quote, wouldn't we? "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" Note, "small" and "97%". That means that you should make the right large-scale decisions before you code. Or measure.

Yes. Another way to put this is that there's a difference between "designing to be sufficiently performant" and "optimization".

It also means "don't be boneheaded about local performance". That's the other 3%. I wish it didn't need to be said, but there are quite a few projects I've seen where people blatantly ruined performance, all the while quoting Knuth.
Post reply on HN