[RESOLVED] Quick question on which code is more efficent.
I recently re wrote a small piece of code, and was wondering if I made the code better or worse when I changed it.
Orignal Code:
Code:
Select Case wcoCoverage.LevelID 'Level ID = Policy or Vehicle Id's
Case wcoWP_CNAuto.ID
Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, False)
Case Else 'Vehicle id
If Not p_dictdatVehicles.ContainsKey(wcoCoverage.LevelID) Then Continue For
If swcoCoverageCode = "VFTP" Then 'Vehicle Limits Total Premium
Dim datVehicle As DILCCA.Vehicle = p_dictdatVehicles(wcoCoverage.LevelID)
Dim decPreValue As Decimal = GetCoveragePremium(wcoCompany, wcoCoverage.ID)
If decPreValue <> Decimal.Zero Then datVehicle.TotalPremiumPrincipal = decPreValue.ToASCurrency
Continue For
End If
Select Case True
Case lstCodesSurcharges.Contains(swcoCoverageCode)
Update_CNAutoSurcharges(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedSurchargesIds, swcoCoverageCode)
Case lstCodesDiscounts.Contains(swcoCoverageCode)
Update_CNAutoDiscounts(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedDiscountsIds, swcoCoverageCode)
Case dictVehicleLimitCodeandIds.ContainsValue(swcoCoverageCode)
Update_CNAutoVehicleLimits(datApplicationDetail, wcoCoverage, wcoCompany, dictVehicleLimitCodeandIds, swcoCoverageCode)
Case lstCodesPolicyChangeForms.Contains(swcoCoverageCode)
Update_CNAutoPolicyChangeForm(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedPolicyFormsIds, sIssusingstate, swcoCoverageCode)
Case Else 'Adds to (Auto->Vehicle->Optional Add Coverages/OPCF)
Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, True)
End Select
End Select
New code:
Code:
Select Case True
Case wcoWP_CNAuto.ID = wcoCoverage.LevelID 'Policy Level
Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, False)
Case p_dictdatVehicles.ContainsKey(wcoCoverage.LevelID) 'Vehicle Level (Will be in Dictionary)
Select Case True
Case swcoCoverageCode = "VFTP"
Dim datVehicle As DILCCA.Vehicle = p_dictdatVehicles(wcoCoverage.LevelID)
Dim decPreValue As Decimal = GetCoveragePremium(wcoCompany, wcoCoverage.ID)
If decPreValue <> Decimal.Zero Then datVehicle.TotalPremiumPrincipal = decPreValue.ToASCurrency
Case lstCodesSurcharges.Contains(swcoCoverageCode)
Update_CNAutoSurcharges(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedSurchargesIds, swcoCoverageCode)
Case lstCodesDiscounts.Contains(swcoCoverageCode)
Update_CNAutoDiscounts(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedDiscountsIds, swcoCoverageCode)
Case dictVehicleLimitCodeandIds.ContainsValue(swcoCoverageCode)
Update_CNAutoVehicleLimits(datApplicationDetail, wcoCoverage, wcoCompany, dictVehicleLimitCodeandIds, lstUsedVehicleLimitsIds, swcoCoverageCode)
Case lstCodesPolicyChangeForms.Contains(swcoCoverageCode)
Update_CNAutoPolicyChangeForm(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedPolicyFormsIds, sIssusingstate, swcoCoverageCode)
Case Else
Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, True)
End Select
End Select
This code basically sits in a for loop, and it first determines if the data belongs to a Policy or a Vehicle, If it's a policy there is only one place it can go, if it's a Vehicle then I determine what the code is and put it in the correct spot.
What I was wondering, would I have been better to do a:
A) IF Statment, Then use a Case in the else part
B) Used two if statements
C) Should just stuck with the orignal code?
D) It doesn't matter?
Are using Select Case True good coding? I've read they are but I've also read that they are not, I understand it's everone personally choice..
The goal is just to make it the most efficent, so I was just curious if I handled it correctly.
Thanks
Re: Quick question on which code is more efficent.
The answer is D. Between those two, the efficiency difference won't amount to anything in a million iterations. Select case may be less efficient in certain situations, and more efficient in others, but it really doesn't make a difference.
Re: Quick question on which code is more efficent.
Wow, I never knew you could use Select Case like that. Learned something new here today.
Re: Quick question on which code is more efficent.
There shouldn't be any measurable difference in the runtime performance, but it's your debug-time performance that matters here. Readability, in other words. I prefer the second code from that viewpoint. I see nothing wrong with Select Case True because I find it easier to read than If with a large number of ElseIfs. However, since the outer Select Case only contains two options you might consider this alternative:
Code:
If wcoWP_CNAuto.ID = wcoCoverage.LevelID Then 'Policy Level
Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, False)
ElseIf p_dictdatVehicles.ContainsKey(wcoCoverage.LevelID) Then 'Vehicle Level (Will be in Dictionary)
Select Case True
Case swcoCoverageCode = "VFTP"
Dim datVehicle As DILCCA.Vehicle = p_dictdatVehicles(wcoCoverage.LevelID)
Dim decPreValue As Decimal = GetCoveragePremium(wcoCompany, wcoCoverage.ID)
If decPreValue <> Decimal.Zero Then datVehicle.TotalPremiumPrincipal = decPreValue.ToASCurrency
Case lstCodesSurcharges.Contains(swcoCoverageCode)
Update_CNAutoSurcharges(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedSurchargesIds, swcoCoverageCode)
Case lstCodesDiscounts.Contains(swcoCoverageCode)
Update_CNAutoDiscounts(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedDiscountsIds, swcoCoverageCode)
Case dictVehicleLimitCodeandIds.ContainsValue(swcoCoverageCode)
Update_CNAutoVehicleLimits(datApplicationDetail, wcoCoverage, wcoCompany, dictVehicleLimitCodeandIds, lstUsedVehicleLimitsIds, swcoCoverageCode)
Case lstCodesPolicyChangeForms.Contains(swcoCoverageCode)
Update_CNAutoPolicyChangeForm(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedPolicyFormsIds, sIssusingstate, swcoCoverageCode)
Case Else
Update_CNAutoAdditionalCoverages(datApplicationDetail, wcoCoverage, wcoCompany, lstUsedAdditionalCoveragesIDs, swcoCoverageCode, True)
End Select
End If
Re: Quick question on which code is more efficent.
I just wanted to thank everyone for there input on the above code.