CS253: Software Development with C++

Spring 2023

Heckendorn

Show Lecture.Heckendorn as a slide show.

CS253 Heckendorn

Heckendorn’s Rule

Heckendorn’s Rule is based on a rule of the Computer Scientist Robert Heckendorn:

Never emit a constant error message

Example

#include <fstream>    // for ifstream
#include <iostream>   // for cerr
#include <string>     // for string

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 string
#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 reading: " << strerror(e) << '\n';
        return 1;
    }
}
./a.out: can’t open shadow password file /etc/shadow for reading: Permission denied