Reduce (decrease) time complexity of foreach loop in ASP.Net

Last Reply 16 days ago By dharmendr

Posted 17 days ago

Hello Everyone,

There is one function in c# for console application where our company went for sonar qube scan and it resulted cognitive complexity of 23 to 15.

Can you please provide me any solution to decrease the complexity to avoid more loops, if conditions and continue statements. 

private void ChildPopulateAssetSettlementHash1(ArrayList lstFieldsAHV, Hashtable fieldPositionsHash_AHV, string agreementId, string securityID, Hashtable AHV_Hash)
{
    foreach (string[] fields_AHV in lstFieldsAHV)
    {
        if (fieldPositionsHash_AHV.Contains("Region"))
        {
            string aRegion = fields_AHV[(int)fieldPositionsHash_AHV["Region"]];
            bool isRegionSkip = IsRegionSkip(aRegion);
            if (isRegionSkip)
                continue;
        }
        string agreementID = fields_AHV[(int)fieldPositionsHash_AHV["Agreement Ext Id"]];
        string instrumentID = fields_AHV[(int)fieldPositionsHash_AHV["Instrument ID"]];
        if (agreementID.Equals(agreementId) && securityID.Equals(instrumentID))
        {
            if (AHV_HASH.ContainsKey(agreementId) && !(((Hashtable)AHV_Hash[agreementId]).ContainsKey(instrumentID)))
            {
                ((Hashtable)AHV_Hash[agreementID])[instrumentID] = fields_AHV;
            }
            else if (AHV_Hash.ContainsKey(agreementId))
            {
                continue;
            }
            else
            {
                AHV_Hash[agreementId] = new Hashtable();
                ((Hashtable)AHV_Hash[agreementId])[instrumentID] = fields_AHV;
            }
            if (agreementID.Equals(agreementId) && !securityID.Equals(instrumentID))
            {
                if (AHV_HASH.ContainsKey(agreementId) && !(((Hashtable)AHV_Hash[agreementId]).ContainsKey(securityID)))
                {
                    ((Hashtable)AHV_Hash[agreementID])[securityID] = fields_AHV;
                }
                else if (AHV_Hash.ContainsKey(agreementId))
                {
                    continue;
                }
                else
                {
                    AHV_Hash[agreementId] = new Hashtable();
                    ((Hashtable)AHV_Hash[agreementId])[instrumentID] = fields_AHV;
                } 
            }
        } 
    }
}

 

Posted 16 days ago Modified on 15 days ago

Refer below code.

private void ChildPopulateAssetSettlementHash1(ArrayList lstFieldsAHV, Hashtable fieldPositionsHash_AHV, 
                                                string agreementId, string securityID, Hashtable AHV_Hash)
{
    foreach (string[] fields_AHV in lstFieldsAHV)
    {
        if (fieldPositionsHash_AHV.Contains("Region"))
        {
            string aRegion = fields_AHV[(int)fieldPositionsHash_AHV["Region"]];
            bool isRegionSkip = IsRegionSkip(aRegion);
            if (!isRegionSkip)
                break;
        }
        string agreementID = fields_AHV[(int)fieldPositionsHash_AHV["Agreement Ext Id"]];
        string instrumentID = fields_AHV[(int)fieldPositionsHash_AHV["Instrument ID"]];
        if (agreementID.Equals(agreementId) && securityID.Equals(instrumentID))
        {
            if (AHV_HASH.ContainsKey(agreementId) && !(((Hashtable)AHV_Hash[agreementId]).ContainsKey(instrumentID)))
            {
                ((Hashtable)AHV_Hash[agreementID])[instrumentID] = fields_AHV;
            }
            else if (!AHV_Hash.ContainsKey(agreementId))
            {
                AHV_Hash[agreementId] = new Hashtable();
                ((Hashtable)AHV_Hash[agreementId])[instrumentID] = fields_AHV;
                break;
            }
        }

        if (agreementID.Equals(agreementId) && !securityID.Equals(instrumentID))
        {
            if (AHV_HASH.ContainsKey(agreementId) && !(((Hashtable)AHV_Hash[agreementId]).ContainsKey(securityID)))
            {
                ((Hashtable)AHV_Hash[agreementID])[securityID] = fields_AHV;
            }
            else if (!AHV_Hash.ContainsKey(agreementId))
            {
                AHV_Hash[agreementId] = new Hashtable();
                ((Hashtable)AHV_Hash[agreementId])[instrumentID] = fields_AHV;
                break;
            }
        }
    }
}