Smart Students

DSCF3619

We have some very smart students. In C# we are considering problems you can have with conditions if you are checking for valid entries, for example:

if ( flimNo < 1 && filmNo > 5 )
{
// unlikely we will get here
}
else
{

// funny how this part always gets obeyed...

}

I made the point that a number which is less than one and greater than five is pretty much impossible. Quick as a flash, one of our First Years said “Root 36”. Which can be -6 or 6.

FYI, what you really want if you want to reject invalid entries is to change that && (and) to || (or).

if ( flimNo < 1 || filmNo > 5 )
{
// film number is invalid
}
else
{
// film number is valid
}

Using the Proper Kinect USB Drivers

End of Year Ball
This picture has nothing to do with Kinect, but I took it after the End Of Year Ball and I quite like it…

I’ve been playing with the Microsoft Kinect SDK and I really like it. The speed with which it snaps on to people and tracks them is really impressive. However, I did have one bit of fun and games when I installed it. Like loads of other people I’ve been using other drivers with the hardware and although some of these have uninstall behaviours they don’t always get rid of the device drivers themselves. This can lead to problems when you try to install the “proper” drivers and the old ones load up and get in the way. So, to get rid of the Kinect drivers you can do this before you install the Microsoft Kinect SDK. First perform the uninstall on all the previous drivers. Now you need to get rid of anything left lying around:

Open up a new command prompt running in Admin mode. The best way to do this is to click the Start button, type CMD into the search box and then hold down CTRL+SHIFT and press Enter. If you get this right you will be rewarded with a User Account Control dialogue box warning you that you are about to do something vaguely dangerous. Click OK.

Now, in the command box give the command:

SET DEVMGR_SHOW_NONPRESENT_DEVICES=1

This sets an environment variable to tell Windows you want to see all the hardware devices, not just the ones that are active . If you type this command wrong you won’t see an error of any kind, but the process won’t work either. You can select and copy the above text into the paste buffer, then right click in the command window and paste it into the command prompt if you like. Now give the command:

devmgmt.msc

This starts the Device Manager. Now open the View menu and select “Show hidden devices”. This is actually quite fun, as now you will see every device that has ever been connected to your computer. If your machine is like mine there will be around 50 or so different Disk drives, one for every memory key that has been plugged in over the years. Look through the device tree for things with the word Kinect in the name, or the name of the package you are removed.  Look in the “Human Interface Devices”, “Sound, Video and Game Controllers” and “Universal Serial Bus controllers” parts. Anything you find that you want to get rid of you must right click and select Uninstall. If the dialog that appears has a checkbox marked “remove driver software files” then you should select this so that the driver files are no longer around to cause trouble.

Note that this could be vaguely dangerous, in that if you delete an important driver for your system you might find that it will stop working until you replace the driver file, but if you only remove things that are to do with Kinect you should be fine. One tip is to plug the Kinect sensor in before you remove the software and just note what appears in the Device Manager when you do this. These are the things that need to be removed.

Once you have removed all the drivers simply exit Device Manager and close the command prompt. The setting is forgotten, so if you want to remove other drivers later you have to do the whole thing again. This technique can also be useful for removing old USB drivers that are causing trouble.

Flipboard

image

I’m not sure if Steve Balmer reads my blog (actually I’m pretty sure he doesn’t) but in the unlikely event that you are reading this Steve, would you please give Flipboard an enormous sum of money and ask them to make their program available on Windows Phone. And the Microsoft Tablet when that appears.

Flipboard takes a very simple idea (read social network feeds and use them to build a personalised magazine) and implements it in truly beautiful way. Your Twitter feed becomes a gateway onto a whole collection of interesting content. Simple tweets containing links are presented as pieces of editorial over multiple pages that you can flip through. If you have an iPad you must get this program. It is completely free and totally wonderful. And I want it on my Windows Phone.

Programming Puzzler

Quick test for all you programming experts. Will this stupid code compile?

public int InfiniteLoop()
{
    while (true)
    {
        Console.WriteLine("Loopy");
        System.Threading.Thread.Sleep(1000);
    }
}

Note: All the namespaces are in place and the WriteLine and the Sleep are perfectly legal calls.

Talking the Talk and Walking the Walk

Lift Mottor.jpg

We had a couple of presentations in the department today. Team Yellow and Team Purple (Tentacle?) gave the initial presentations for their group projects.  To say that the teams had been working together for  a week or so and it was their first stand up together they did very well.

One thing that did stand out though was some of the phrases that were used and this brought home to me how you need to be careful how you talk in front of an audience, particularly if you want to convince them you know what you are doing.

