Parsing PowerShell Programatically

I've recently been working with a lot of PowerShell at work, and the experience has been, well, less than perfect. PowerShell is a dynamically-typed scripting language, and is most often used for administering Windows systems. The combination of making system changes and the runtime errors associated with dynamically-typed languages has been frustrating. It's quite common to get the system into a weird or inconsistent state when developing PowerShell scripts.

Ideally all your PowerShell is properly orchestrated with -WhatIf and SupportsShouldProcess functionality, but in my experience it's common for there to be large differences in behavior between -WhatIf execution and real execution.

The continue.

tagged as and

Level up your VsVim

If you're like me, you installed Visual Studio, installed VsVim, and then happily started coding. However, did you know that Visual Studio's semantic understanding and manipulation of your code is exposed to VsVim?

For example, you might already know that pressing gd in VsVim will "go to the definition" of a method. But what if we want to "go to the implementation" e.g. through an interface to the underlying concrete method? The standard Ctrl-F12 requires some contortions, and maybe requires glancing down at your keyboard.

It's easy to fix this; just add this mapping to your ~\_vsvimrc:

map gi :vsc Edit.GoToImplementation<CR>

Let's break this continue.

tagged as , and

The Implementation of C# Local Functions

C# 7 local functions provide a more intuitive syntax over creating verbose System.Func delegates, as well as being more capable (they support ref and out parameters, async, generics, etc). In addition, some articles have mentioned that local functions compile down to normal methods, thus reducing GC allocations when compared to System.Func.

I was curious about that last part. How does it work? Let's open up the dotPeek decompiler and find out!

First, here's a simple test program using a local function:

class Program
    static void Main(string[] args)
tagged as and

Solving the Zebra Puzzle with LINQ and C# 7

I recently ran across the Zebra Puzzle, a logic puzzle from about 50 years ago:

  1. There are five houses.
  2. The Englishman lives in the red house.
  3. The Spaniard owns the dog.
  4. Coffee is drunk in the green house.
  5. The Ukrainian drinks tea.
  6. The green house is immediately to the right of the ivory house.
  7. The Old Gold smoker owns snails.
  8. Kools are smoked in the yellow house.
  9. Milk is drunk in the middle house.
  10. The Norwegian lives in the first house.
  11. The man who smokes Chesterfields lives in the house next to the man with the fox.
  12. Kools are smoked in the house next to the house where the horse is kept.
  13. The Lucky Strike smoker drinks orange juice.
  14. The Japanese smokes Parliaments.
  15. The Norwegian lives next to the blue house.

Now, continue.

tagged as and

Finding Dead C# Code in an ASP.NET application

Large, long-lasting codebases tend to accumulate unused code, or dead code, over time. This happens as features are added, changed and removed.

Some types of dead code, like unreferenced methods, are easy to detect with Visual Studio's out-of-the-box static analysis. However, other types of dead code are trickier; maybe there's a method that's only referenced by unit tests, or it's referenced under a condition that's never valid:

if (DateTime.Now < new DateTime(1970, 1, 1))
    // some dead code

How can we detect this type of dead code? Rather than using static analysis to detect it, we can use dynamic analysis---the continue.

tagged as , and

Increasing Code Quality with SonarQube

I just posted an article about SonarQube, an open source static analysis tool, and how we set it up to scan every incoming GitHub pull request at work.

You can read it at the Jetabroad Tech Blog.

tagged as , and

Quick Tip: Format Finder for .NET date strings

If you've written C# for any length of time, chances are you've become very familiar with the MSDN documentation pages about Standard Date and Time Format Strings and Custom Date and Time Format Strings. Any time you need to format a DateTime, those pages are invaluable.

I don't find myself reading those pages often enough to warrant memorizing the strings, but I do read them often enough that it's worth automating the lookup. So I wrote a simple webapp to do it.

I plan on skewing those curves by sharing this tool. So, here it is: Format Finder.

Format Finder presents you with continue.

tagged as , and

Haskell's Elegant Fibonacci Implementation in C#

Haskell is a high-level, functional, programming language. Its combination of higher-order functions and lazy evaluation can lead to beautifully elegant algorithm implementations. One such implementation is the Fibonacci series algorithm:

let fib = 1 : 1 : zipWith (+) fib (tail fib)

This creates a variable called fib that contains an infinite sequence of Fibonacci numbers. We could print this variable, but our program would start trying to print an infinite number of elements. A better approach is to choose a finite number of elements from the sequence:

take 10 fib

The above implementation of fib fascinates me, so I decided to try my continue.

tagged as , , and