Introduce Explaining Variable Example

Top  Previous  Next

Here's a hypothetical example from a resort billing application:

 

 

summer1

 

It's pretty clear that the long set of conditions in the If clause could be replaced by something clearer.

 

Here's the code for cutting and pasting, if you wish:

 

Public Function getRate(startDate As Date) As Single

 

'calculate summer rates

If (Month(startDate) = 7) Or (Month(startDate) = 8) _

   Or ((Month(startDate) = 9) And (Day(startDate) < 21)) _

   Or ((Month(startDate) = 6) And (Day(startDate) >= 21)) _

   Then

    getRate = summerRate

Else

    getRate = offSeasonRate

End If

 

End Function