On writing code with debugging in mind

/*************************************************
*                                                *
* .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-. *
* |                   ______                   | *
* |                .-"      "-.                | *
* |               /            \               | *
* |   _          |              |          _   | *
* |  ( \         |,  .-.  .-.  ,|         / )  | *
* |   > "=._     | )(__/  \__)( |     _.=" <   | *
* |  (_/"=._"=._ |/     /\     \| _.="_.="\_)  | *
* |         "=._"(_     ^^     _)"_.="         | *
* |             "=\__|IIIIII|__/="             | *
* |            _.="| \IIIIII/ |"=._            | *
* |  _     _.="_.="\          /"=._"=._     _  | *
* | ( \_.="_.="     `--------`     "=._"=._/ ) | *
* |  > _.="                            "=._ <  | *
* | (_/                                    \_) | *
* |                                            | *
* '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=' *
*                                                *
*    ABANDON ALL HOPE, ALL YE WHO ENTER HERE     *
*************************************************/
ASCII art image of a skull and crossbones with the text “abandon all hope, all ye who enter here” displayed below the skull and crossbones.

One factor of code quality that does not get enough attention is how easy is it to debug the code. No matter how skilled the team that writes the software, there will be bugs. When these bugs are found some poor soul will need to debug the code to determine the cause of the bug. Unfortunately this can quickly turn into a nightmare because many functions are not debuggable. Sometimes even well designed code is so difficult to debug that it causes the poor soul who has to debug it to abandon all hope.

Non-Debuggable Function

The following function is very simple. At first glance it seems to be a concise and well designed function. However, it is a major contributor to the debugging this application is a nightmare problem.


inline bool CCoolAppDocument::IsValidStartOffset(long nOffset) const
{
    return nOffset >=0 && nOffset < GetLength();
}

You may be wondering, how can this innocent, one-line function cause someone to abandon all hope?

When the function works as expected (meaning it returns the value you expect) it cannot. However, when the function returns false when you think it should return true the nightmare begins. The most critical piece of information that might explain why this function is not returning the expected value, the document length, is hidden away within a function call. As a result, there is no way you can look at parameter and variable values in the debugger watch window and know why the function is behaving in the way that it is.

Debuggable Function

The following function does exactly the same thing but it can be debugged in a microsecond.


inline bool CCoolAppDocument::IsValidStartOffset(long nOffset) const
{
    const auto documentLength = GetLength();
    return (nOffset >=0 && nOffset < documentLength);
}

All you have to do to debug this function is look at the value of the nOffset parameter and the documentLength variable in the watch window and then the reason for the unexpected behavior is immediately obvious.

In the case of today’s nightmare that inspired this article, the problem was that both the nOffset parameter and the documentLength variable happened to be equal to 296.

Additional Comments

I know that some of you are wondering whether I am aware that some debuggers will provide the return value of a function that was just called. The answer is of course I am aware of that. However, as far as I know not all debuggers support this functionality. In addition, this functionality is not as easy to use or as intuitive as the watch variable feature.

To me it just makes sense to make a very small change to make it possible to diagnose problems with the more intuitive and easier to use watch variable feature of the debugger.

Is the Novel coronavirus (COVID-19) contained in the United States?

Meme showing a dog sitting at a table. The room is on fire. The dog says 'This is fine'.
This is fine.

On March 6, 2020, President Donald Trump’s top economic adviser, Larry Kudlow, claimed that the coronavirus “is contained” in the United States. There were only 75 confirmed cases in the United States at that time.

Today there are 1,629 confirmed cases.

Now that is what I call contained! The Trump administration is obviously doing an excellent job. We should be grateful that we have such a diligent and hard working POTUS!

Sources

All hail Donald Trump; a.k.a. Donald Drumpf, a.k.a. Lord Dampnut, a.k.a. Dumbnut, a.k.a. Donnie Trumpelstiltskin, a.k.a. Donald tRUMP; our most, or perhaps least, honored (not my) POTUS and our great Bloviator in Chief!

The Trump Blue-Collar Boom — Damned Lies and the Truth

Damned Lies

President Trump hosted a group of America’s governors for a policy discussion at the White House yesterday, and the blue-collar economic boom was top on the agenda.

Source: https://www.whitehouse.gov/1600daily/ on February 11, 2020

Truth


All hail Donald Trump; a.k.a. Donald Drumpf, a.k.a. Lord Dampnut, a.k.a. Dumbnut, a.k.a. Donnie Trumpelstiltskin, a.k.a. Donald tRUMP; our most, or perhaps least, honored (not my) POTUS and our great Bloviator in Chief!

