Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

The (1) also mentions C.

I would partially agree with you, if it only mentioned C++.

Only partially, because too many C++ devs still use plain old C strings.



sort by: page size:

Not sure why it mentions C++. C++ has std::string which is far superior to this.

Which is why s/he specified that the post was about c++ and not c in literally the first two words of the post :)

The article is about C, not C++.

> It's a somewhat common pattern in C (...)

C++ isn't C, though.


> everyone chooses a different subset to use

I prefer using C++ like "C but with cin and cout" and I informally call it C+ :)


The article doesn’t use any C++ features, though. It’s straight C code. Pretty sure that’s what they meant

> so much cleaner and complete than those "algorithmic and templating" ones of C++

That's C++ strings, too: https://docs.microsoft.com/en-us/cpp/atl-mfc-shared/referenc...

Not only they have better API (replace, tokenize, implicit cast to const pointers), these strings are often faster. That particular class is Windows-only, but nothing prevented C++ standard folks to come up with conceptually similar cross-platform stuff. BTW, that CString class predates C++ standard library by many years.

> And C++ can quickly become difficult to read after overloading various operators on classes or after using fancier less-known features.

Yes, and I saw quite a lot of code like that.

But in other cases these features help with readability. I often code math-heavy stuff in C++ processing vectors, matrices, quaternions, complex numbers, etc. The ability to implement custom math operators on these structures IMO helps with readability.

What doesn't help is the ability to abuse them, like C++ iostreams do with `operator <<` everywhere.

Not just operators, it's generally too easy to abuse features of the languages, writing code that's very hard to work with. Unfortunately, not doing that requires lots of experience with the language.

I'm not planning to switch due to the good parts. First-party SIMD intrinsics support. Trivial interop with C and C++ libraries: hard requirements like OS kernel APIs and GPU APIs, industry standards like libpng, or just very nice to have like Eigen. Very small runtime allows to build dynamic libraries, consume them from anywhere, and not worry about binary size or runtime dependencies. Also tools like debuggers and profilers are very good.

But when performance is less critical, I'm more productive using other, higher-level languages.


C++ isn't C.

Note that the baseline is C not C++ used badly.

The article is about C (as in C11 standard) and not about C++ (as in C++11).

This is C, not C++.

That's C, not C++

In the context of the first paragraph in this post, they are not. The stuff he talks of in that paragraph applies to C, not C++.

Yup... old habit of writing C/C++ :)

He said C11, not C++(11).

That was my thinking. But as most C++ developers already know this, I'll skip the C++ mentioning :)

>...Here's an example of C++ code written intentionally with all the latest C++ features.

Does it really need to include <string.h>?


I agree with this. I only mentioned "C++11" in my comment so that its clear what I'm referring to.

The tweet contains zero substance. I did the dirty work to dig through the linked links to get the goods and share, enjoy! It's a good read.

---

Thanks!

A few comments:

In some ways, I still think the Quake 3 code is cleaner, as a final evolution of my C style, rather than the first iteration of my C++ style, but it may be more of a factor of the smaller total line count, or the fact that I haven’t really looked at it in a decade. I do think “good C++” is better than “good C” from a readability standpoint, all other things being equal.

I sort of meandered into C++ with Doom 3 – I was an experienced C programmer with OOP background from NeXT’s Objective-C, so I just started writing C++ without any proper study of usage and idiom. In retrospect, I very much wish I had read Effective C++ and some other material. A couple of the other programmers had prior C++ experience, but they mostly followed the stylistic choices I set.

I mistrusted templates for many years, and still use them with restraint, but I eventually decided I liked strong typing more than I disliked weird code in headers. The debate on STL is still ongoing here at Id, and gets a little spirited. Back when Doom 3 was started, using STL was almost certainly not a good call, but reasonable arguments can be made for it today, even in games.

I am a full const nazi nowadays, and I chide any programmer that doesn’t const every variable and parameter that can be.

The major evolution that is still going on for me is towards a more functional programming style, which involves unlearning a lot of old habits, and backing away from some OOP directions.

http://www.altdevblogaday.com/2012/04/26/functional-programm...

John Carmack

next

Legal | privacy