Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

June, 2010

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    A Rose is a Rose

    • 6 Comments

    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):

    Select Case computerChoice
        Case 0          '   Rock
            If rPaper.Checked Then
                Stat = 2    '   Lose
            ElseIf rSissors.Checked Then
                Stat = 1    '   Win
            Else
                Stat = 0    '   Tie
            End If

     

    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.

    enum whoWins { iTie, iWin, iLose };
    enum iPic {Rock, PaperS, Scissors};

     

    The code (in C#) now looks something like:

    switch (computerSelects)
    {
        case (int)iPic.Rock:                    //   Rock
            if (rPaper.Checked)
                Stat = (int)whoWins.iLose;      //   Lose
            else if (rScissors.Checked)
                Stat = (int)whoWins.iWin;       //   Win
            else
                Stat = (int)whoWins.iTie;       //   Tie
            break;

     

    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)

    Dim iWin As Integer = 1
    Dim iLose As Integer = 2
    Dim iTie As Integer = 0
    Dim pickRock As Integer = 0
    Dim pickPaper As Integer = 1
    Dim pickScissors As Integer = 2

    But the resulting code feels cleaner  to me.

    Case pickRock          '   Rock
        If rPaper.Checked Then
            Stat = iLose    '   Lose
        ElseIf rSissors.Checked Then
            Stat = iWin    '   Win
        Else
            Stat = iTie    '   Tie
        End If

     

    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?



  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    New England Innovation Month 2010

    • 1 Comments

    June has been designated as New England Innovation month. It was innovation (the company was Digital Equipment) that really caused me to move to New England 30 years ago. Today I think people tend to underestimate the innovation going on in New England. Forbes recently named Boston number 8 on their list of the most innovative cities in America though which is something. But the west coast seems to get a lot of the innovation attention. Of course there is a lot going on out there but, well, let’s just say I’m not a west coast person and leave it at that.

    I think it is important to promote innovation, new business start ups, and technology growth where ever you are though. Diversity has a lot to do with where people live and not just their ethnic, racial, religious and gender attributes. Innovation is not one size fits all. That’s one reason I am glad that Microsoft opened the New England Research & Development Center (aka NERD Center) in Cambridge Massachusetts. Some of the best universities in the world for innovation and technology are based in New England. Arguably the Boston area has the highest density of top technology programs in the world. So a lot of innovation starts here even if it something moves elsewhere eventually.

    If you are in New England and interested in technology (or teach or know students who are) and innovation there are always lots of things to do to get involved. The New England Innovation month site lists many of them. Microsoft NERD is please and proud to host technology and innovation events regularly. You  can see a list of events at NERD on their website. Many of these events are great for learning about technology, networking to build a career/job finding network, and just being with interesting people who do interesting things.

    If you are not in New England, what do you do? Start with user groups for products you are interested in. Microsoft.com has a list of user group links you can try. You may also have a group like the Mass High Technology Leadership Council who sponsor many events including my favorite event Tech Tuesdays (hosted by Microsoft at NERD BTW).

    Technorati Tags:


Page 6 of 6 (17 items) «23456