Visual Studio

Visual Studio is Microsoft’s Integrated Development Environment (IDE) used for building software for Windows and the .NET platform.

String Hashing in F#

I recently wrote a small program to convert some strings into a ‘unique enough‘ hash which could be used as a short reference.

Whenever I’m trying to come up with an idea of how to handle these kinds of functions, I tend to turn to my favourite language, F#. This allows me to do rapid prototyping in a very functional way.

Below is the prototype version of this hashing program:

open System
open System.Text
open System.Security.Cryptography

let encode (alpha:string, number:int) =

    let b = alpha.Length

    let rec enc (s:string, n:int) =
        match n with
        | x when x <= 0 -> s
        | _ -> enc (s + alpha.[n % b].ToString(), n / b)

    match number with
    | 0 -> alpha.[0].ToString()
    | _ -> enc ("", number)

let md5Int (input:string) =

    let clean (str:string) =
        str.ToLowerInvariant()
           .Trim()

    let computeHash (str:string) =
        let bytes = Encoding.Unicode.GetBytes(str)
        use crypto = new MD5CryptoServiceProvider()
        crypto.ComputeHash(bytes)

    let convert (bytes:byte[]) =
        let i = BitConverter.ToInt32(bytes, 0)
        Math.Abs(i)

    convert (computeHash (clean input))

let hash (input:string) =

    let a = "ABCDFGHJKLMNPQRSTVWXYZ0123456789"
    let i = md5Int input

    encode(a, i)

Once I have this program created, it’s very easy to use in F# Interactive like so:

> hash "Here is a test string!";;
val it : string = "1W2ALLB"
> hash "The hash is much smaller, which is great.";;
val it : string = "5DAF5T"
>

It’s a great time to be a Microsoft-focused software developer

The Build 2014 conference took place in San Francisco last week, where thousands of developers and designers came together to learn all the latest information about what it means to use Microsoft development tools, and build solutions that run on Microsoft platforms.

I have over 137 GB of videos yet to watch, but I can already say that it has been one of the best Microsoft developer conferences to date.

Here are my thoughts so far, with a few links to some of my highlights.

.NET is alive and well – and becoming more open

build-2014-4

There was a general feeling that .NET developers had been left behind in recent years. A strong push to using HTML and JavaScript really got some people worried when Windows 8 was announced – but at Build 2014 the message was loud and clear. The .NET platform is alive and well.

It’s almost as if they asked .NET developers what would make them really happy, and set about doing all those things. For me, personally, I was most excited by the promise of open-sourcing even more of the framework, including the brand-new compiler platform (Roslyn) that has been years in the making.

Initiatives like .NET Native will allow applications built with C# and Visual Basic to get much closer performance to applications created with C++. Starting first with Windows Store apps, these architecture specific binaries will increase performance at runtime without losing the productivity of developing with the architecture-agnostic .NET Framework.

Roslyn and .NET Native are in preview, and won’t be completed until the next version of Visual Studio. But there have also been a wide range of updates to the ASP.NET web platform, MVC, Entity Framework and way more – most of which are either available now with Visual Studio 2013 Update 2, or can be downloaded using packages from NuGet.

You can tell Windows is under new leadership

build-2014-1

Cortana – the new assistant built into Windows Phone 8.1 – was the highlight of the show. There had been rumours of Microsoft’s answer to Siri and Google Now for quite some time, so a lot of the information had already come out. But still, it was great to see Joe Belfiore talking to her publicly for the first time, confirming what Halo fans were waiting for: yes she is called Cortana, and yes, she sounds like Cortana.

As Cortana is such a huge step for Microsoft, I’ll be going in depth on what I think over the next few weeks.

Cortana wasn’t the only addition to Windows Phone 8.1, there were hundreds of new features and tweaks to the UI that will take weeks to be documented – the new calendar is just one of these. I’ve tried it in the emulator and it’s really great. The developer preview is due this month.

As a developer, the news of Universal Apps was very welcome. This allows high amounts of code sharing between applications designed for both Windows and Windows Phone. I can’t wait to try this stuff out, especially with the new JavaScript support.

Windows 8.1 has some new features too. The much-leaked Windows 8.1 Update includes changes to how the operating system behaves with a keyboard and mouse – though these changes are only the start of the improvements that are coming.

Azure is the future, and already here

build-2014-3

Microsoft’s cloud platform has improved tremendously over the last few years and this year was no exception – in fact, there were so many new features I can’t even begin to go through them all. Some I probably won’t even use (Java? No thanks.) but I know the option is there, which will inform my decisions for cloud hosting in the future.

Scott Guthrie and his team talked at great length about new features that will improve productivity for ‘DevOps’ tasks significantly. For example, the new Azure Portal includes the ability to view statistics, create new infrastructure, build new virtual machines, deploy websites and even do code changes with Git source control – all from one UI.

I already use Azure as part for my personal projects, as well as Visual Studio online as my source code repository. I plan on using it even more in the future – so I’m pleased to see Microsoft lead in this area. And they really are leading.

It doesn’t have to be Windows & .NET

build-2014-2

Times have certainly changed, and PCs that run Windows are no longer the most popular computing devices. As a predominantly Microsoft developer, this makes me wonder about the future. This year’s Build has continued the recent trend of Microsoft supporting other platforms in new ways – plus with partnerships with like likes of Xamarin, I see no reason to even contemplate switching over to Android (the most popular platform) in the foreseeable future.

While I was hoping that there would be the announcement of a Xamarin acquisition, they didn’t disappoint. It was great to see Maguel even joining Anders on stage during the keynote. Both of these guys are very smart, and I look forward to what else the partnership can bring. Being able to develop universal apps that also target iOS and Android is not very far away at all.

