Archive for December, 2009
Teaching the kids some programming
I don’t want to create a dynasty of coders, but I’d like to get rid of their blank look they get when I try to explain what the heck it is that I’m doing there. So since there was not much exciting to do today anyway I set up the laptops in the living room, and knocked on wood theat they wouldn’t find the experience boring and nerdy.
My kids are ten and eleven years old; my older son hasn’t shown any interest in computers apart from gaming yet; my younger daughter has lately been experimenting with typing stories into Word (OpenOffice Writer, to be precise), organizing music in iTunes, and manipulating photos.
I wanted something simple, quickly rewarding, and graphical for starters (to avoid the boring/nerdy impression mainly). I had seen KPL some time ago, but it’s been converted to/absorbed in a larger product called Phrogram: it’s commercial now, there seems to be quite a good community support, but I found it a little hard to find out where Phrogram starts and where it ends. What I miss on their website is a clear vision statement – it’s got 3D and database support, a debugger, testing… without having evaluated it too closely, I’d say it goes a lot further than being an absolute beginner’s tool.
Then there’s Scratch, which looks great for creating interactive media and things like point-and-click adventures. Scratch has got a graphic programming editor that lets you react to events, cause things to happen, and use structures like loops and forks (“when the kitten is clicked, play a ‘meow’ sound, for three times”).
Scratch looks like a lot of fun and surely supports getting wild with. The gallery of what people do with it is quite impressive. I’ll certainly give it a go later.
The thing I finally chose was a MS research project called Small Basic, which is specifically targeted to beginners. It features a highly simplified programming language, and a nice text editor that offers autocompletion and ad-hoc help. What I found striking about it is that it lets you use different types of programs: console apps, which are still great when you want to play with command-response style things (like a calculator); 2D vector drawing for cearting games and: The Turtle.
The Turtle offers a Logo-style programming model: There’s a turtle sitting in the middle of your screen; you can tell it to move, and to turn. While it moves, the turtle draws a line. You can’t have it any simpler. So here’s how to draw a rectangle:
Turtle.Move(100) Turtle.TurnRight() Turtle.Move(100) Turtle.TurnRight() Turtle.Move(100) Turtle.TurnRight() Turtle.Move(100) Turtle.TurnRight()
In fact, that was the first thing I let my kids do, and they got it working within minutes, and were enthusiastic about the result (*phew*). Then I gave them a pencil-drawing of a more complex shape, and they got that working after some fiddling as well.
Then they started making some own drawings (including an accidental swastika – by copy&pasting parts of the shape above) and were having fun. Soon there were complaints about the redundancy of typing in the same line of code several times (“can’t I just make it repeat this?”, typing “Turtle.DoItAgain()”). Time to move on: enter the for loop.
For howOften = 1 To 4 Turtle.Move(100) Turtle.TurnRight() EndFor
wow! That went a lot better than I’d expected. Next time I’ll try to introduce variables to draw a spiral:
For howOften = 1 To 100 howLong = howOften * 10 Turtle.Move(howLong) Turtle.TurnRight() EndFor
BTW, Small Basic compiles into “real” exe files that you can show off with, and can be converted to Visual Basic.
I’m quite confident that the first programming lesson was some fun, and sparked some interest.
Expressing some love for mindmaps
The smarter people amongst us may have found out ages ago: Mindmaps are just great.
I lived through my own private mindmap-epiphany when I saw a consultant taking notes from a day-long meeting in the form of a mindmap: he actually produced something that had structure, captured all the necessary content, and even had presentable style. My own notes kind of fell apart when the discussion was meandering from topic A to topic B, coming back to A via C, D, E, F to M, and Y.
Following that experience, I tried to use maps for taking notes during meetings, interviews, and as the first phase for writing documents, and for me, it flows very naturally. The main improvement is that structure comes by itself. You may find that a branch of ideas belongs somewhere else, and just push it over: mindmaps support refactoring.
I currently use FreeMind - it’s free, works well enough, and uses a very simple XML document format that can easily be transformed into something else if necessary.
The remaining problem is that I need to type on your laptop, which gives the people I talk with the uneasy feeling that I’m just checking my e-mail or tweeting or whatever I might do. Would someone get me a big tablet, please?
Running NUnit 2.53 tests for Silverlight
Jeff Wilcox has described in this article how to make NUnit tests run in the Silverlight unit testing framework. That article provides all the groundwork for what you need to do:
- Compile a version of the NUnit framework that runs in the Silverlight 3 context
- Provide metadata for the Silverlight unit testing framework so it knows which attributes to look for in order to locate what method is a test (and a little more)
- Register this metadata in your Silverlight unit test project.
I’d like to thank Jeff for his great work, and provide some raisins on top: an update to version 2.53 of NUnit. Here’s what’s to do:
- Take the compatibility shim and test metadata provider from Jeff’s article.
- Download the current NUnit source code (in this case 2.5.3). Create a new Silverlight project, and add the code for the NUnit framework into it. Compile: a whole bunch of exceptions come up.
- NUnit’s source code uses the #if precompiler directive in order to exclude parts that are incompatible with different version of the runtime. By defining the NET_2_0 symbols, we can make a lot of the errors disappear: the nongeneric implementation of Stack, for example, is excluded now. We need to exclude some more code: Instead of deleting it altogether, I chose to use the SILVERLIGHT symbol and add a #if !SILVERLIGHT directive to get rid of unsupported code instead. In cases where a whole method body is excluded, add a section on the bottom that throws a NotSupportedException:
#if SILVERLIGHT throw new NotSupportedException( "Not supported in Silverlight" ); #endif - We need to get rid of the serialization constructors from all the exceptions.
- Remove the attributes for threading demands – they’re useless in Silverlight anyway.
- Remove the attributes for threading demands – they’re useless in Silverlight anyway.
- Add a new shim class for the non-generic System.Collections.Specialized.ListDictionary to the Compatibility project. It is a simple derivation of the genetic Dictionary.
- In the PathComparison class, there are some unsupported string operations. I didin’t bother exluding and replacing these, and just refactored them to supported alternatives. (Thinking about it, I should probably have used the .NET2.0 switch. Next time, promised)
- Finally, the metadata provider uses a changed property for in the ExpectedException attribute. Corrected, aaaaaand…
- Done. Now all you need to do is reference the three generated assemblies, follow Jeff’s article on how to set up the testing framework for the NUnit provider, and start writing tests.
This was easy enough that I hope it can be done for any upcoming version of NUnit.
The point where I’d like to disagree with Jeff Wilcox the idea that NUnit was a legacy solution, and that you should start new projects with Visual Studio testing. NUnit provides much richer assertions, and a better syntax for setting them up: you won’t find assertions for examining collection content or thrown exceptions in Visual Studio testing. Or the upcoming ability to run tests in parallel compartments to simulate a distributed environment. NUnit is far from being a discontinued legacy solution, it is the de facto standard and quickly developing. And: thanks to the extensibility of the Silverlight unit testing framework, there’s not a reason not to use it for Silverlight as well.
UPDATE
The project is now hosted as nunit-silverlight on Google Code.
What is Silverlight trying to tell you?
This is what I spent the last hour with:
When building your Silverlight project fails with the error message “Whitespace is not allowed after end of Markup Extension.”, you’ve got a blank (or more) behind a databinding expression, like here:
<Button Content="{StaticResource ResText_SaveButton} "/>
See the blank behind the curly brace? That one.
If however it tells you “Unexpected ATTRIBUTE in parse rule PropertyElement ::= . PROPERTYELEMENT Content? ENDTAG..” then you may have stuck an attribute to an element that does not expect one, as to marveled at here:
<my:DataList.EditSection DataContext="{Binding FormData}">
