|
This post originated from an RSS feed registered with .NET Buzz
by Raymond Lewallen.
|
Original Post: Refactoring pattern quiz number 1
Feed Title: Raymond Lewallen
Feed URL: /error.htm?aspxerrorpath=/blogs/raymond.lewallen/rss.aspx
Feed Description: Patterns and Practices, OOP, .Net and Sql
|
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Raymond Lewallen
Latest Posts From Raymond Lewallen
|
|
Here is a pattern I'm sure we all see quite a bit. Nothing wrong
with it really. Small, clean, simple. However, when you take into
context the fact that the following code is part of an entire program
used to manage carnival rides, how would you refactor the following
code? No need to post code, an explanation will do. I'll post the
answer and the code in a little while.
Part of a carnival program
Public Class Foo
Private baseTokenAmount As Int32 = 1
Public Function GetNumberOfRequiredTokens(ByVal typeOfPerson As PersonType) As Int32
Select Case typeOfPerson
Case PersonType.Infant
Throw New Exception("Too young to ride")
Case PersonType.Child
Return baseTokenAmount
Case PersonType.Adolescent
Return baseTokenAmount * 2
Case PersonType.Adult
Return baseTokenAmount * 3
Case PersonType.Senior
Throw New Exception("Too old to ride")
End Select
End Function
Public Enum PersonType
Infant
Child
Adolescent
Adult
Senior
End Enum
End Class
Read: Refactoring pattern quiz number 1