For example take the phrase “User Friendly”. It is all very well to say “We are going to produce a user-friendly solution”. You want to convey that you think this aspect of a system is important. However, saying it like this is pretty much meaningless. The customer is not expecting you to produce something that is “user-hostile”, but the phrase could also be expressed as “We’re not going to make something that acts as if it hates you”. 

It is far better to say what you are actually going to do to solve the problem. “We are going to closely involve the end user in the design and implementation so that they find the system easy to use.” is a much better way to express your intentions.  Take a similar approach when you talk about security. Rather than saying you think something is important you must say what you are going to do about it.

The other thing that came out from the presentations was partly my fault. I’d said earlier that it is very important to make the customer aware of those aspects of the system that you are not going to implement. For example, you might be expecting the customer to back up the data rather than providing data backup as part of your solution. You need get this over, but I’m not sure you should have have a slide with the heading “Things we are not going to do”.  It is far better to say things like “The server infrastructure that you are using will be used to back up our data along with that from other systems”. This puts the responsibility in the right place without sounding like you are avoiding work.

If all this sounds a bit like the dread “marketing speak” then I’m very sorry about that, but I do feel that it is important that you make sure that things you say are backed up with a some kind of action plan and you should avoid sounding negative about your intentions.

Bubblegum for Windows Phone pictures and Good Advice

image

Bubblegum is a new program for Windows Phone 7 (and coming to other platforms they say) that lets you take pictures and share them with your chums on Twitter, Facebook and Foursquare. The application also has some very “hipsteresque” (if that is a proper word) filters that you can apply to the pictures before you send them. These make you look even more artistic and interesting.  As a true artist, I have of course posted a few of my own. My name on Bubblegum is, unsurprisingly, RobMiles it you want to take a look.

The program is free, and fun. It was written by a couple of folks from Microsoft who are also a couple, if you see what I mean. Aarthi Ramamurthy and Sriram Krishnan have done a super job in making a nice little program which does the job with flair and humour. Sriram Krishnan even has a blog. Everyone should read his “Stuff I’ve learned at Microsoft post”. Great stuff.

http://www.sriramkrishnan.com/blog/

SyncToy and Drive Letter Setting Fun

image

At the end of the year I have to go through my marathon photo tidying up session. I keep all my writings and other important stuff on SkyDrive, Live Mesh and DropBox but there is just not room in the cloud for all the pictures that I take. And so this is the time when I go through the pictures directories of all my various machines and pull them all together to make a final, definitive set of all my photos that I can copy across a bunch of hard disks.

And when I’m doing this I find SyncToy very useful. I can use it to bring together files from lots of different disks and also make sure that two disks hold exactly the same set of files. It doesn’t seem to mind being pointed at directories that hold many gigglebytes of files and I can just leave it to get on with the work.

Oh, and when I’m using external drives I find it very useful to be able to make sure that when I plug the drive in it gets exactly the same drive letter each time. You can do this using the Disk Management tool. Open up Control Panel and then for “Disk Management” Then open the scary “Create and format hard disk partitions” option to bring up the Disk Manager tool:

image

Select the drive that you want in the list of volumes and right click it. Then select “Change Drive Letter and Paths..” to bring up the Change dialog box.

image

Then click Change and nominate the letter that you want to use in the combo box on the right:

image

Once you have done this you just click OK down all the menus and, presto, your drive has the letter you chose. And even better, the letter is remembered for next time you plug the drive in. Great stuff.

C# Fun with Pexforfun

image

pexforfun is fun. Especially if you like writing code. It gives you a mental workout, teaches you programming smarts and has a lovely test driven, puzzle powered approach based on “Code Duels”.

You are set the task of writing a program that behaves in the same way as some “mystery code”. You type your code into the browser (you get intellisense support and everything) and then hit the “Ask Pex!” button. Pex then compiles your program and runs it against the test cases for that mystery code. If your program works you get bragging rights and then move on to the next puzzle. If your program fails you get to see which tests failed, so that you can refine your code for next time.  You can log into the system so that you can track your progress through the puzzles or you can just turn up and have a go, like I did.

I’ve just done one puzzle and really enjoyed it. I think we will be using pex during our first year programming labs at Hull, it really is a nice way to sharpen your C# skills. Find out more here:

http://www.pexforfun.com/

Cube Mania and Web Development

cube mugs

I think I may be getting too far into this Nissan cube thing

I really like my Cube. I went out last night to a Hull Digital Developer group meetup. There ware two good talks, one by John Polling from “The League of Extraordinary Developers” (great name) about SASS and Compass and CSS and another by James Greenwood from Strawberry about HTML5.  Both speakers knew their stuff and the content was interesting (although I’m not really a web developer type person myself).  But I must admit that my thoughts kept going back to my little tin box outside and the drive home in it…Next time I’ll be more focused. 