Querying TFS with TFPT.EXE and PowerShell

At Branded3 we use Team Foundation Server for source control, task managment, and various other tracking purposes. One of the benefits of this is being able to run queries with WIQL to pull off reports.

As is usually the case with me, I have set up a couple of PowerShell scripts that use TFPT.EXE from the Team Foundation Server Power Tools to make life a little simpler for myself…

Viewing Open WorkItems

$TFSSERVER   = "hq-tfs08-01.branded3.net"

Function Get-WorkItem
{
    $query = "SELECT [System.Id], [System.Title] FROM WorkItems " +
             "WHERE [System.AssignedTo] = 'Julian Kay' " +
             "AND [System.State] <> 'Closed' " +
             "AND [System.State] <> 'Resolved' " +
             "ORDER BY [System.Id]"

    tfpt query /collection:$TFSSERVER /wiql:$query /include:data
}

This little script gets a list of WorkItems which are not closed or resolved from TFS. I find this much faster than opening a copy of Visual Studio to find out which tasks I have assigned to me. True you could run this kind of script by using a batch file, but I like the fact I can use this in conjunction with the various Outlook scripts I use for PowerShell with simple one-liners like foreach ($workItem in Get-WorkItem) { Add-OutlookTask “$workItem” }

Getting Work Hours

Function Get-WorkItemHours
{
    $month = (Get-Date).ToString("MMMM")
    $year  = (Get-Date).Year
    $query = "SELECT [Completed Work] FROM WorkItems " +
             "WHERE [System.AssignedTo] = 'Julian Kay' " +
             "AND [Assigned Month] = '$month' " +
             "AND [Assigned Year] = '$year'"

    $hours = tfpt query /collection:$TFSSERVER /wiql:$query /include:data
    $total = 0.0
    foreach ($hour in $hours)
    {
       $total += $hour
    }
    $total
}

We also have custom fields which help us keep track of the hours we’ve spent on a project, and as shown above, we can even see how many hours have been spent in a month. By running this simple script I can be sure that all my time is correctly recorded to enable us to both bill correctly, and keep track of how long things really take.

Naturally, everyone has their own requirements for scripts like this, but PowerShell gives me the flexibility to create these simple utilities with very little development work.

Writing and Running F# Scripts with Vim

When I’m writing software for the .NET Framework I tend to have a copy of F# Interactive open. This lets me run commands directly like so…

This is fine for simple stuff, but if you’re writing something a little more complex it’s better to write a script. In the past I’ve done this by having another copy of Visual Studio open and running the script that way. This works really well, and includes colour coding, intellisense and all the other good stuff you expect.

However there are times that having yet another copy of Visual Studio is a little heavy for just keeping track of a script. Enter Vim, the de facto command line editor for Unix and other operating systems.

  1. Set up Vim to work with PowerShell
  2. Grab yourself a copy of the F# Syntax file
  3. Save it into your Vim plugins directory
  4. Add the following lines to your vimrc file…
au BufRead,BufNewFile *.fs set filetype=fs
au BufRead,BufNewFile *.fsx set filetype=fs

Now when you create a .fsx file, you can run it directly from F# Interactive by using Vim’s shell execution feature.

:!fsi %

This will run your script in F# Interactive and present you with the results. The :! Vim command is for running the external program, and the % represents the filename of the currently open document.

Note that you’ll need to have set up F# Interactive by either adding it to the path or setting an alias in PowerShell. If you haven’t done this already, you can do it by adding the following lines to your PowerShell profile:

$FSIPATH     = "C:\Program Files (x86)\Microsoft F#\v4.0\Fsi.exe"

Set-Alias fsi  $FSIPATH

Build your own Astrolight app

Astrolight is a simple red flashlight application which you can get in the Windows Phone Marketplace, it’s so simple that I’m going to tell you how you can create one too. With two lines of code.

Assuming you’re all up and running with the free Visual Studio Express for Windows Phone, we just need to start a new Windows Phone Game project…

Then we double click the Game1.cs file and find the Draw method. Here we need to change replace one line with two:

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
    graphics.IsFullScreen = true;
    graphics.GraphicsDevice.Clear(Color.Red);

    base.Draw(gameTime);
}

Press play and watch your brand new app on the Windows Phone Emulator!

Five Favourite Visual Studio 2010 Extensions

Productivity Power Tools

Probably the best code focused extension for Visual Studio 2010 is the amazing Productivity Power Tools. It includes such gems as the Solution Navigator which lets you navigate to classes and members, search for references, and filter your solution to show only checked out files.

NuGet Package Manager

NuGet is an awesome open source package manager for the .NET platform, which includes a very large amount of third party libraries ready to use in a couple of clicks. Or, if you fancy, by using a PowerShell console. They totally get extra points for the PowerShell integration…

F# Outlining

The name pretty much describes what it does. Visual Studio 2010 was a pretty massive release for Microsoft, including F# for the first time. This is why I totally forgive them for not implementing outlining support for this fledgling language. Fortunately someone else did their job for them.

Spell Checker

Everyone wants to have properly spelt comments, especially when you’re using the XML comment features in the .NET languages to produce documentation for your customers. While this plugin is pretty simple, it works a treat and even supports custom dictionaries.

Python Tools for Visual Studio

Not just an extension, but a whole bunch of Python related win for Visual Studio. This latest version works with both CPython and IronPython which really increases the scope for these tools.

Don’t forget to couple these guys with the Microsoft Web Platform Installer for great justice.