4 ms·
I teach a C++ class, and I can assure you that one of the most frequent errors I see in students' codes is to copy-and-paste a header file into another and modi
by ziotom78 4y ago
I teach a C++ class, and I can assure you that one of the most frequent errors I see in students' codes is to copy-and-paste a header file into another and modify it instead of writing a new one from scratch (beats me why they think this is efficient), forgetting to change the name of the include guard or only modifying the #ifdef part but not the #define part.
Since a few years I teach them only to use #pragma once, and the problem has disappeared.
- pif 4y ago> the problem has disappeared. No, it did not. You have empirically found a wonderful way to teach your students that their errors are their responsibility, and you are wilfully giving it up and sort of spoiling them. Extrapolating a bit, you are helping them join the idiots who complain about undefined behaviour while proudly showing their horribly wrong code.
- jhanschoo 4y agoIn the absence of an IDE to manage include guards, they are pointless busywork that make refactoring harder. The future solution would be C++ modules, but in absence of that pragma once is a lifesaver.
- ziotom78 4y agoYes, it did. The problem has literally disappeared. I believe that I can teach more interesting ways to catch and prevent errors than that stuff. I find that #ifdef/#define errors are boring and not interesting. The #ifdef/#define errors are due to the archaic way to literally include C/C++ files one into another. By following your reasoning, one would «spoil» their own students and making them «idiots» just by using a language with a proper module system (Rust, Python, Pascal, Ada, Nim…). Moreover, suggesting to use "#pragma once" means to teach them that you can use the features of the language at your advantage. It is silly to use X if it is error prone when there is Y that is simpler and prevents you from doing that kind of error. I am teaching them that it is worth to dig in the specification of your language and check for possible alternatives of doing X. I'll give you a hint. Please should stop teaching your students how to write Makefiles just because you want to let them know that a tab at the beginning of a line is different than eight spaces, seriously. Make them learn CMake and use the time in your class to teach more useful stuff about dependencies, feature discoverability, etc. Your students will be eternally grateful.
- pif 4y agoI'm not advocating for keeping pragma once a secret: just let them use the historical way once, fall into the beginner error, and let them notice how dangerous copy/paste can be.