Show Lecture.Heckendorn as a slide show.
CS253 Heckendorn
Heckendorn’s Rule
Heckendorn’s Rule is based on a saying of the Computer Scientist
Robert Heckendorn:
Never emit a constant error message
Example
#include <fstream> // for ifstream
#include <iostream> // for cerr
#include <string> // for ifstream
using namespace std;
int main() {
const string pwfile = "/etc/shadow";
ifstream in(pwfile);
if (!in) {
cerr << "Bad file\n";
return 1;
}
}
Bad file
Frustration
Imagine the frustration of the user who reads this uninformative message.
- Which file is bad?
- What was wrong with it?
- What was the program trying to do with the file?
- What program was running, anyway?
More Information
There’s always more information that you can provide:
- program name from
argv[0]
- filename
- operation attempted (open for reading, open for writing, deletion, rename)
- failure code via strerror or perror
Example
#include <fstream> // for ifstream
#include <iostream> // for cerr
#include <string> // 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 e = errno; // cerr might change errno
cerr << argv[0] << ": can’t open shadow password file "
<< pwfile << " for read: " << strerror(e) << '\n';
return 1;
}
}
./a.out: can’t open shadow password file /etc/shadow for read: Permission denied