Is it bad practice to store certain values as strings

programming practicesstrings

That's a very vague title but I couldn't think of a better way to word it. But, just as an example, think about the direction that a character in a game is moving in. It just feels kind of wrong to be using a string and then doing things like if(character.direction == "left"). It seems to me that it leaves too much room for silly errors, like accidentally using Left or l or whatever instead of left. Are my suspicions correct? If so, what's the preferred way of achieving something like this?

Best Answer

If the language you are using supports the use of enums, I'd use them. It allows you to limit the number of options available for a given type. e.g. in Java:

public enum Direction {
    LEFT,
    RIGHT,
    UP,
    DOWN
}