CS253: Software Development with C++

Spring 2020

Heckendorn

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.

More Information

There’s always more information that you can provide:

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