r/cpp • • Aug 25 '21

Experience C++ developers: do you enjoy using C++ (even for personal/hobby projects?)

Do you actually enjoy using C++, or do you only use it for work, or purely for performance? This question is aimed at people with years of experience, who've worked on large projects in C++. I see people who dislike the language, but use it purely for performance reasons. This sort of discourages me from learning it. I don't want programming to feel like work.

204 Upvotes

327 comments sorted by

View all comments

Show parent comments

11

u/[deleted] Aug 25 '21

[removed] — view removed comment

26

u/[deleted] Aug 25 '21 edited Aug 25 '21

Do universities teach those as “best practices”?

What I mean is, that's the style they teach and require in class.

I'm one of the few C++ developers I know and constantly have to deal with "lmao, you suck because you write in C++, it's so bad, because I tried it in college and it was horrible." Not all professors, but the many times I've looked up what was taught at various universities of people I've talked to and found they're teaching C++98. Usually it ends up looking like forcing students to write in "C with function overloading", though I've also seen almost literal "C with classes" before. I know C++ used to be called "C with classes" but it's a lot more than that now. They ignore smart pointers since in C++ "you have to manage memory" and use using namespace std to get rid of all of "those pesky std::" to make things fit on PowerPoint slides.

They're just often not up to date and often just trying to "simplify" things for the students due to time constraints and the number of students in the class (50-100+) to ease TA and Professor burden. However, it's the programming equivalent of teaching a future mechanic about modern car design by working on a Model T.

The comparison I jokingly give in terms of similar timelines is: "Oh, you must hate Python too, because I only used version 1.5 and I didn't like it."

8

u/gnosnivek Aug 25 '21

You also just get some plain weird stuff because people don't know better and bad advice gets passed down. I remember being told in my first C++ class that any data allocated with new had to be released with free().

I mean, I guess it technically didn't break anything, but hoo boy am I glad I don't have to abide by that rule anymore.

3

u/Ahajha1177 Aug 25 '21

Oh geez, pretty sure that's UB, isn't it? Not to mention that the destructors don't get called... shudders

2

u/gnosnivek Aug 26 '21

I'm actually not 100% certain how illegal it is. It's UB to call delete on anything not allocated with new, and it's UB to call free on anything not allocated with malloc, but AFAIK there's no rule that says that a pointer allocated with new must have delete called on it (after all, leaking memory isn't UB). So if the implementation of new gets memory from malloc() and doesn't alter it in any way, this might actually be legal on that implementation. I'm not sure (would love a pointer if there's a rule that explicitly makes this undefined!)

That being said, making a habit of this seems like a surefire way to get smacked by horrifying amounts of UB at some point.

Not to mention that the destructors don't get called

Well obviously the solution to that is to manually reimplement the destructor using loops/recursion/free whenever you need to call delete. (We had to do this and it was dumb).

1

u/Ahajha1177 Aug 26 '21

Maybe it's more that it's not cross-platform, as it relies on new calling malloc internally, which seems reasonable but as you said is implementation defined.

9

u/gnosnivek Aug 25 '21

> why not just use C?

This is actually a really interesting question that I haven't thought about. I wonder if schools feel like they have to give both a low-level and high-level introductory programming experience, don't want to have one class for each, and fall back to C++ because you "can" do both in the same language.

I remember trying to tutor a student who was taking a C++ intro programming class, and after going through classes, constructors, inheritance, operator overloading, and some basic data structures, the class suddenly went "now for your final project, write a game of life simulation using only C constructs (arrays, malloc/free, printf)"

It was a gratuitous turn into "low level" programming (low level in massive air quotes here) and I wonder if that was there because someone said "well we need to make sure the students have experience with C before moving on" and nobody was willing to commit to actually having a quarter to properly teach it.

6

u/[deleted] Aug 25 '21

[removed] — view removed comment

2

u/SirClueless Aug 25 '21

I think it can work. At my university, they teach a freshman course aimed at future CS majors in Java, and in your sophomore year you have two courses you can take in either order taught in Java (Algorithms) and C and Assembly (Programming Systems) respectively.

