Computer Science Teacher

December, 2008

• When Is A Short Circuit a Good Thing?

I get inspiration for blog posts from a number of places. Sometimes from Twitter (see yesterday’s post) and sometimes for other blogs. Actually other blogs are a constant and regular source of inspirations. Today’s post was inspired by a Programming Pop Quiz posted by Rob Miles. The sample code he uses works because of what is called short circuiting. Let me show you the code and explain it in plain English (or what passes as plain English from me.)

while (startPos < input.Length && input[startPos] != '"') startPos++;

This is a while loop that first checks to see if startPos is less than the length of input. One assumes that input is a string array based on the context. The expression then check to see if the string in that location (indicated by startPos) is not equal to a double quote. Assuming that startPos is less than input.Length AND that the indicated character is not a double quote the value of startPos is incremented and the loop continues. Clear?

Now here is where the short circuit comes in. If the value of startPos is greater than or equal to the length of input and the computer were to execute the second comparison there would be an error. Why? Because the program would be attempting to access a location that is not defined and that is outside the range of the array. This is what we call “a bad thing.” However, and good for us, if the first comparison evaluates to false the computer “knows” that it does not need to do the second comparison and so it doesn’t. This is a good time to review Truth tables if you are confused about why we don’t need the second comparison if the first is false.

In Visual Basic this gets a little interesting though. The C# code (or is it C++ or Java or some other C-family language? Does it matter?) converts to the following in Visual Basic .NET.

While startPos < input.Length AndAlso input(startPos) <> """"C
startPos += 1
End While

You’ll notice that rather than And the operator that is used is AndAlso. What’s up you may be asking. Doesn’t VB have a keyword And? Yes it does. AndAlso is new though. In Visual Basic 6.0 and earlier there was only the And operator. The And operator did not short circuit. What that means is that the code above would not work the same way as the C#/C++/Java example if the And operator was used. You could (probably would) run into the unwanted exception error. Initially in the first beta versions of Visual Basic .NET the And operator was changed to short circuit just as && does. It seemed like a good idea. And in theory it was. The problem was that there was a lot of old code out there that depended on And not doing a short circuit. Ultimately it was decided to add the AndAlso command to do short circuit operations. By the way this is also why there is an Or and OrElse operator in VB .NET. Paul Vick of the Visual Basic design team explains their reasoning on his blog.  He also explains how C-family languages had two operators where VB had one.

This is one of those issues that often comes up when there is an installed base of users. Its not always an easy answer. What do you think? Or your students think. Did the VB team make the correct decision? Why or why not?

• Tetris for Small Basic

Talk about a great sample of the week – the Small Basic blog announces a version of the classic Tetris written in Small Basic. At a little over 500 lines of code (530ish) its a pretty cool and yet manageable game program. If you were thinking of writing a version of Tetris of your own in any language this may be one you want to look at for ideas. The code is all there. And if you were not sure how for the simplicity of Small basic could take you (or students) this may impress you. It did me.

I’d love to hear what others think about this program and/or what they were inspired to do themselves after viewing it. And if nothing else, Tetris is always a great way to kill time until vacation ends. :-)

Oh and make sure you have the latest 0.2  version of Small Basic (feature information here.)

So I got a number of links to links today. OK some are direct to the items but some are to pages with just too many links to copy. Wouldn’t be right. The first big set of links I found at the NCWIT blog post titled 100 Recommended Resources on Gender in S&E (S&E  stands for science and engineering). It links to Ruta Sevo’s “10 x 10 List.”There are a lot of great resources there and it’s not all for girls. Headers are below:

For anyone:

• Self study guide – reading for newbies
• Libraries, knowledge centers, Bibliography

• Biographies of women in science and engineering (Role models)
• Video’s and CD’s designed to inspire girls
• Guides for parents and afterschool leaders
• Afterschool Activities, materials and kits

For educators and researchers:

• Training and Consulting Services, Technical Assistance Projects
• Best practices resources – K-12
• University-level transformation/change
• International activity
• National policy reports
• Research on discrimination and women in S&E
• Statistics on diversity in S&E
• Title IX and S&E education
• More organizations

For girls and boys:

• Games and online activities for children

From the Ideas for Teaching Computer Technology to Kids blog I found two great resources.

Going Green – There is lots of talk about the environment, using less energy and related topics these days. The UK issue of TechNet magazine is a special “green edition.” How does computer hardware and software relate to saving energy? Check it out!

Page 1 of 7 (21 items) 12345»