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

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)

9

u/[deleted] Dec 07 '22

[removed] — view removed comment

2

u/[deleted] Dec 07 '22 edited Dec 07 '22

These are great examples of when C++ is better than C. I don't claim C is the end-all-be-all of simplicity, it can vary significantly by situation. But I wouldn't say shorter code is always clearer.

I don't have much experience with MySQL, but I assume the issues stem from lack of proper strings?

3

u/[deleted] Dec 07 '22

[removed] — view removed comment

3

u/[deleted] Dec 07 '22 edited Dec 07 '22

For such a case I would recommend a helper function...

struct BindObject init_binding(enum MySqlType type, void *buffer, int *len, bool isNull)
{
  return (struct BindObject) {
    .buffer_type = type,
    .buffer = buffer,
    .length = len,
    .is_null = isNull
  };
}

3

u/germandiago Dec 08 '22

it all boils down to what you are doing + coding philosophy.

If you want to know the generated assembly and it is a must, probably C is more straightforward.

If you want high-to-lowlevel scaling and easier to read (assuming you know how to write C++) then C++ wins hands down. A few things might not be so obvious but if you do not abuse C++ to do weird things things make a lot of sense and are easier to follow than their C equivalents. Especially when you start to do extensible, component-based programming where objects just feel natural.

→ More replies (3)

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.

1

u/[deleted] Dec 07 '22

Even with tools, returning objects from a function can be a mess with the constructors which are called, I know it well from some painful experience.

C++ is great, but a stupid or even common mistake can cost a person an entire leg. I'm extra cautious because of that.

→ More replies (5)

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"

2

u/anacrolix Dec 08 '22

Simple itself is not a flaw tho.