r/cpp • • Jan 09 '19

Dear /r/cpp

Sonny from Codecademy here. Last month (Dec 3rd), we launched 4 modules of our first ever Learn C++ course!

https://www.codecademy.com/learn/learn-c-plus-plus

  1. Hello World

  2. Variables

  3. Conditionals & Logic

  4. Loops

And as of this morning, 87,504 learners around the world have started the course!

I'm extremely happy and proud of this because C++ was my first language that was taught in my CS class and 10 years later, I got the chance to work with Dr. Stroustrup and share something that I love with the world.

However, as my teammate Mariel and I about to author the next few modules, I couldn't help but feel that there can still be a lot of improvements made in the first 4 modules. I'm wondering if anyone has taken or is interesting in taking this course and give me some feedback and ways it can improve.

There are also a few things I'm still conflicted on:

  • Should I include `return 0;` in the very first lesson?

  • Where is the perfect place to introduce `using namespace std;`?

  • Where to introduce the different types of errors and debugging?

Any feedback would be highly appreciated! And happy to mail out some Codecademy tees and Pro memberships too!

17 Upvotes

50 comments sorted by

View all comments

55

u/TheThiefMaster C++latest fanatic (and game dev) Jan 09 '19 edited Jan 09 '19

Should I include `return 0;` in the very first lesson?

IMO Yes - you don't need to explain it at this point if you don't want, just say it's necessary.

Where is the perfect place to introduce `using namespace std;`?

Don't. It's considered bad practice.

Where to introduce the different types of errors and debugging?

Introduce debugging very early on - it's a fundamental part of developing. Introduce types of errors as they make sense - no need to talk about access violation / segmentation fault until you introduce memory and pointers, for example.

And for some other suggestions:

  • try to avoid new/delete - they are considered outdated and are very rarely used in modern code. When you get to classes use them on the stack or globals at first, make_unique/make_shared can come later, and pointers later still.

  • introduce c++ standard containers (std::string, std::vector, std::array especially) very early - try to avoid C strings/arrays and hand implementation of data structures (e.g. linked lists by pointers), they're only useful when you're a more advanced programmer. You don't even have to explain templates when you introduce vector/array - just say that's how you use it. Writing templates can come later.

23

u/cY6NDLKG Jan 09 '19

Including `return 0;` is also useful in demonstrating that `main()` is just a function. This is particularly useful to people familiar with functions from their math classes. I've encountered enough people who aren't able to make this connection on their own to warrant a conversation about the mathematical roots of programming.

2

u/Gotebe Jan 11 '19

It's also useful to show the "integration" with the shell or other callers. Also the primary reason for the return value of main.

13

u/NotUniqueOrSpecial Jan 10 '19

just say it's necessary.

Wait, what? Are we talkng about main()?

Because if we are it's absolutely not. The standard says that if the end of main is reached, then 0 is automatically returned.

11

u/dahitokiri Jan 10 '19

While that kind of pedantry is of interest to enthusiastic C++ developers with some experience under their belt, it would just confuse newer developers.

The exception (pun not intended) can be taught as a random aside after the students gain some experience.

5

u/[deleted] Jan 09 '19

[removed] — view removed comment

6

u/kalmoc Jan 10 '19

Sure, but please teach the easy to use stuff (which should also be your default tools most of the time) first and only later, what's going on under the hood. Not the other way round.

That "let's first teach assembler, then c, then c++98 (stop using malloc - use new), then the stl (stop using new for dynamic arrays, use vector), then c++11 (stop using delete use smart pointers) and if anyone hasn't switched to a completely different language by now, maybe we are telling him about c++14 (stop using new, use make_unique) ... " attitude , is a didactic nightmare.

8

u/TheThiefMaster C++latest fanatic (and game dev) Jan 09 '19

I agree that it all needs covering - eventually. But new/delete doesn't need to show up in the first lecture on OOP, as it does in so many courses - classes are just as useful in globals or on the stack.

3

u/[deleted] Jan 09 '19 edited Jan 09 '19

[removed] — view removed comment

1

u/kalmoc Jan 10 '19 edited Jan 10 '19

If you don't comce from a different language, where everything is a reference/pointer, accidential slicing is actually a very rare problem. Nothing you have to worry about when you start to teach inheritance and polymorphism.

Also your example about keeping references into a vector: Sure, iterator invalidation is a topic that needs to be mentioned at some point, but it just isn't a common enough issue to waist brain capacity on when students are still learning the basics of the language and making their first 100 line program.

C++ experts are so great at pointing out all potential problems and corner cases of the language or a piece of code that they completely loose perspective of what is important for someone just starting to learn the language (especially, if you want to keep them motivated).

0

u/[deleted] Jan 10 '19

[deleted]

6

u/TheThiefMaster C++latest fanatic (and game dev) Jan 10 '19

I would agree if there would be a C++ version of from std import vector, tuple, optional, string

using std::vector; is a thing. I haven't checked, but it should work.

0

u/[deleted] Jan 10 '19

[deleted]

7

u/TheThiefMaster C++latest fanatic (and game dev) Jan 10 '19

way too verbose.

using std::vector, std::tuple, std::optional, std::string;

https://gcc.godbolt.org/z/p0eqrU

Available as of C++17.

4

u/dodheim Jan 10 '19

Truly one of C++17's most underrated features.

1

u/Adverpol Jan 11 '19

Awesome. When we make the switch (that's years away) I'll probably transition into this.

1

u/TheThiefMaster C++latest fanatic (and game dev) Jan 11 '19

Typically we (games industry) are right up to date on compilers but they won't let us use `std::` at all...

So we both can't do this for opposite reasons!

5

u/[deleted] Jan 10 '19

Because then they'll learn that typing std:: is actually good and proper and serves an actual purpose. Then they might never get the idea of putting using namespace std in a header somewhere to "make things nicer", or some other horrible thing.

0

u/[deleted] Jan 10 '19

[deleted]

6

u/[deleted] Jan 10 '19

I find the latter much easier to read. "std::string" is one thing to me and I immediately know what it is without really thinking.

"string" sticks in my brain as "identifier whose meaning is unknown, keep in mind you may need to look up what the heck it is"

Some of that undoubtedly has to do with the code I'm used to reading.

2

u/[deleted] Jan 10 '19

True. At least until you get a namespace collision due to a library you want to include or someone else decides to commit something that collides with something in the standard library.

1

u/Adverpol Jan 11 '19

Then I'll (somebody'll) fix that file and move on. I find the fears of those namespace collisions, at least for std, overrated. I just introduced unity builds in our code-base (4000+ files), which as a side-effect means virtually every file in our code-base receives using namespace std. There were plenty of collisions due to liberal inclusion of other namespaces (e.g. boost) but none due to std.