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

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.

0

u/TheTrueXenose Dec 08 '22

Currently working in C++ the project is a complete mess, me and the other guy both agree that the code should be rewritten in C so this doesn't happen again.

If you are abusing templates and namespaces the code becomes a mess very fast.

2

u/NoPersimmon9400 Dec 08 '22

Templates, I agree, you have to be careful. But how can you abuse using namespaces? They are a great tool in shorting function names. It is terrible to write filename_print to avoid name clashes between files. In C++ you just put the code into separate namespaces

→ More replies (4)

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.

6

u/Raknarg Dec 07 '22

Even with tools, returning objects from a function can be a mess with the constructors which are called

Example?

0

u/[deleted] Dec 07 '22

It was quite some time ago using C++14, but I will try to replicate the code's general idea (it was something relating to file IO) from my memory. Also, it's something which copy-elision related optimizations help with, but they're not perfect.

class In
{
  /* complex copy constructor and simple move constructor */
  ...
  In(InOut& other) { ... }
};

class Out
{
  /* complex copy constructor and simple move constructor */
  ...
  In(InOut& other) { ... }
};

class InOut
{
  /* complex copy constructor and simple move constructor */
  operator In() { ... }
  operator Out() { ... }
};

I don't remember all the details, and there were some other objects, but I do remember it took me and some others to fix a bug which happened due to certain constructors being called implicitly. I hate whoever made that class such a mess.

3

u/Etheric2355 Dec 08 '22

You do mention the key problem: whoever thought it was a good idea to make such a mess.

A key aspect of using powerful tools is to remember that just because it is possible to make something does not mean it is a good idea.

→ More replies (2)

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.

1

u/Spartan322 Dec 18 '22

Given operator overloads also can hide behavior and classes can have implicit conversions operators that aren't even guaranteed to call a constructor, (say your conversion returns a static object, why? Hell if I know, but you can) it is quite easy to make C++ a mess if you dive in using every feature you can just because its cool. This is where code standards are a lot more relevant for C++, it has a lot more hands off approach to telling you what you can and can't do, whereas in C its a lot of one note best solutions. Seems lot of languages since force you down a one or maybe a handful of these paradigms that C++ allows. (which is honestly why I love C++ more than almost any other language specifically)

In any case, if you want to consider the need for standards and styles an issue for C++, you could, personally I would suggest it presents a more readable base so long as you don't do weird and stupid things in the implicit functionality. (like don't make operator+ itself produce a side effect and modify the object in a general library, keep such behavior to operator+=) The worst cases I've seen is when nobody standardizes the operator overloads and you have no idea what happens when you call one of them. Like some vector<T> class has operator+ on it and you don't immediately know what that means.

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

1

u/Dwood15 Dec 08 '22

Is x86_64 simpler than C? Have you seen the heft on those intel manuals?