Oh, and don’t forget if you are from Hull that you need to register for Hull Digital real soon. Looks to be a good one.

Quick Number Formatting in C#

IMG_2184

I’ve been playing with the Windows Phone accelerometer, and displaying results. Thing is, it gives me values which have lots of decimal places that I don’t really want. They make the numbers flicker in a way that hurts my head. What I want is a way to just display them to two decimal places.

I must admit that previously I’ve resorted to very dodgy behaviour at this point, such as multiplying by 100, converting to an integer and then dividing by 100 again. Must be my old assembler ways coming back to haunt me. Turns out that there is a much easier way of doing this in C# which is to use the string formatter:

message = "Accelerometer" +
    "\nX: " + string.Format( "{0:0.00}",accelState.X) +
    "\nY: " + string.Format( "{0:0.00}", accelState.Y) +
    "\nZ: " + string.Format( "{0:0.00}", accelState.Z) ;

This little block of magic gets the X, Y and Z values out of a vector and then displays them to two decimal places in a string I can display in my XNA program.

The key is the {0:0.00} bit in the format, which gives a pattern for the display of the first (and only) value to be converted. This pattern says a single leading digit and two decimal places.

If you want to do interesting things like put commas into numbers (so that you can print 1,234,567 type values) then you can do that too. You can find out more about how the format strings work in my C# Yellow book (subtle plug) starting on page 50.

Office 2010 Now on MSDN

image

If you are lucky enough to have an MSDN subscription you can now head over there and download Office 2010 Professional Plus. This gives you Word, PowerPoint, Excel, OneNote, Outlook, Publisher,  Access, InfoPath and a bunch of SharePoint goodies as well. There are two versions, for 32 bit and 64 bit installations.

If you want to use the 64 bit one you will have to uninstall all the previous 32 bit versions of Office 2007, including any compatibility tools and viewers, but the installer talks you through this and tells you what to do. It looks like the 64 bit version is well worth the effort, it loads in a trice and seems a bit more snappy than the 32 bit one.

Once the programs get running they look very snazzy too. Outlook found all my previous details and just works a treat. There are some nice improvements that I’m finding as I use the programs. The mini “Start” button has been replaced by a File tab on the menus (which makes sense) and the way you modify styles in Word hasn’t changed (which is just as well, as I only found out how to do that in Word 2007 last week).

For me the big news is the SharePoint integration and the group working options which look very interesting.

Exploding Images with Photoshop Elements

Dalby Forest Fence

Every now and then I fall over hilarious bugs that really shouldn’t be out there. It kind of cheers me up. I’ve never written software with holes like these, and they are selling theirs as product.

I’ve found a truly smashing one in Photoshop Elements. I have a love-hate relationship with this program. I stick with it because it does what I want eventually and I can’t be bothered to learn another program. I have the feeling that it could do pretty much anything, although I also have the feeling that I’ll never figure out how to use it.

Anyhoo, the bug/feature has to do with bulk processing of images. I’ve started reducing the size of my pictures before I send them to Flickr so that they don’t take too long to transfer. I make them 1500 pixels in width, which seems to work OK. I did one batch, and then tried to do another.

And my machine stopped. For quite a while. Eventually I killed the Photoshop export and tried to figure out what had happened. Turns out that the program remembers the number that you enter to resize your picture, but not the units. The program was busily creating a bunch of images 1500 cms wide. Or around 450 feet. This begs several questions.

  • Why does it just remember the number?
  • Has anyone ever really needed to produce a picture that big?
  • Did anyone ever test this?

Oh well.

Wacom Bamboo Fun and Games

Lights

I didn’t actually buy much yesterday at the Gadget Show. Just a pair of enormous headphones from Sure that sound amazing (I daren’t tell number one wife how much I paid for them but they were heavily discounted) and a Bamboo Fun graphics tablet. They had them at 20% show discount and although they didn’t have any left to sell I was able to order one for home delivery the following day. I was thinking “Yeah, right.” as I gave my details, but the company were as good as their word and the tablet arrived today.

The thing about the Bamboo fun that had me most interested was that it supports multi-touch input as well as pen based control. It turns out that multi-touch is indeed supported, but not in a proper Windows 7 way. The Windows 7 touch API is designed for screen only use, there is no support for multi-touch tablet input in the operating system. Instead the tablet driver produces cursor and keyboard commands that allow you to do things like pinch zoom in compatible programs.  This means there is unfortunately no way you can use it for things like the Windows Phone emulator, where multi-touch input would be very nice. I’m not that bothered though, the device comes with a very good software bundle including a free copy of Photoshop Elements 7 and it is a nice tablet with a responsive pen which will make image editing easier.

