Naming Events in .NET

.NET Comments

Update 2016/12/29: I've complained about the On prefix in the comment section of the documentation (and Jonas Gauffin too). In fact the prefix was a mistake and is fixed by now.

Things are changing in the .NET world. A couple of days ago Microsoft released .NET Core 1.0, the new cross-platform, open source, and modular .NET platform.

Unfortunately, not only the managed framework's changing, but naming guidelines too.

Lets start with the old .NET framework. Microsoft says in the Naming Guideline for Events:

✓ DO name events with a verb or a verb phrase.

Examples include Clicked, Painting, DroppedDown, and so on.

✓ DO give events names with a concept of before and after, using the present and past tenses.

For example, a close event that is raised before a window is closed would be called Closing, and one that is raised after the window is closed would be called Closed.

The (slightly outdated) Event Naming Guidelines for .NET Framework 1.1 even says more explicitly:

  • Do not use a prefix or suffix on the event declaration on the type. For example, use Close instead of OnClose.

That's what we've been taught for the last 15 years: Events are named without a prefix.

Let's repeat: Events are named without a prefix.

Entry .NET Core!

Robin Müller, maintainer of the library, changed the names of all event from an unprefixed name to prefixed with On in a recent commit. I complained that this renaming would contradict the guidelines recommendations by Microsoft and what we've learnt the last 15 years.

However, Robin pointed me to the new guidelines (emphasis mine):

There are a number of conventions that you should follow when declaring an event. Typically, the event delegate type has a void return. Prefix event declarations with 'On'. The remainder of the name is a verb.

WTF? Do we have to forget the old habits and rename all events when moving from good ol' .NET Framework to .NET Core?

The new guidelines include a comment section at the bottom where I asked for the rational behind the changed guideline a few days ago, but I still got no answer. I'd really like to now...

Sidenote: Most of us are used to name the handler for an event On<name of the event>, e.g.

foo.SomethingHappened += OnSomethingHappened;

What should we call those event handlers now? OnOnSomethingHappened?

From Jekyll To Pretzel

Jekyll, Pretzel Comments

Some time ago I told that I converted my blog from CommunityServer to Jekyll on Github Pages. However, I was not satisfied with that solution. Running a static site has one drawback: redirects for moved pages.

When you change the structure of a web site, you don't want old links to reference now invalid URLs. That's would be called link rot. Instead, you want that users following a link to an old location are redirected to the new page automatically.

Actually, there are plugins for Jekyll for redirection, the most popular is jekyll-redirect-from. However, what it does is creating an HTML page at the old location with a HTTP-REFRESH meta tag pointing to the new URL.

I don't like this solution because the status code of the HTTP response is 200, indicating that the old URL is still valid. I guess that Google or other search engines pay attention to the HTTP-REFRESH meta tag, but nevertheless it's a bad idea for SEO reasons.

Instead, a correct implementation would return the status code 302, indicating that the page moved permanently. Unfortunately that's not possible generally for static web sites.

That was the main reason I ditched Github Pages. I didn't want to go back to self-hosting on a virtual server, so I decided to move my site to Azure. Additionally I took the chance and replaced the blog engine too.

Not being a Ruby guy, I searched for a similar blog system but written in .NET. There are a few, but the most appearing to me was the Pretzel. Pretzel is an open source blog system, behaving more or less the same as Jekyll. Additionally it supports several extension points, which I as a developer really like.

I'll write about running Pretzel on Azure and different Pretzel extensions I wrote in some future posts.

TL;DR: I wanted to have more control over my website, therefore I moved from Github Pages to Azure, and from Jekyll to Pretzel.

7Zip4Powershell in PowerShell Gallery

7-zip, Powershell Comments

PowerShell Gallery Logo

A few days ago the Preview suffix was removed from PowerShell Gallery. This gallery is a central repository for PowerShell content such as modules and scripts. You can read the public announcement in the Windows PowerShell Blog. You can find instruction how to take advantage of the gallery on its homepage, and more details on the Get Started page.

Until now I published 7Zip4PowerShell, my PowerShell commands to compress and expand files with 7-Zip, as a plain assembly. However, several users asked for a "real" PowerShell module. I took the public release of the PowerShell gallery as an opportunity to accommondate that demand. The 7Zip4PowerShell module is now available in the gallery, and installing it is as simple as invoking

Install-Module -Name 7Zip4Powershell

I'm very happy that the folks at Microsoft created that gallery, because it makes the discovery of interesting modules so much easier.

Saving bandwidth with Zopfli

Zopfli Comments

Today Jeff Atwood published the article Zopfli Optimization: Literally Free Bandwidth, praising the compression algorithm Zopfli. Zopfli was created by Google and published in 2013:

The Zopfli Compression Algorithm is a new open sourced general purpose data compression library that got its name from a Swiss bread recipe. It is an implementation of the Deflate compression algorithm that creates a smaller output size compared to previous techniques. [...]

The output generated by Zopfli is typically 3–8% smaller compared to zlib at maximum compression, and we believe that Zopfli represents the state of the art in Deflate-compatible compression. Zopfli is written in C for portability. It is a compression-only library; existing software can decompress the data. Zopfli is bit-stream compatible with compression used in gzip, Zip, PNG, HTTP requests, and others.

Jeff gave Zopfli a try and got impressive results:

In my testing, Zopfli reliably produces 3 to 8 percent smaller PNG images than even the mighty PNGout, which is an incredible feat.

However, Zopfli has one drawback. Its awesome compression ratio comes with a speed penalty, it's more than 80 times slower than gzip.

Because of its slowness Zopfli is not the best choice for compression at runtime. But where it really shines is when it's used for pre-compressed data. A very good candidate are PNG encoded images. There's even a Zopfli encoder for that purpose, ZopfliPNG.

So because of the proclaimed reduction of the size of PNGs, I gave ZopfliPNG a try. First I measured the current size of all PNG images of my site with this PowerShell command:

PS> gci *.png -Recurse | Measure-Object -Sum Length

Count    : 110
Average  :
Sum      : 4775284
Maximum  :
Minimum  :
Property : Length

That's about 4.6 MiB of PNGs. Than I let ZopfliPNG re-compress all these files:

gci *.png -Recurse | %{ zopflipng.exe -y --lossy_transparent $_.FullName $_.FullName }

A few minutes and 110 files later the command has finished. 56 files have been changed, i.e. ZopfliPNG was able to produce a smaller size for more than half of all images.

The entire size of all PNGs is now this:

PS> gci *.png -Recurse | Measure-Object -Sum Length

Count    : 110
Average  :
Sum      : 3616048
Maximum  :
Minimum  :
Property : Length

So from the former 4.6 MiB it went down to 3.4 MiB, that's a reduction by 26 percent. Quite impressive for just changing the compression algorithm.