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 comments

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++).

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.

1

u/[deleted] Dec 08 '22

If I'm to share more of my experience, some of the best code I've seen used a constructor only to default-initialize the values, and an explicit init() method to initiate (like copy/move constructor) the object.
It made things much easier to follow for complex objects.

2

u/germandiago Dec 08 '22

It is equivalent to just use (when the last byte of storage is not important) a default constructor where you do your init() and use std::optional<YourType> obj = std::nullopt and it is more regular to use and less surprising.

You can later assign and really construct it. No two-phase init.

→ More replies (0)