Show Lecture.Heckendorn as a slide show.
Heckendorn’s Rule is based on a saying of the Computer Scientist Robert Heckendorn:
Never emit a constant error message
#include <iostream> #include <fstream> // for ifstream using namespace std; int main() { const string pwfile = "/etc/shadow"; ifstream in(pwfile); if (!in) { cerr << "Bad file\n"; return 1; } return 0; }
Bad file
Imagine the frustration of the user who reads this uninformative message.
There’s always more information that you can provide:
argv[0]
#include <iostream> // for cerr #include <fstream> // for ifstream #include <cerrno> // for errno #include <cstring> // for strerror using namespace std; int main(int /* argc */, char *argv[]) { const string pwfile = "/etc/shadow"; ifstream in(pwfile); if (!in) { const auto save_errno = errno; // cerr might change errno cerr << argv[0] << ": can’t open shadow password file " << pwfile << " for reading: " << strerror(save_errno) << '\n'; return 1; } return 0; }
./a.out: can’t open shadow password file /etc/shadow for reading: Permission denied