r/cpp_questions • • Feb 13 '22

OPEN Damn learncpp.com is AWESOME!!!!

Title says it all. I am learning C++ on such a deeper level from the usual youtube videos.

113 Upvotes

19 comments sorted by

View all comments

3

u/alfps Feb 14 '22

The tutorial stands out as high quality.

Below I comment on the few things that are not, as far as I read (or skimmed) the tutorial.


Part 0.7.

The first statement that I found a bit dubious, after a lot of excellent text and illustrations:

❞ Q: Can I use a command-line compiler (e.g. g++ on Linux)?

Yes, but we don’t recommend it for beginners

Most students who try it find the command line much more convenient than using an IDE, and get a much better understanding of the tools usage. An IDE is designed for large multi-file projects that are the opposite of what a beginner does; it hides the tools usage; and it presents additional obstacles of its own, such as Visual Studio's "precompiled headers" (which are on by default, with non-standard perplexing preprocessor behavior). Happily this tutorial emphasizes that one should turn off "precompiled headers" in a Visual Studio project, although without mentioning the non-standard behavior.

Another nit: Microsoft's Windows Terminal application should have been mentioned and recommended, and there should be a recommendation to make it the default console window handler. Windows as a programming environment is difficult enough without having to struggle with the limited and, let's face it, ugly, classic console windows. Windows Terminal really helps.


Part 0.8.

❞ Q: When executing a program, the console window blinks and then closes immediately.

There's a recommendation to introduce a "stop at the end" statement in the program.

This is only a problem when using an IDE. A good solution is to find out how to run a program from the particular IDE, so that the program's console window persists when the program's finished. For example, in Visual Studio use Ctrl+F5.

A "stop at the end" statement is just ungoodness: no advantage, but adds some problems.

On the positive side this part mentions that an anti-virus may interfere with program execution. That's a very common problem for beginners and it's the first time I see it covered in a tutorial. Very good.

There's also discussion of other common practical problems, and that's very good (and also uncommon in tutorials).


Part 0.10

Terminology: talks about "compiler extensions". But what's discussed are language extensions, not compiler extensions.


Part 1.1

This text, introducing statements, is fine.

But I miss in the preceding discussion mention of tabs versus spaces (tabs have problems, use spaces), and discussion of using namespace std;, which a student is bound to encounter in examples on the net and in books.

The tutorial apparently doesn't use using namespace std; and that's a valid approach (that also incidentally steers clear of controversy), but a student needs to know.


Part 1.2

Discusses commenting. In the section about commenting out code I miss discussion of the possible problems with /* comments for that, and the not uncommon idiomatic solution of #if 0.


Part 1.3

Discusses objects and variables. The first tutorial I've seen that gets the C++ notion of "object" correct.

Problem with the terminology discussion. The main problem is not that "object" in other contexts can refer to functions, as pointed out here. Rather, the main confusion is with "object" in the OO sense as instance of a class, which is the meaning students encounter in e.g. Java and C#. I had expected that to be mentioned. It isn't.


Part 1.4

About assignments and initialization. This is the first error I saw in the tutorial:

❞ Unfortunately, direct initialization can’t be used for all types of initialization (such as initializing an object with a list of data). To provide a more consistent initialization mechanism, there’s brace initialization

However, any curly braces initialization is either direct initialization or copy initialization, it's not a third form. I.e. this is a false dichotomy. Unfortunately this is not very clear in cppreference; the link I gave now goes to the relevant paragraph in the draft standard online.


Part 1.6

About missing initialization. The terminology discussion is a bit misleading, probably because the term "initialize" has multiple meanings in C++, particularly a narrow formal one related to constructors, and a more general informal one from C and programming in general. The text communicates that "initialize" has only the narrow formal meaning. That can make problems for students later on.

Not sure how this could be explained any better, though. It's complex, so perhaps pointing out that what's given is a simplified explanation that will be refined later.


Part 1.7

About naming things. I miss a short discussion of using non-English letters. Supported by the C++ standard (I believe roughly the same rules as in JavaScript), but decidedly not by all compilers, so best practice = use only the English alphabet.


Well, that's enough time used for me.

It appears to be a very good tutorial. Some small problems, but there's nothing like the void main and similar ungoodness that used to plague C++ tutorials in the past, and importantly, this tutorial addresses the practical problems that a student is likely to encounter. At least up till and including part 1.7 the information given will not lead anyone astray in the in-practice, but there are small points of terminology and classification of things.

3

u/_Decoy_Snail_ Feb 14 '22

Seconded on IDEs. They are great for linting your code and helping to avoid stupid mistakes, but they make compilation feel like magic, sometimes inseparable magic from running. Good luck passing anything to a compiler or arguments to your program as a total beginner who just saw an IDE. I think they absolutely shouldn't be recommended to beginners.

1

u/bert8128 Feb 16 '22

I recently discovered that if you start up VS and choose "Visual Studio Command Prompt", or do the equalent from the Windows menu, you can just say "cl myfile.cpp" from the command line and myfile.exe will be created (assuming nothing fancy of course, and of course your code must be valid). Perfect for building micro-projects. cl complained innitially about some things but gave the compiler switches to make the problems go away. Simples!

1

u/bert8128 Feb 14 '22

I agree that precompiles headers should be off by default but is there anything particularly non- standard? PCHs are not in the standard so presumably any other build env that uses them is equally non-standard.

PCHs are extremely useful on large projects, unless you particularly like sword fighting on chairs, so probably should be taught at some point at intermediate level.

1

u/alfps Feb 14 '22

With Visual C++ precompiled headers enabled the compiler skips source code until it finds an include for the precompiled header or a special stop skipping directive.

This non-standard silently-skip-source-code behavior means that a student who just puts the source code of some text book's multi-file example in a default VS project, will encounter baffling errors.

The errors hint that turning off precompiled headers would be a good idea, but a student who has not encountered that feature will generally just not see a reason for the errors. Who to trust, VS or the book author? Hm.

That's why it's nice that the tutorial makes a big point of turning off precompiled headers in a VS project.

1

u/bert8128 Feb 14 '22

This is a valid criticism of PCHs in VS. My point is that they are already non standard, so why would expect any particular behaviour? Beginners (and most small projects) are well advised to shy away from PCHs on any platform - they are a classic case of premature optimisation.

1

u/[deleted] Jul 18 '23

[removed] — view removed comment

1

u/alfps Jul 18 '23

In my time the first and second editions of Stroustrup's "The C++ Programming Language", later also "The Annotated Reference Manual" by Stroustrup & Ellis, plus Turbo C++ and g++ compilers. There was no standard yet and the language was changing. I learned some incorrect things (e.g. with Turbo C++ a call to a virtual function from a constructor could execute the implementation in a derived class) that I had to unlearn later.

Even Stroustrup was learning, because the language he had created was changing in basic ways. E.g. the 2nd ed. of TCPPL had at least one instance of void main, probably invalid according to the ARM which served as language reference, and at any rate invalid when C++ got standardized. He didn't at first believe it when I later mentioned it in a C++ group.

And I guess both Stroustrup and I are still learning, for the language is still changing: there is no such thing as "learning C++" and be finished.