It works out pretty well, I think. I firmly believe that anyone looking to be a software engineer should take a class taught in C at some point in their life, but it doesn't have to be your introduction to programming.

2

u/jk-jeon Aug 26 '21

Here is my perspective.

I started programming with VB6 when I was 11 yo and it was a lot of fun. I learned C after that and eventually abandoned VB6 in a few years because it looked like everyone was regarding VB6 as "a children's toy". But when I first learned C, the console and CUI thing looked boring and pointless since I could make more decent-looking GUI's with VB6.

My point is that, it really depends on the audience. If "the first course" is for children of age 4-9, do you think C will be more effective than something like Scratch? Or, how about the first course in university for students not majoring in CS?

Another point is this. For me, even after learning C, computer was still a total mystery until I learned a lot more details later. I mean, what the hell is monitor and keyboard, and how is it relevant to this pointer and memory thing? You know, monitor, keyboard, speaker, mouse, or whatever UI devices (and possibly including HDD/SSD?) are how ordinary people perceive computer. They don't understand why those are called "peripheral" devices. There is still too deep gap between their daily understanding of computer and what basic knowledge in C teaches them. So teaching C might not be that "touching". At least it really wasn't to me.

2

u/[deleted] Aug 25 '21

I think every student should learn and use C for a moderately complex project at some point in their life, if only to learn all the ways you can shoot your own foot with it, and get a better understanding and appreciation for what modern languages do for us.

3

u/gnosnivek Aug 25 '21

I absolutely agree. C demands a discipline unlike many modern languages and it's good to work in a no-guardrails environment just to appreciate what you really have.

That being said, I also think it's strange to detour into C at the end of a C++ intro class. Especially in the case I mentioned, the students hadn't been given the proper tools (either computational or conceptual) to successfully work with C code, and most of them just slapped things around until it (sort of) passed the tests.

1

u/[deleted] Aug 25 '21

Yes, going from C++ to C would only confuse the students, they're completely different languages.

7

u/da2Pakaveli Aug 25 '21

Not Uni but my school did. Some of my teachers would probably be surprised if they heard C++ had newer versions.

4

u/braxtons12 Aug 25 '21

When I took my data structures and algorithms course 6 years ago (the only course we had in c++, everything else was java, python, or C; mainly Java), the standard library was barely even acknowledged to exist, and by that I mean we were told that std::string and std::vector are a thing, and that's about it.

This was also the first course that wasn't in Java, so naturally people didn't get the whole headers vs implementations thing. And I'm not entirely convinced all the professors did either, because one never explained them at all and just had his students write everything in one huge .cpp file for each project.

2

u/[deleted] Aug 25 '21

As someone who learned C++ back in 2005 from a university, not a single thing he listed there was considered best practice and most of those were explicitly called out as bad practice.

That list sounds like a compilation of the worst takes from CS schools across the globe condensed into a single list hehe.

0

u/SJC_hacker Aug 25 '21

I guess maybe they stay away from those abstractions as a way to get students to program closer to the hardware, e.g. more in line with C, as a learning tool, but then why not…just use…C? And use C++ elsewhere as it’s intended to be used?

This is exactly what I think they should do. As much as C++ tries to distance itself from being a low level language, at its core thats what it is . Certain language features such as references will confuse beginners because they assume they work like in other languages (they don't, underneath they are semantic sugar for pointers)

But ... it should come AFTER they learn essentials such as algorithms, etc. When learning C and C++. The focus should be on the language and all the nasty details that go with it, like linking in libraries, etc. Ditch any IDE, just command line, preferably with Linux. Call gcc/clang manually. Understand what the compiler flags are doing. Ack (or find/grep if you want to be old school) to look up references in a source tree. Really understand whats under the hood. Thats how you learn.

0

u/pjmlp Aug 26 '21

I guess because even C with Classes is more type safe than just using C.

That was the whole point why Bjarne originally created CFront, to avoid ever going trough his Simula -> BCPL downgrade ever again.