Debugging and Logging are two valuable tools for isolating problems and errors in software. While debugging allows you to step-through currently executing code and get a “behind the scenes” view of the environment, logging is used as an audit trail to help find what happened when the code was executed.
Obviously, debugging is best for tracking down problems that are easily reproducible in a development environment, whereas logging is good for intermittent or unexpected problems. That is, “obviously” to everyone but Eric Polino’s coworker, Christophe.
Christophe is on a one-man crusade to eliminate the need for debugging. “If you simply log everything you do,” he’d often say, “then you can simply look at the logs to see where things went wrong. This has the benefit of working in production as well.” And what does “log everything” mean? Following is Christophe’s ideal code, and a pattern he wants to make the standard.
// assign variables logFile.info("START: Assigning variables."); logFile.debug("Getting particular drug information."); // gets drugs brandNameDrugs = Utils.formatDrugList(dl.getDrugsByLabel(calculateBean.getDrugName())); logFile.debug("Getting major classifications."); // get all major classifications (usage) majorClassifications = cl.getMajorClassifications(); logFile.debug("Getting classifications."); // get all classifications classifications = cl.getClassifications(); logFile.debug("Getting all classifications."); // gets all the major classifications and classifications allClassifications = cl.getAllClassifications(); logFile.info("END: Assigning variables."); // assign variables to session for search.jsp logFile.info("START: Assigning variables to the session."); logFile.debug("Storing brandNameDrugs to the session."); request.setAttribute("brandNameDrugs", brandNameDrugs); logFile.debug("Storing majorClassifications to the session."); request.setAttribute("majorClassifications", majorClassifications); logFile.debug("Storing classifications to the session."); request.setAttribute("classifications", classifications); logFile.debug("Storing allClassifications to the session."); request.setAttribute("allClassifications", allClassifications); logFile.info("END: Assigning variables to the session."); // search for drug by label to calculate logFile.info("Searching for drug."); logFile.debug("Drug Name: " + calculateBean.getDrugName()); Vector drugs = Utils.formatDrugList(dl.getDrugsByLabel(calculateBean.getDrugName())); // if drug is found, calculate the drugs in relate to the generic and therapeutic drugs. logFile.info("Verify results from drug search."); logFile.debug("Drug Search Results: " + drugs.size()); if(drugs.size() > 0) { logFile.info("Get first result from drug search results"); // grab first element, because getDrugsByLabel returns a Vector of possible drugs from getDrugName() Drug drug = (Drug) drugs.get(0); logFile.info("Verify for Tier-1"); if (drug.getTier().equals("Tier-1")) { logFile.info("Get all Brand Name Equivalent Drugs by GCN Number."); logFile.debug("GCNNumber: " + drug.getGcnNumber()); calculateEquivalentResults = Utils.formatDrugList(dl.getBrandNameEquivalentDrugs(drug.getGcnNumber())); } else { logFile.info("Get all Generic Equivalent Drugs by GCN Number."); logFile.debug("GCNNumber: " + drug.getGcnNumber()); calculateEquivalentResults = Utils.formatDrugList(dl.getGenericEquivalentDrugs(drug.getGcnNumber())); } logFile.info("Get all Therapeutic Alternative Drugs by GPI6 number."); logFile.debug("GPI6: " + drug.getGpi6()); calculateTherapeuticResults = Utils.formatDrugList(dl.getTherapeuticAlternativeDrugs(drug.getGpi6())); //assign to session variables to display in search.jsp. //If there is nothing in these variables, nothing will be displayed. logFile.info("START: Assigning session variables."); logFile.debug("Assigning Equivalent Drugs to the session."); request.setAttribute("calculateEquivalentResults", calculateEquivalentResults); logFile.debug("Assigning Therapeutic Drugs to the session."); request.setAttribute("calculateTherapeuticResults", calculateTherapeuticResults); logFile.debug("Assigning search drug results."); request.setAttribute("calculateDrug", drug); logFile.info("END: Assigning session variables."); } else { // if the drug being calculated cannot be found, a no results erro will display //logFile.error("No Results."); errors.add("calculate", new ActionMessage("errors.calculate.noresults")); }
Since the department is governed by meritocracy, Christophe’s eight years makes him a “Senior” Developer and, therefore, correct.