Let me think out loud here a little and get some opinions. On what? On how far to go when naming things in programs. let me start at the beginning. The other day I decided to code up a game of “Rock Paper Scissors.” Perhaps latter to develop into the more geeky “Rock Paper Scissors Lizard Spock.” I’m doing this as a demo of the Select/Case structure in Visual Basic and the switch/case structure in C# along with a little compound if/then/else stuff. It’s a common enough project and simple enough that when I started I let the default object names stand.
I mean after all how hard is it to remember that RadioButton1 is the option for Rock, RadioButton2 is the option for Paper, and RadioButton3 is the option for Scissors? Actually not to bad. But what number value stands for the computer picking each of those options? And I wanted to select a number to represent the outcome of the comparison to make displaying the result. OK so if RadioButton1 is the player picking Rock then 1 is the computer picking 1. Right? Ah, but then I have to set the minimum as well as maximum because otherwise I have to remember to subtract 1 so that radioButon1 and 0 sort of go together. Ah, forget about it I don’t want to think that hard – rename the silly radio buttons. So now they are rRock, rPaper and rScissors. This gives me something simple that looks like (in part):
That’s the VB code of course. But then I thought, I can use enums in C# and maybe that would be easier. So I created enums for both the computer pick and the result.
The code (in C#) now looks something like:
It’s all very well self documented and it’s pretty clear what is going on but it feels ugly to me. It’s the conversion/casting that bugs me the most as that just feels wrong to me. How about using integer constants? It’s a little more setup (in VB)
But the resulting code feels cleaner to me.
For this type of project I think I get all the benefits I want from named objects (in this case integers) in terms of the code being self-documented and requiring less actual thought on things that I really shouldn’t be wasting brain cells thinking about but without the complexity of having to fully specify and cast the enums into integers. Is it still overkill? I could see some people saying so but for me there is enough added value to make it worthwhile. Clearly the computer and the compilers really don’t care. Names are just identification and in many cases they will replace variable references with loading constants into registers to speed. We really don’t need to worry too much about that.
What do you think? Hopefully I didn’t lose anyone switching between C# and VB but honestly that’s the way my crazy mind works. how would you handle this sort of thing? Is it a worthwhile conversation to have with beginners? Obviously I think so but am I wasting time?