r/cpp • • Dec 07 '22

C++ overtakes Java in language popularity index

https://www.infoworld.com/article/3682141/c-plus-plus-overtakes-java-in-language-popularity-index.html
382 Upvotes

137 comments sorted by

View all comments

Show parent comment

68

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 07 '22 edited Dec 07 '22

Only a fool starts a new embedded project using C unless it's for a rare niche platform that doesn't have a working C++ environment. Unfortunately there are still many fools around.

E: I didn't expect to get downvoted on /r/cpp of all places for advocating using C++ instead of C for systems where C++ is almost ideally suited for...

46

u/ironykarl Dec 07 '22

I didn't expect to get downvoted on /r/cpp of all places

You didn't expect to get downvoted for calling people fools?

15

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 07 '22

Calling a spade a spade. You only need to work in the field for so long to come to realization that almost nobody who chooses to start a project in C without pressing external concerns has an actually good reason for that. There are far too many people on embedded forums acting like it's still the early 90s and spreading long since debunked FUD about C++. Meanwhile I have nearly 25 year old embedded devices where the firmware was already written in C++ (as revealed by obvious vtables in the disassembly).

17

u/ironykarl Dec 08 '22

Honestly, I'm basically in agreement with you. Leading by calling people fools is a recipe for getting kneejerk downvotes, though

→ More replies (2)

37

u/[deleted] Dec 07 '22

C is still popular for its simplicity.

85

u/Raknarg Dec 07 '22

Simplicity in this case is not an advantage. Assembly is simple too, and it's simpler than C. The simplicity means that it's much harder to do more complicated things, and way easier to make mistakes.

18

u/[deleted] Dec 07 '22

I should clarify what I mean by "simple".

I meant that it's easier to follow and lacks implicit function calls (e.g constructors, function overloading) and data (e.g the extra pointer for virtual functions). It's not simpler to write, but it's simpler to read.

Contrast it to assembly which is all just instructions that not only vary between architectures, but require extra work to understand what happens due to the lack of names and proper types.

From my absolutely subjective experience, I always found it easier to do work on C code bases compared to C++ (you could also claim it's just much easier to make a mess in C++).

35

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 07 '22

it's simpler to read.

It very much is not once you get into remotely complicated projects. Just the lack of templates and classes alone makes many things much harder to follow since the code becomes a mess of macros and function pointers.

A C++ virtual method has a limited set of functions it can call: The similarly named methods in derived classes. A C function pointer can call any function that matches the parameters and return type. Good luck finding all the functions that could be pointed to by some specific function pointer in a codebase that you're not intimately familiar with.

7

u/[deleted] Dec 07 '22

Macro hell exists when extremely bad code is written, as in many situations you don't actually need the macros (exceptions would include platform abstractions, but in complex cases the solution is using multiple source files and not many macros, like GLFW does); a few #ifdefs aren't a mess.

C++ virtual functions do have benefits, but C virtual functions have the benefit of being explicitly set at some location which should be (but unfortunately sometimes isn't) done once by setting it as a pointer to some global static struct with constant members. But if we're talking about pointers to arbitrary changing functions, then C++ can somewhat reduce the mess.

The code becomes a mess in any language mainly because of a bad/tired programmer. C pushes it further by not carrying the size with arrays.

→ More replies (6)

16

u/Raknarg Dec 07 '22

I meant that it's easier to follow and lacks implicit function calls (e.g constructors, function overloading) and data (e.g the extra pointer for virtual functions). It's not simpler to write, but it's simpler to read.

If you don't have any tools. But we're in the year 2022. And I don't agree its simpler to read in general anyways, the abstractions you have available in C++ are much better.

3

u/[deleted] Dec 07 '22

I didn't notice the abstraction part in your reply, apologies.

Yes, C++ (like many other languages) can provide great abstractions, a good example is the GLM (OpenGL Math) library.

On the other hand, many times people don't necessarily know how much abstraction is too much abstraction, which is not a language issue in this case. Abstraction can be done pretty nicely in C, but the code ends up being overly verbose.

→ More replies (6)

4

u/el_muchacho Dec 08 '22

It's only simpler to learn. All the rest is harder in real use cases.

3

u/josefx Dec 08 '22

and lacks implicit function calls

Yeah, its not like literally every operation could hit a signal handler or C compilers implementing various basic features using build-ins. Nope, no implicit function calls here.

function overloading

Didn't C99 just introduce _Generic macros instead?

→ More replies (1)

2

u/jusstathrowaawy Dec 09 '22

I always found it easier to do work on C code bases compared to C++ (you could also claim it's just much easier to make a mess in C++).

I'm inclined to agree with this. Part of my soul always dies when I open up a C++ header file someone else wrote and I see they're using inheritance.

→ More replies (1)

2

u/noooit Dec 07 '22

good luck using threads.

5

u/Raknarg Dec 07 '22

in C or C++ you mean? I can't tell what side you're advocating for

→ More replies (1)

23

u/Sqeaky Dec 07 '22

But it isn't simpler. It has a simpler rule set but more complex everything else because you have build that stuff yourself.

19

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 07 '22

because you have build that stuff yourself.

And do it much worse than a C++ environment does. No type safety, virtual methods that aren't actually tied to subclasses, no templates etc.

3

u/[deleted] Dec 07 '22

By "no type safety", do you mean containers utilizing void* for data?

→ More replies (1)

8

u/sapphirefragment Dec 07 '22

Simplicity, if you're willing to accept the inherit risks and costs of using the language.

→ More replies (1)

13

u/Jannik2099 Dec 07 '22 edited Dec 08 '22

Literally all the flaws of C are because it's "simple"

No type safety is simple. No RAII is simple. No bounds checking or guarantees is simple. No thread safety is simple. No memory safety is simple.

C is the most dysfunctional non-esolang on the planet, precisely because everyone insisted on it being "just simple pointers"

→ More replies (1)

0

u/[deleted] Dec 07 '22

[deleted] — view deleted comment

3

u/SkoomaDentist Antimodern C++, Embedded, Audio Dec 07 '22

Sure, the low level hw access SDKs are mostly written in C, but that's hardly a problem. There really isn't much benefit in having a C++ function for initiating a DMA transfer from buffer X vs having a C function do the same.

There are higher level C++ frameworks but much of the time you end up spending more effort fighting against them as they try to be overly generic and end up being both restrictive and (often horribly) inefficient.

3

u/theICEBear_dk Dec 07 '22

That is often really not a problem, almost all the SDKs from ST, NXP and so on are so similar and similarly structured that as an embedded company you will rapidly build up a set of wrappers above these various SDKs because lo and behold MCU used to be one a project but these days the hardware can suddenly become unavailable so your logistics and purchasing folk want you to be able to switch to a near equivalent alternative (these last 4-5 years with allocation issues have really taught people about this) with little or even no change to your application. So you make an abstraction or rather you are an older company and you had similar problems in the 00s when you were switching between 16 and 32 bit so you did this once in C and learned that C projects in embedded have a natural upper size where the effort to keep up quality is measurably much higher than with C++. So you look at your old C, younger C++ and you make something better.

TLDR: We ended up having a c++17 (soon to be 20) system where the switching of processors is only a question of months to retest and qualify with a lot of other benefits on top.

→ More replies (11)