I have a winform application in vb.net and am developing a Rock Paper Scissor game.
I use enum in one place and string in another to represent some an weapon type as shown below:
ENUM example:
Imports RockPaperScissors.Weapon
Public Class PlayerComputerRandom
Inherits Player
Private Enum weaponsList
Rock
Paper
Scissors
End Enum
Public Overloads Sub pickWeapon()
Dim randomChoice = New Random()
Dim CompChoice As Integer = randomChoice.Next(0, [Enum].GetValues(GetType(weaponsList)).Length)
If CompChoice = "0" Then
pWeapon = New Rock()
ElseIf CompChoice = "1" Then
pWeapon = New Paper()
Else
pWeapon = New Scissors()
End If
End Sub
End Class
STRING example:
Public Class Player
Public pWeapon As Weapon
Public Sub pickWeapon(ByVal WeaponType As String)
If WeaponType = "Rock" Then
pWeapon = New Rock()
ElseIf WeaponType = "Paper" Then
pWeapon = New Paper()
Else
pWeapon = New Scissors()
End If
End Sub
End Class
Can you tell me the advantages and disadvantages of each approach? Im new to OOP and coding so I just want to what the issue is with this approach and what would be the better approach?
Any help would be greatly appreciated.
Many Thanks,
If you are going to use strings, you should at least use constants. Using string literals everywhere is just a bad idea because you can easily type something wrong. By always using constants, at least you can avoid that issue. Secondly, by using constants, it is easy to change the string value, if you ever need to, or to find all the places in the code where the constant is used:
However, the problem still exists that it is not at all obvious, when reading the code, what all of the possible values are. This can be further alleviated by grouping all the string constants into a class so that it essentially works like an enum, like this:
Then, you could use the constants like this:
However, the code is still more confusing than it needs be, in that, when you call the
pickWeaponmethod, all it tells you is that you need to pass it a string. So, to make it clear, you would need to add a comment that explains that you must pass one of the constants in theWeaponsclass.It’s always better, when possible, to have the code be self-documenting. When you ask for the enum, you know immediately what possible values the method expects, so there is no need to add a comment to that affect.
Additionally, since everywhere you want to set or compare the value, you want to use the constant instead of the literal, it makes no difference to the rest of the code what that actual value is. Rock could equal “Rock”, or “ROCK”, or “Hey Look, I’m a String!”. As long as everywhere in the code uses the constant, it will still work. So, if that’s the case, then there’s really no advantage to having a string over just using an integer value in an enum.
There are some times where a string constant does make more sense. For instance, if you are saving the value to a database, and the value in the database is more useful as a human-readable value, it may make sense to use “Rock” instead of 0. But as long as you don’t have any extenuating circumstances where you need it to be a string, I would definitely recommend sticking to using the enumeration for all of the above reasons.