On Sondland’s impeachment testimony

How to interpret Sondland’s impeachment testimony. Hint, it is very bad news for the Republicans and Trump.

“I know that members of this committee have frequently framed these complicated issues in the form of a simple question: Was there a ‘quid pro quo?’  With regard to the requested White House call and White House meeting, the answer is yes.”

Ambassador Gordon Sondland

Translation: Yes. Trump did in fact try to commit Extortion, Bribery, and Blackmail.

Thank you Sondland.

Sonland said he acted “at the express direction of the president of the United States.”

Translation: Trump was directly involved and was calling the shots.

Again, thank you Sondland.

Sondland then said “there was no secret” and “Everyone was in the loop.”

This is completely irrelevant. If everyone involved including the POTUS, ambassadors, Sondland, and Giuliani knew that Trump was guilty of Extortion, Bribery, and Blackmail that does not make it right. He was still guilty of Extortion, Bribery, and Blackmail!

Again, thank you Sondland.

Today was a very good day, but not for Trump and his supporters.

See Also


All hail Donald Trump; a.k.a. Donald Drumpf, a.k.a. Lord Dampnut, a.k.a. Dumbnut, a.k.a. Donnie Trumpelstiltskin, a.k.a. Donald tRUMP; our most, or perhaps least, honored (not my) POTUS and our great Bloviator in Chief!

On the legitimacy of the closed-door depositions

Every time I see Republicans mention the fact that “House Intelligence Committee Chair Adam Schiff (D-CA) held secretive closed-door depositions” I want to scream. These closed-door depositions were essentially the same a a grand jury hearing.

Grand jury hearings are always secret here in the United States.

See Federal Grand Jury Secrecy: Legal Principles and Implications for Congressional Oversight for details.

The following is a key excerpt from the “Federal Grand Jury Secrecy: Legal Principles and Implications for Congressional Oversight” document.

Traditionally, the grand jury has conducted its work in secret. Secrecy prevents those under scrutiny from fleeing or importuning the grand jurors, encourages full disclosure by witnesses, and protects the innocent from unwarranted prosecution, among other things.

Federal Grand Jury Secrecy: Legal Principles and Implications for Congressional Oversight

In other words, every time a Republican brings up these secretive closed-door depositions they are just twisting the facts and trying to deceive people by claiming that the process was not legitimate when it is just how things are done here.

How to Selectively Hide or Display Elements Based on the Web Browser Using CSS


I recently encountered a situation in which a feature I wanted to add to my website was not supported in Internet Explorer because it relied on third-party JavaScript code that is not supported in Internet Explorer. The feature was trigged by pressing a button. The page was displayed properly but when the button was pressed nothing happened. The console revealed the reason why nothing happened when the button was pressed. The third-party JavaScript code uses a language feature not supported by Internet Explorer.

I decided that the simplest solution was to hide the button for Internet Explorer and display it for all other web browsers.

A quick Google search suggested that this was easy. The article How To Create an IE-Only Stylesheet suggested that all I need to do is use conditional comments as follows.


<!--[if !IE]><!-->
<p>This paragraph should be hidden in Internet Explorer and visible in all other web browsers.</p.
<!--<![endif]-->

While this does work for older versions of Internet Explorer, it does not work for Internet Explorer 10 and Internet Explorer 11 as discussed in Conditional comments are no longer supported.

After giving this some thought, I decided that it might be possible to do this using CSS, as follows.


if (IsInternerExplorer()) {
.hidden-for-ie-visible-for-others {
    display:none !important;
}
else {
.hidden-for-ie-visible-for-others {
    display:block;
}
}

The problem is that CSS does not support if/else statements.

The Quest for a Solution

I found several good resources such as How to Target Internet Explorer 10 and 11 in CSS that showed how to do the if part. It can be done as follows.


@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.hidden-for-ie-visible-for-others {
    display:none !important;
}
}

Unfortunately, the How to Target Internet Explorer 10 and 11 in CSS does not provide any information on how to do the else part. I was at a loss for how to do the else part until I thought to do a Google searc for css not Internet Explorer. This led me to the StackOverflow question Apply CSS to all browsers except IE using media query. The accepted answer suggests that the @supports rule can be used to detect all browsers except Internet Explorer as follows.


@supports not (-ms-high-contrast: none) {
   /* Non-IE styles here */
}

