Reducing cyclomatic complexity in a simple string concatenation I have written the following function that takes an address object and builds a string using the object properties. It will later be used to display a tooltip

var getAddressTooltip = function (address) {

    var tooltip = '';

    if (address.MasterAddress !== undefined &amp;&amp;
        address.MasterAddress !== null &amp;&amp;
        address.MasterAddress.Id !== undefined &amp;&amp;
        address.MasterAddress.Id !== null) {
        tooltip += '[' + address.MasterAddress.Id + '] ';
    }
    if (address.PurchasingContact !== null &amp;&amp;
        address.PurchasingContact !== undefined &amp;&amp;
        address.PurchasingContact.Name !== null &amp;&amp;
        address.PurchasingContact.Name !== undefined &amp;&amp;
        address.PurchasingContact.Name.length &gt; 0) {
        tooltip += $.trim(address.PurchasingContact.Name) + ',\n';
    }
    if (address.ShipTo !== null &amp;&amp; address.ShipTo !== undefined &amp;&amp; address.ShipTo.length &gt; 0) {
        tooltip += $.trim(address.ShipTo) + ',\n';
    }
    if (address.Line1 !== null &amp;&amp; address.Line1 !== undefined &amp;&amp; address.Line1.length &gt; 0) {
        tooltip += $.trim(address.Line1) + ',\n';
    }
    if (address.Line2 !== null &amp;&amp; address.Line2 !== undefined &amp;&amp; address.Line2.length &gt; 0) {
        tooltip += $.trim(address.Line2) + ',\n';
    }
    if (address.City !== null &amp;&amp; address.City !== undefined &amp;&amp; address.City.length &gt; 0) {
        tooltip += $.trim(address.City) + ',\n';
    }
    if (address.State !== null &amp;&amp;
        address.State !== undefined &amp;&amp;
        address.State.Name !== null &amp;&amp;
        address.State.Name !== undefined &amp;&amp;
        address.State.Name.length &gt; 0) {
        tooltip += $.trim(address.State.Name) + ',\n';
    }
    if (address.ZIP !== null &amp;&amp; address.ZIP !== undefined &amp;&amp; address.ZIP.length &gt; 0) {
        tooltip += $.trim(address.ZIP) + ',\n';
    }

    //replace trailing comma / line break
    if (tooltip.substr(tooltip.length - 2, 2) === ',\n') {
        tooltip = tooltip.substr(0, tooltip.length - 2);
    }

    return tooltip;
};


As you can probably tell, I'm rather fastidious about ensuring the values are there before concatenating them, as we have had problems in the past with properties coming back as undefined and bringing the whole application crashing to a halt. 

However, the cyclomatic complexity of this function is through the roof because of all the null / undefined / length &gt; 0 checks that I'm doing. It is entirely possible that I'm taking things too far.

What I'm wondering is if anybody knows a faster, smarter and tidier way of doing the same thing? Do I really need all these checks? It appears that if I want valid, safe and linted JavaScript, all of them are necessary.
What about this:

var getAddressTooltip = function (address) {

    var tooltip = '';

    if (address.MasterAddress &amp;&amp;
        address.MasterAddress.Id) {
        tooltip += '[' + address.MasterAddress.Id + '] ';
    }
    if (address.PurchasingContact &amp;&amp;
        address.PurchasingContact.Name) {
        tooltip += $.trim(address.PurchasingContact.Name) + ',\n';
    }
    if (address.ShipTo) {
        tooltip += $.trim(address.ShipTo) + ',\n';
    }
    if (address.Line1) {
        tooltip += $.trim(address.Line1) + ',\n';
    }
    if (address.Line2) {
        tooltip += $.trim(address.Line2) + ',\n';
    }
    if (address.City) {
        tooltip += $.trim(address.City) + ',\n';
    }
    if (address.State &amp;&amp;
        address.State.Name) {
        tooltip += $.trim(address.State.Name) + ',\n';
    }
    if (address.ZIP) {
        tooltip += $.trim(address.ZIP) + ',\n';
    }

    //replace trailing comma / line break
    if (tooltip.substr(tooltip.length - 2, 2) === ',\n') {
        tooltip = tooltip.substr(0, tooltip.length - 2);
    }

return tooltip; };


Since JavaScript considers null, undefined, and the empty string to be falsy, the value of the variable itself can be used to substitute for all the tests. Further, as any object is automatically truthy, the tests like address.PurchasingContract will be true if the object is there.

References:

MDN: A Reintroduction to JavaScript (just the first thing I came across)