r/cpp • u/sonnynomnom • 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
Hello World
Variables
Conditionals & Logic
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!
18
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
mainis 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.
0
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
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
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
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
Jan 10 '19
[deleted]
6
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
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 receivesusing namespace std. There were plenty of collisions due to liberal inclusion of other namespaces (e.g.boost) but none due tostd.5
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
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).
7
u/Scyther99 Jan 09 '19
Since this seems to be course for the very beginners, I would stay away from language specific stuff and focus on fundamentals, cycles, flow control, etc. Debuggng should be introduced as early as possible if you have any homework assignments or anything they do on their own.
24
u/tvaneerd C++ Committee, lockfree, PostModernCpp Jan 09 '19
Just don't do using namespace std;.
If you teach students to always type std:: (std::vector, std::cout, etc...) then you don't actually need to explain what namespaces are at all (you can introduce that later).
You just say a bunch of useful stuff is available, and it is all named starting with "std::". If someone asks "why isn't it std::int then?" you say "the base stuff is just int, etc, but these extras things in std are built on top of the base - later we will teach you how to built your own helpful pieces just like std:: stuff."
3
u/jherico VR & Backend engineer, 30 years Jan 10 '19
Just don't do
using namespace std;I concur, although it might be reasonable to show them early on the ability to do something like
using string = std::string; using vecstr = std::vector<string>;To a beginner the constant typing of
std::feels like a pointless burden, especially if you're not an experienced touch-typist. But learning that they can alias long names helps with that quite a bit.2
u/tvaneerd C++ Committee, lockfree, PostModernCpp Jan 10 '19
Maybe.
Are most people now fast typists? I mean the internet generation? Doesn't everyone type? (Although code typing is definitely somewhat different)
1
u/corncobcareers Jan 12 '19
i think everyone being a proficient physical keyboard typist may have already passed. many young people primarily interact with computers through mobile devices and may be less proficient at using PCs.
1
u/TheThiefMaster C++latest fanatic (and game dev) Jan 10 '19
using string = std::string;You can actually do
using std::string;- no need to repeat the type name if you aren't changing it!Works with templated types like vector too:
using std::vector;As of C++17 you can even do a comma separated list:
using std::vector, std::string;0
Jan 10 '19
[deleted]
3
Jan 10 '19
I think it's better to learn good habits straight away than first learn poor habits and then unlearn them. This goes double for people without preconceptions.
2
u/tvaneerd C++ Committee, lockfree, PostModernCpp Jan 10 '19
I'd say there is always something at least questionable with teaching something for instructional purposes, unless you are clear about it "we will do this for now to keep it easy, but we'll show you a better way later".
I just highly suspect it isn't taught that way. It is taught as "do it this way".
4
u/MontagFTB Adobe Source Libraries | stlab Contributor Jan 09 '19 edited Jan 09 '19
Should I include
return 0;in the very first lesson?
You do not need to return from main and the program is still well-formed. (main is the only function that returns a value for which this is true.)
If you do decide to keep the return expression, I would suggest using EXIT_SUCCESS as it is considerably easier for a beginner to understand than an integer value.
5
u/JonKalb CppCon | cpp.training Jan 09 '19
When teaching a complicated subject to beginners, it is best to avoid special cases and exceptions to general rules. If possible, avoid the special cases and postpone discussion of exceptions to general rules until after beginners are comfortable with general cases. It is also best to postpone discussion of distracting details.
The special rules about main() (that it must have a return type, which must be int, that, even though it has a return type, it need not have a return statement, that if it doesn't have a return statement the system will provide a returned value) are all minor not-first-day-of-learning issues.
The fact that OS's expect programs to return zero to indicate success and non-zero to indicate errors is a distracting detail that is also unworthy of mentioning in a first class in C++ programming.
In keeping with the best teaching practices, it would be best to not have to mention any of these special rules/exceptions or distracting details to beginners. Unfortunately, main() is the one function that every program must contain. In order to not have to mention any of the special rules, you will need to have your example main() return a value and if you return zero, you will need to explain the semantics of that value.
So I agree with MontagFTB. Ending your program with
return EXIT_SUCCESS; // Tell OS we were successful.and the explanation that we need to signal to the OS that our application executed successfully, minimizes the distractions and avoids a lot of explanations that are simply not important to beginners.
What MontagFTB didn't point out is that you will also need to have
#include <cstdlib>because EXIT_SUCCESS is defined in cstdlib.
Note that there is hope for this situation. It is possible that future standards will allow main() to return void, which will significantly simplify "hello world" level instruction.
1
Jan 09 '19
Went through it (i skipped all the example projects i didn't feel like writing so much code), in general everything seems simple and the progression makes sens, pretty good start.
the missing return 0, just seems like bad style to have the int main function not returning an int. Which brings me to the next point:
"\n" vs std::endl; there's a bit of a debate about it but i'd probably go with std::endl; just something you might want to think about.
I'd probably go with std::sqrt() over sqrt when you're already using cmath and adding std::pow() might be a nice addition, cmath would actually be there for a reason (afaik sqrt is not from cmath, std::sqrt is)
Quiz x = false; doesn't have to be a bool and might cause unexpected behavior e.g. if x is a integer. I'd say it's really important to know that just because you assign an value it doesn't have to be the right type.
int i = false; // works just fine
using namespace std; It's complicated but important to introduce, i'd probably say you can make an extra module just for "using" maybe after you introduced a few other std functions or types (vector and sort maybe?)
using namespace std; using std::cout; using Numbers = std::vector<int>;
There might be some details i missed and i got pretty annoyed by the site that asked me every 5 seconds to upgrade to pro and start my 7 day trial... god i that annoying. A skip button would be nice to, couldn't look at some things because i couldn't be bothered to write the code.
1
2
u/Gotebe Jan 11 '19
return 0
I quite like return EXIT_SUCCESS/FAILURE. It's standard C since forever, it might teach people that magic numbers have implied meaning whereas explicit is better and for those suffering from OCD, it might dawn that there could be a platform where these macros give different value for whatever reason.
3
Jan 09 '19
One interesting thing not mentioned in this thread is "inheritance/composition with references, not pointers". This is another thing Kate Gregory talked about. If you use references instead of pointers you can postpone introducing pointer syntax (and thus pointers themselves and everything that comes with that avalanche if questions) until your "class" is ready to tackle that subject on its own - pointers aren't introduced as "a necessary evil" to do what other languages do without them.
4
u/ShillingAintEZ Jan 09 '19
Are we just allowing advertisements disguised as questions now?
7
Jan 09 '19
well if it is advertisement it's pretty shitty trying to sell a basic programming course to the in general very advanced users in r/cpp
7
u/STL MSVC STL Dev Jan 09 '19
This is somewhat advertisement-ish, but I'm going to allow it, on the grounds that it's sparking useful discussion. However, the usual guidelines about self-promotion stand - posts that are purely "go buy this thing" are not creating discussion and will be removed, and similarly for repeated self-promotion from accounts that do nothing else.
As long as people are being good kitties and helping the C++ community grow, we can take a permissive stance.
3
u/ponybau5 spaghetti oh my Jan 09 '19
It's a good idea to return 0 after main to indicate no errors, non zero should indicate error, say a file failed to open or unexpected exception.
1
u/ZaitaNZ Jan 09 '19
Not sure why this is being downvoted. Returning 0 from main is VERY useful for integrating with external and automated tools (e.g. jenkins, bash scripts). Knowing that returning 0 indicates success and non-zero indicates an error is used basically everywhere I've worked over the past 20 years.
My current project has ~30 different build and test profiles in Jenkins. Knowing how an application interfaces with the CLI is very important.
5
u/nwL_ Jan 09 '19
I downvoted it because
return 0;is auto-generated by the compiler and adds no value to the lesson. You can introduce it when you introduce functions – “Like functions, the main() also returns, usually 0 on success.” There, done.1
-2
u/Scyther99 Jan 09 '19
Yea definitely, these guys will today learn what variable is and the next day they will automate their builds with jenkins.
0
u/ZaitaNZ Jan 10 '19
The number of developers I've worked with over the past 20 years who have amazing understanding of the language and it's standards, but very little understanding of the ecosystems their code runs in is very high. Teaching from the beginning how your application interacts with the Operating System should be fundamental.
This is a variable, it can be stored on the stack or heap by doing X; the pros and cons are Y; memory is handled through Z design. There are primitive and complex types; e.g. ABC. This is the main function, it interacts with the operating system through Z processes. The concepts are not that difficult.
1
1
u/CubbiMew cppreference | finance | realtime in the past Jan 09 '19
Should I include
return 0;in the very first lesson?
I vote for 'no' - I've seen far too many newbies asking what it means. Simpler is better.
TC++PL's "hello world" (reproduced on Wikipedia's C++ page) doesn't use it.
-11
u/Pragmatician Jan 09 '19 edited Jan 09 '19
You should not include
return 0;in the first lesson. It has no value to a beginner who can do nothing but memorize it unless you explain how the CLI works. A lot of C++ programs don't care about themainreturn value anyway.using namespace std;is great for beginner code because it reduces verbosity of vocabulary types such asvector,stringandarray, and makes the language less intimidating. This is also a pretty easy thing to unlearn once you get to explaining namespaces.
7
u/distributed Jan 09 '19
This is also a pretty easy thing to unlearn once you get to explaining namespace
I don't think that learning something with the intent to unlearn it later is a particularly good idea
0
Jan 09 '19
[deleted]
1
u/jjmc123a Jan 09 '19
Also, if I have a bunch of vector declarations, I might use "using std::vector", since it is pretty clear where the vectors come from. But yeah, "using namespace xxx" in general can be problematic, especially with a huge one like std.
17
u/ste_3d_ven Jan 09 '19
I would look to Kate Gregory's talk on teaching C++ to people I feel she has put together a very good and consistent curriculum
Her talk on this can be found here:
https://youtu.be/YnWhqhNdYyk