Unfortunately, -ms-high-contrast is not supported for older versions of Internet Explorer; it was added in Internet Explorer 10. Therefore, if we were to make use of both the @media technique and the @supports technique the hidden-for-ie-visible-for-others class would be defined for Interner Explorer 10 and Internet Explorer 11 and for most other browsers but it would not be defined for older versions of Internet Explorer.

The Solution

Fortunately, the CSS Specificity feature provides a solution. CSS properties are calculated using Precedence Rules. A simple description of these Precedence Rules is as follows.

  1. Inline css (html style attribute) overrides css rules in style tag and css file.
  2. A more specific selector takes precedence over a less specific one.
  3. Rules that appear later in the code override earlier rules if both have the same specificity.
  4. A css rule with !important always takes precedence.

Source: What is the order of precedence for CSS?

Therefore, the hidden-for-ie-visible-for-others class can be defined as follows.


/* Define the hidden-for-ie-visible-for-others class for old
versions of Interner Explorer. */
.hidden-for-ie-visible-for-others {
    display:none;
}
/* Define the hidden-for-ie-visible-for-others class for
Interner Explorer 10 and Interner Explorer 11. */
@media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
.hidden-for-ie-visible-for-others {
    display:none !important;
}
}
/* Define the hidden-for-ie-visible-for-others class for
other modern browsers. */
@supports not (-ms-high-contrast: none) {
.hidden-for-ie-visible-for-others {
    display:block !important;
}
}

Then you can use the hidden-for-ie-visible-for-others class as follows.


<p class="hidden-for-ie-visible-for-others">This paragraph should be hidden in Internet Explorer and visible in all other web browsers.</p.

The Birth of the Trump Economy

The Trump Economy
The Trump Economy

Economies are truly enormous. They are so large that as a result of the law of conservation of momentum, they take forever to stop, speed up, slow down, or change direction. For this reason, the Obama economy is just now coming to an end.

The Trump economy is a new born baby, and it does not appear to be healthy.

Trump is well aware of this. Why else would he have had his official propaganda machine, the West Wing Reads mailing list, send out the following drivel this morning?

Americans’ View of the Current Economy is the Highest in 19 Years

Americans’ assessment of current economic conditions has climbed to the highest level in nearly two decades, buoyed by a strong job market, Reade Pickert reports for Bloomberg.

He is desperately trying to distract us from the truth by shouting as loud as he can, the economy is doing great.

Perhaps Democrats should just let the Trump economy grow up; Let Donnie Trumplestiltskin commit political suicide, then offer salvation.


All hail Donald Trump; a.k.a. Donald Drumpf, a.k.a. Lord Dampnut, a.k.a. Dumbnut, a.k.a. Donnie Trumpelstiltskin, a.k.a. Donald tRUMP; our most, or perhaps least, honored (not my) POTUS and our great Bloviator in Chief!


The Trump Economy Image Information

Source
Wikimedia Commons: Baby Trump blimp
License
Creative Commons Attribution-Share Alike 4.0 International license

Using a select element to change the active stylesheet for a web page.

This article discusses the use of a select element to change the active stylesheet for a web page. It also provides step by step instructions on how to create the select element.

  • Add a link element to the head of your web page. Set the rel attribute to “stylesheet,” the href attribute to the URL of the default stylesheet you wish to use for your web page, and the id attribute to a unique value.

    Code Example:

    <link
      rel="stylesheet"
      href="https://www.w3.org/StyleSheets/Core/Swiss"
      id="W3C-Core-Styles" />
    

  • Add the select element. Set the value attribue of each option element to the URL of the stylesheet and the contents of each option element to the text you wish to be displayed for the combo box.

    Code Example:

    <select id="SeclectW3CCoreStyle">
    <option
      value="https://www.w3.org/StyleSheets/Core/Chocolate">
    Chocolate
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Midnight">
    Midnight
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Modernist">
    Modernist
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Oldstyle">
    Oldstyle
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Steely">
    Steely
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Swiss">
    Swiss
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Traditional">
    Traditional
    </option>
    <option
      value="https://www.w3.org/StyleSheets/Core/Ultramarine">
    Ultramarine
    </option>
    </select>
    

    Typically, I add this select element, along with a lable and the Set Style button, to the footer at the bottom of the page.

  • Add a Set Style button, as follows.

    Code Example:

    <input
      id="SetW3CCoreStyleButton"
      onclick="SetStyle('SeclectW3CCoreStyle', 'W3C-Core-Styles')"
      type="button"
      value="Set Style" />
    

  • Add the following JavaScript code to a script element in your page or to a .js file on your website.

    function isEmptyOrSpaces(str){
      if (str == null){
        return true;
      }
      if (str.length == 0){
        return true;
      }
      if (str.trim() === ''){
        return true;
      }
      return false;
    }
    
    function SetStyle(selectStyleElementId, linkElementId){
      console.log(
        "SetStyle('" + selectStyleElementId
        + "', '" + linkElementId
        + "') called.");
      let seclectStyleElement =  document.getElementById
        selectStyleElementId);
      if (!seclectStyleElement){
        console.log(
          "Could not find the '" + selectStyleElementId
          + "' element.");
        return false;
      }
      let seclectedStyle = seclectStyleElement.value;
      if (isEmptyOrSpaces(seclectedStyle)){
        console.log("Could not get the selected style.");
        return false;
      }
      let linkElement = document.getElementById(linkElementId);
      if (!linkElement){
        console.log(
          "Could not find the '" + linkElementId
          + "' element.");
        return false;
      }
      linkElement.setAttribute("href", seclectedStyle);
      return true;
    }
    

