Wednesday, August 31, 2016

To Var or not to Var


I got asked today about my use of “var” in .NET. As it happens in every debate, I forgot all reasons of why I use “var.” After thinking about it, the main reason is, and always will be, “I like var.;” it’s concise, it’s easy to read, and it saves me time typing more complex types. I have never found myself wondering what type the variables is, so I don’t care about the way I declare its type. I only care about how I use that type. But unlike Jim Jeffries common argument for guns: “Fuck you! I like guns!” (as described in his standup on YouTube:, which I agree is a perfectly valid argument for doing something, there is more reasons to using “var.” So I will talk about each one in turn.

  1. It’s as readable as the implicit type declaration. It makes no difference whether you declare your type before or after the variable. The following are the same.

    List<Widgets> widgets;

    var widgets = List<Widgets>();

  2. I don’t have to repeat myself. I am lazy, even more so when I write code. I can “get two birds stoned at once!” So doing this:

    var widgets = List<Widgets>();

    Is easier to write than this:

    List<Widgets> widgets = new List<Widget>();

  3. I get the benefit of my variable never being null! No more “Object reference not set to instance of an object!” Sure, this is not a problem for simple types like string, int, but it’s a pain in the ass for and lists like List<Widget>. How many times have you declared your list, forgot to initialize it, then try to put junk in it?! I know I have!
  4. Using “var” is only allowed in local scope, which means that every time I declare something with “var”, you know it will be only available in the local scope. Thank you Captain Obvious!
  5. I don’t care what the compiler does with it when it compiles! It’s none of my business.

A Sidebar on Code Readability

The bigger issue with readability that most folks miss, is how you name your variables and how you structure your code. Here are a couple of things that make code much harder to read:

  • Variable names after single letters. “L” or “R” might mean something to you if you wrote the code, but to anyone new looking at it, it might as well be Greek. Which is great if you are Greek but not so much for everyone else. How about “Log” or “Result?”
  • Function size. If your function is bigger than a page, and I have to scroll to read it, you are doing it wrong.
  • Inconsistent capitalization. A widely accepted standard is that parameters are camelCase, private _variables are small case and can start with “_” and public Properties are title case.
  • Too many parameters. If a function has more than 2-3 parameters, it’s time to make a object to pass those parameters. Readability goes out the window when function parameters start spanning multiple lines.
  • Silly patterns that somehow still persists. By that I am talking about variables having the type letter in the name, such as “bFlag” or “iNumber.” Is “b” byte, is it bool? A better choice would be “IsFlag.” There is nothing to be gained by specifying the type in the variable name. The IDE already knows the type, and you have all the intellisense associated with that type.
  • Multiline strings. Inline SQL is the poster child for this. A slightly better approach is put your multiline string at the bottom of your class and use some string replacement to fill out it’s values at runtime. Use verbatim strings if you want multiline strings that you can copy/paste in other places. Or better, use an ORM, that’s what they are for!


Credit where it’s due, in no particular order: Burton Rheutan, Endurance Idehen, James Allen.

Wednesday, March 23, 2016

Installing Software on Windows the Easy Way (Using Chocolatey)

I've been using Chocolatey for a while now, and today I want to share the coolness, for those of you that haven't heard of it or used it. It’s much like apt-get for Windows (automatic way to install software from the web).

You install it by running this on the command line:

@powershell -NoProfile -ExecutionPolicy Bypass
-Command "iex ((new-object net.webclient).DownloadString(''))"

After that, let’s say you want to install Chrome, it’s simple as (running from CMD)
choco install chrome

or EverNote:
choco install evernote

or Node:
choco install nodejs.install

You just have to remember to run CMD as administrator. Bypass the license prompt by using the "-y" command argument. You can install any of the 3779 packages it supports. They are listed at

Monday, March 14, 2016

Using NUnit 3.2 with ReSharper Ultimate 10

I just upgraded my ReSharper to 10 in the hope that it will fully support NUnit 3.2 (the most current version as of the time of me writing this). However that didn’t turn out to be the case. I still had to Google around to find why ReSharper won’t run my tests while using NUnit 3.2. The solution turned out to be simple – install the ReSharper NUnit runner for NUnit 3 through the package console and restart Visual Studio!

Install-Package NUnit.ReSharperRunner3

Sunday, March 13, 2016

Poor Man’s Batch Encoding with HandBrake

I wanted to have a way to batch encode a directory (or even multiple directories) full of files without having to use the HandBrake interface to queue each directory. Ideally, I will do this in .NET in the long run but for now, here is a simple way to walk through all the directories and encode all files with the same HandBrake settings.

How it Works

  1. Deletes any existing files with the names “_encode_info.log” and “_encode_progress.log”
  2. Gets all the files in the directory and sub directories where it runs, ignoring itself
  3. For each file it finds:
    1. it gets the name of the directory the file is in, creates a directory with the same name under the “destinationPathRoot.”
    2. Sets the name of the output file to be under the newly created directory under “destinationPathRoot”
    3. If the file already exists, it sets it’s name to have the current date and time (to avoid overwriting)
    4. Calls the EncodeVideo function with the source file path and destination file path
    5. If the destination file path exists after the encoding, it deletes the source file path


  • handBrakeExecutable – Set the path to the HandBrakeCLI executable
  • destinationPathRoot – The root of the path where to put the encoded files

How to Use It

Copy the contents below and paste it in a .bat file. I name mine according to the HandBrake arguments I’m using. For example “_encode-Q25-1Audio,bat” (meaning using variable quality of 25 with 1 audio stream). Copy the .bat file in the root of the directory where your videos reside. Run it.


@echo off
setlocal enabledelayedexpansion enableextensions

rem Set the path to the Handbrake executable
set handBrakeExecutable=Path\To\HandBrakeCLI.exe

rem Set the root path where files will be encoded to
set destinationPathRoot=Path\To\Done\Videos

if exist "%CD%\_encode_info.log" del "%CD%\_encode_info.log"
if exist "%CD%\_encode_progress.log" del "%CD%\_encode_progress.log"

rem Get all the files in the directory and sub-directories
for /f "tokens=* delims=|" %%G in ('dir /s /a-d /b *.*') do if not %%~xG==.bat (
    rem Set the source file path
    set sourceFilePath=%%G

    rem Get the current directory and remove the trialing slash
    set currentDirectory=%%~dpG
    set currentDirectoryNoTrailingSlash=!currentDirectory:~0,-1!

    rem Get only the name of the current directory
    for %%f in (!currentDirectoryNoTrailingSlash!) do set currentDirectoryName=%%~nxf

    rem Create the path to the destantion path based on the current directory name
    set destinationPath=!destinationPathRoot!\!currentDirectoryName!

    rem if the destination doesn't exist, create it
    if not exist !destinationPath! (
         mkdir !destinationPath!

    rem Process the file
    echo File: %%G

    rem Set the destination file based on the destination path and the current file name
    set destinationFilePath=!destinationPath!\%%~nG.mp4

    rem If the filename already exists, append the current date and time to it
    if exist destinationFilePath (
        rem Get the current date and time in MMDDYY_HH_MM_SS format
        for /f "tokens=1-8 delims=:./ " %%G in ("%date%_%time%") do (
            set currentDateTime=%%G%%H%%I_%%J_%%K

        set destinationFilePath=!destinationPath!\%%~nG-!currentDateTime!.mp4

    rem Call Handbrake to encode the file
    call :EncodeFile "!sourceFilePath!", "!destinationFilePath!"

    rem Delete the source file if the destination exists
    if exist !destinationFilePath! (
        del "!sourceFilePath!"


rem echo Source: %~1
rem echo Destination: %~2

rem Call HandBrakeCLI to encode the file
"!handBrakeExecutable!" ^
-I "%~1" ^
-o "%~2" ^
-f mp4 ^
-O ^
--decomb="fast" ^
--crop 0:0:0:0 ^
--strict-anamorphic ^
--modulus 2 ^
-e x264 ^
-q 25.0 ^
--cfr ^
-a 1 ^
--encoder-level="4.0" ^
--encoder-profile=high ^
1> _encode_progress.log ^
2> _encode_info.log


Wednesday, April 29, 2015

OctopusDeploy CCTray Integration

My CCTray integration for OctopusDeploy is now fully functional!

Check it out if you want CCTray to report the status of your OctopusDeploy builds.

You can find the source code on GitHub with an example web forms page that outputs the XML.
// //]]>