Tuesday, 25 August 2015

Clean Code

I'm re-reading Clean Code by Robert C. Martin and a few quotes from the intro have really stuck out:

"Quality is the result of a million selfless acts of care - not just of any great method that descends from the heavens".

I like this quote a lot. I'd take the word "selfless" out of it though. When I'm writing code I'm often thinking about Future Me and trying to make his life as easy as possible. Every act of care, or short-cut not taken, makes Future Me's life that bit easier. I want Future Me to not curse Past Me.

The idiom "a bad penny always turns up" is cited too. This one rings true. The amount of times a short-cut taken in a fatigued moment has come back to bite me is unnerving!

Friday, 14 August 2015

Uri.TryCreate C#

Came across a strange problem at work yesterday:

was returning true.

On further investigation strings such as "foo:bar" will also return true. It seems a colon separating a few characters is enough to satisfy TryCreate's criteria of a valid URI being contained in the string. I assume colons are related to the username:password prefixes some URIs contain, but I would have assumed that in itself would not be enough.

Anyone know why this would be the case?

Friday, 7 August 2015

Using the Google Analytics API in .NET

An old application which talks to Google Analytics (GA) in order to produce some custom reports started throwing an error the other day. It turns out Google has discontinued the old way in which you connect to their GA API. They also seem to have been through a few iterations of how you connect to them since the application was originally written.

Unfortunately, details on language specific implementation examples (C# / .NET in my case) don't seem to be easily available. The official documentation appears particularly confused - it's been updated so many times you're never sure whether you're looking at the latest and greatest or some deprecated version.

After some searching about I found the following guide really useful: http://www.daimto.com/google-analytics-api-v3-with-c/

In essence it seems to boil down to the following:

1) You used to connect to the Google Analytics API with a straight-forward username and password combination. There is a token returned after successful credentials being supplied, which apparently didn't need to be used, at least in the code I was looking at.

2) Connecting now is done by registering your application in the Google Developer Console and then generating a public/private key pair, of which you'll use the private key in your application.

3) This application will have an email associated with it which looks like xx-yy@developer.gserviceaccount.com. You'll need to make sure this account is authorised to read the Google Analytics accounts you want to pull info from.

4) At the time of writing, in order to utilise the asymmetric key combo you need to nuget in the GoogleAnalytics .net library: https://www.nuget.org/packages/Google.Apis.Analytics.v3/. You use the private key to create an X509Certificate2 which you pass to the a new instance of the AnalyticsService.

It's a shame that by far the best documentation and examples are by third-parties! Of which I do not count myself one.

Monday, 3 August 2015

Working With DateTime

I recently listened to this fascinating and terrifying Hanselminutes podcast on the perils of working with datetime: http://hanselminutes.com/485/the-problem-with-datetime-nodatime-with-matt-johnson

It reminded me of another really good video on why working with datetimes in programming is hard and dangerous and generally error prone: https://www.youtube.com/watch?v=-5wpm-gesOY