This example is fully functional. It does lack one key feature, the ability to store the selected style in a cookie. The article Store and Retrieve Cookies demonstrates the use of the localStorage.setItem and localStorage.getItem functions.

Modify the SetStyle function by adding the folloing line just above the ‘return true;’ line at the bottom of the function.


window.localStorage.setItem(
  "SeclectedStyle", seclectedStyle);

Note that the localStorage object is only supported on modern web browsers. That said, the Local storage with Window.localStorage page provides JavaScript code to emulate the feature for older browsers.

The next step is to set the style and the selected item in the select element when the web page loads. To acomplish this you need to add the following function.


function SetStyleFromCookie(selectStyleElementId, linkElementId){
  console.log(
    "SetStyleFromCookie('" + selectStyleElementId + "', '" + linkElementId + "') called.");
  let seclectedStyle = window.localStorage.getItem(
    "SeclectedStyle");
  if (isEmptyOrSpaces(seclectedStyle)){
    console.log(
      "Failed to get the value of the 'SeclectedStyle' cookie. Using the default value.");
    seclectedStyle = 'https://www.w3.org/StyleSheets/Core/Swiss';
  }
  let linkElement = document.getElementById(linkElementId);
  if (!linkElement){
    console.log("Could not find the '" + linkElementId + "' element.");
    return false;
  }
  linkElement.setAttribute("href", seclectedStyle);
  let seclectStyleElement =  document.getElementById(selectStyleElementId);
  if (!seclectStyleElement){
    console.log("Could not find the '" + selectStyleElementId + "' element.");
    return false;
  }
  seclectStyleElement.value = seclectedStyle;
  return true;
}

Finally, use the following code to call the SetStyleFromCookie function when the web page is loaded.


window.addEventListener('DOMContentLoaded', (event) => {
  SetStyleFromCookie('SeclectW3CCoreStyle', 'W3C-Core-Styles');
});

References

To see this code in action, go to Using a select element to change the active stylesheet for a web page.

The Undeniable Truth About the Crisis at the Border

One thing that has not been discussed enough is that the current immigration crisis was created by the United States. The United States has been taking actions that have destabilized Central America for the past 100 years.

In addition, the migrants have been mischaracterized by the Republican party as illegal immigrants. They are not. The asylum laws say you have to come here to the United States before you can ask for asylum. There is no possible way of asking for asylum while outside of the United States.

I know what some of you are no doubt saying right now; “you are supposed to seek asylum at a border crossing.” That is exactly what they are trying to do.

Asylum seekers are being turned away at the border, and have been all along. The actions of the Trump administration is in violation of international law.

These legal seekers of asylum are being forced to wait at the border for weeks or even months in a dangerous situation until they have no choice to cross the border without permission in the hopes that they can try to request asylum again once they have crossed the border.

So lets review.

  1. The United States has spent 100 years destabilizing Central America.
  2. Things have finally become so bad in Central America that people are fleeing to seek a better life elsewhere.
  3. You cannot request asylum until you are here in the United States.
  4. When the legal asylum seekers arrive at the border they are being turned away illegally.
  5. These legal asylum seekers are left with no choice but the cross the border without permission since they are being turned away illegally at border crossings so they can try to request asylum again once they are here.

If the Democrats want to force Trump out of office, they need to focus on making these truths known to all.


All hail Donald Trump; a.k.a. Donald Drumpf, a.k.a. Lord Dampnut, a.k.a. Dumbnut, a.k.a. Donnie Trumpelstiltskin, a.k.a. Donald tRUMP; our most, or perhaps least, honored (not my) POTUS and our great Bloviator in Chief!