Processing Lots of Files in C#

4346439556

Elliot came to see me today with a need to process a whole bunch of files on a disk. I quite enjoy playing with code and so we spent a few minutes building a framework which would work through a directory tree and allow him to work on each file in turn. Then I thought it was worth blogging, and here we are.

Finding all the files in a directory

The first thing you want to do is find all the files in a directory. Suppose we put the path to the directory into a string:

string startPath = @"c:\users\Rob\Documents";

Note that I’ve used the special version of string literal with the @ in front. This is so my string can contain escape characters (in this case the backslash character) without them being interpreted as part of control sequences. I want to actually use backslash (\) without taking unwanted newlines (\n)

I can find all the files in that directory by using the Directory.GetFiles method, which is in the System.IO namespace. It returns an array of strings with all the filenames in it.

string [] filenames = Directory.GetFiles(startPath);
for (int i = 0; i < filenames.Length; i++)
{
   Console.WriteLine("File : " + filenames[i]);
}

This lump of C# will print out the names of all the files in the startPath directory. So now Elliot can work on each file in turn.

Finding all the Directories in a Directory

Unfortunately my lovely solution doesn’t actually do all that we want. It will pull out all the files in a directory, but we also want to work on the content of the directories in that directory too. It turns out that getting all the directories in a directory is actually very easy too. You use the Directory.GetDirectories method:

string [] directories =
          Directory.GetDirectories(startPath);
for (int i = 0; i < directories.Length; i++)
{
    Console.WriteLine("Directory : " + directories[i]);
}

This lump of C# will print out all the directories in the path that was supplied.

Processing a Whole Directory Tree

I can make a method which will process all the files in a directory tree. This could be version 1.0

static void ProcessFiles(string startPath)
{
   Console.WriteLine("Processing: " + startPath); 
   string [] filenames = Directory.GetFiles(startPath); 
   for (int i = 0; i < filenames.Length; i++)
   {
      // This is where we process the files themselves
      Console.WriteLine("Processing: " + filenames[i]); 
   }
}

I can use it by calling it with a path to work on:

ProcessFiles(@"c:\users\Rob\Documents");

This would work through all the files in my Documents directory. Now I need to improve the method to make it work through an entire directory tree. It turns out that this is really easy too. We can use recursion.

Recursive solutions appear when we define a solution in terms of itself. In this situation we say things like: “To process a directory we must process all the directories in it”.  From a programming perspective recursion is where a method calls itself.  We want to make ProcessFiles call itself for every directory in the start path.

static void ProcessFiles(string startPath)
{
  Console.WriteLine("Processing: " + startPath); 

  string [] directories = 
                  Directory.GetDirectories(startPath); 
  for (int i = 0; i < directories.Length; i++)
  {
    ProcessFiles(directories[i]);
  }

  string [] filenames = Directory.GetFiles(startPath); 
  for (int i = 0; i < filenames.Length; i++)
  { 
    Console.WriteLine("Processing : " + filenames[i]); 
  }
}

The clever, recursive, bit is in red. This uses the code we have already seen, gets a list of all the directory paths and then calls ProcessFiles (i.e. itself) to work on those. If you compile this method (remember to add using System.IO; to the top so that you can get hold of all these useful methods) you will find that it will print out all the files in all the directories.

Console Window Tip:  If you want to pause the listing as it whizzes past in the command window you can hold down CTRL and press S to stop the display, and CTRL+Q to resume it.

Links for Software Engineers

Pot Pourri

I was talking to our .NET Development Postgrad students and we decided that there were a few things that you should be familiar with if you want to become a “proper” Software Engineer. These are the things I think you should do:

Read “Code Complete 2” by Steve McConnell. Perhaps the best book ever on software construction.  Then keep your copy where it is handy, and have a policy of reading a bit now and then, just to keep up to speed. If you can track down a copy of “Rapid Development” you should read this to.

Read I.M. Wright’s “Hard Code” blog. And buy the book if you like.

Read “How to be a Programmer”. Excellent stuff.

This is not everything you should do. There are other good places to look. But it is a start. Oh, and if anyone out there has other ideas about good, pragmatic texts for budding coders, then let me know and I’ll add them.

Christmas Chumby

4269183539

My “big” Christmas present to myself was a Chumby. This is a fun little internet appliance that you can write programs for.  I’ve started doing just that, and you can find out more at my newly minted Chumby pages.