AgodaAnalyzers icon indicating copy to clipboard operation
AgodaAnalyzers copied to clipboard

Add elseif to switch case code fix

Open dicko2 opened this issue 1 year ago • 0 comments

The reasoning behind this one is around making code more readable.

I'm note sure if you can outright say the elseif is always bad. But many cases where we see it used there's multi else ifs conditions that result in the same result.

In these cases when using a switch(true) you can group multi cases together. This makes it clearer for the people reading the code which conditions will create the same result.

if (!mandatorySurcharge && !collectedByHotel)
    {
		cmsId = CmsId.RatePerNightOne;
	}
	else if (!mandatorySurcharge && collectedByHotel && dmcId == DmcId.YCS)
	{
		cmsId = CmsId.RatePerNightTwo;
	}
	else if (mandatorySurcharge && !collectedByHotel)
    {
        cmsId = CmsId.RatePerNightOne;
    }

// vs this alternative 

switch (true)
{
    case (!mandatorySurcharge && !collectedByHotel):
    case (mandatorySurcharge && !collectedByHotel):
        cmsId = CmsId.RatePerNightOne;
        break;
    case (!mandatorySurcharge && collectedByHotel && dmcId == DmcId.YCS):
        cmsId = CmsId.RatePerNightTwo;
        break;
    default:
        // Handle any other cases here
        break;
}

dicko2 avatar Oct 08 '23 07:10 dicko2