Werner sent us some code from the telcom industry. Before we even get to the code, we have to look at one of the comments.

// This is a hack to be
// able to compile

I might not need to say this, but if you feel like you need to trick the compiler into accepting your code, you may need to rethink your overall design. Then again, when we look at the code in context, the comment makes less sense:

mainserver = null;
mainserver = new WASPApplicationServer();       // This is a hack to be
                                    // able to compile
try {
        mainserver = null;
        mainserver = new WASPApplicationServer();
        noprob = true;
} catch (Exception eee) {
        msg = "There was a problem initializing the server. Log Trap and try again";
        logger.error(msg, eee);
        msg = msg + "\n" + eee;
        snmpev.sendSNMPcritical(moClass, moObjInst, "SERVER: INITIALIZATION", "Unknown Cause",
        "Failure to initialize new WASP Server", msg, msg);
        noprob = false;

The “hack” is 100% unnecessary. Worse, if creating a WASPApplicationServer needs to throw exceptions, then doing it outside of the try is completely self-defeating.

The real treat here, though, isn’t the comment or the “hack”, but the humble little noprob flag. Instead of using exceptions to represent, well, exceptional states and error modes that need to be handled, complete with internal state and a full stack trace about what happened, when and why, this throws all of that away and just sets a noprob flag.

Downstream code checks the flag, and if it’s false, terminates the application.

Werner removed the “hack”, but doesn’t know who to blame, as the problem code predates their migration from CVS to SVN back in 2011, and they didn’t migrate history.

[Advertisement] ProGet supports your applications, Docker containers, and third-party packages, allowing you to enforce quality standards across all components. Download and see how!