Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add elseif to switch case code fix #182

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add elseif to switch case code fix #182

wants to merge 2 commits into from

Conversation

dicko2
Copy link
Contributor

@dicko2 dicko2 commented Oct 8, 2023

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant