CS253: Software Development with C++

Spring 2019

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 <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

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 <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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-12-29T15:27

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building