Andy Crouch - Code, Technology & Obfuscation ...

2017 Retrospective

Man Standing In Desert

Photo: Unsplash

Well, it’s a week before Christmas and this will be the last post I write for 2017. I thought a little 2017 retrospective would be fun.

What went well

I started this blog back in January as a way to get back into writing on a regular basis. My aim was to post once a week as I worked on some different side projects.

The process has worked out quite well. I have posted 44 times throughout the year only skipping weeks I was on holiday. I wrote about the fact that I procrastinate about everything early in the year. I have worked to overcome this and have found the process of finishing a post much easier as we get to the end of the year,

The continued success and growth of Open Energy Market has been exciting to be a part of. From expanding the team to helping guide the company through interesting growth issues. It has been a busy but very productive year,

What needs improvement

While I am happy with how the first year of the blog went the main negative is that it didn’t always go to time. While I always created a post a week it didn’t always get posted. The reasons for this including the process of writing and procrastination. As I overcame these issues time became my biggest foe.

Due to the time and effort required by Open Energy Market this year I did not get to work on any side projects. Any spare time was dominated by research for issues we faced at Open Energy Market or reading on business leadership.

Next steps.

In 2018 I plan to make a small amount of time a day available to work on something new and personal. I want to hit a post a week again on time every Tuesday.

One of the things that I am very keen to learn more about is functional programming. Having had my interests piqued earlier in the year by a Clojure fanboy, Elixir is very much on my radar. As I move forward in learning more of that I will write my thoughts and idea’s here.

Finally, I am now heading off for a week to enjoy Christmas with my family. So I wish anyone reading this a Merry Christmas (if that’s your thing) and I will be back in 2018.

Frustrating UI's

Frustrated Man In White Shirt Looking At Mac

Photo: Tim Gouw - Unsplash

UI’s can be the difference between a killer application and something that someone has to use.

Well crafted UI’s focus the user’s attention on the task they are undertaking. It presents the data in a clear and concise manner. It will also allow the user to complete the task in the simplest manner with the least effort.

Sometimes that is not the case

Take the screen below

Azure Screenshot

(Some data is obscured for obvious reasons)

Azure’s UI is like Marmite, some people love it and some do not. On the whole I have grown to like many parts of the UI. I also understand they are improving the service and UI all the time incrementally.

But, look at the picture again. You will see that there is no way to expand the table columns that actually contains the log message. I found this out when having issues setting up a data sync. The process kept failing and leaving nice long log messages for me. The only way I could get to read the whole message was to hover over the log column. Then the message displayed in a tooltip. Very frustrating.

I am someone that will Google furiously before raising a support ticket. Not being able to copy the data hampered this. In the end, the only way I could find to copy the data was to use Chromes dev tools. A quick inspection later and I copied the data from the elements title attribute to use in my searches.

Now I am picking on Azure here and that’s not entirely fair. I would guess though that there are many frustrating application UI’s out there.

Tell me which ones get to you the most on twitter or email.

Regex Use

Black And White Giraffe

Photo: Unsplash

Regex’s hit the headlines recently when someone wrote an article on their performance. I don’t remember the exact details but they turned code that took days to run into hours by removing the regex implementation.

Regex’s have their place. But most (non-Perl) developers don’t use them often enough to know how to use them correctly. Be honest when was the last time you used one without an ample amount of Googling?

I came across an example today. I couldn’t remember off the top of my head how to get an URL parameter in JavaScript. Every example I came across did something very similar to

var parseQueryString = function(url) {
  var urlParams = {};
  url.replace(
    new RegExp("([^?=&]+)(=([^&]*))?", "g"),
    function($0, $1, $2, $3) {
      urlParams[$1] = $3;
    }
  );
  
  return urlParams;
}

Now, I am not saying this is wrong but using Regex’s for something like this feels like cracking a nut with a sledgehammer.

After searching my notes I found the following solution

function getUrlParameterFrom(url, parameterKey) {
    var url = new URL(url);
    return url.searchParams.get(parameterKey);
}

(If you don’t want to pass URL, you can just substitute for window.location)

Isn’t that a cleaner solution using the available functionality of the language/framework? No need to Google and figure out the pattern you need. Simple to read. Easy.

What’s your views on Regular Expressions? Do you use them frequently? Message me via twitter or email to discuss.

Questions I Get Asked This Time Of Year

Oriental Resturant Table

Photo: Unsplash

It comes to this time of year and I find myself out and about more at various functions and parties. As someone that doesn’t drink very much and who is a very strict vegetarian, I get asked a common set of questions. I thought that this year I would write down the answers and direct people here (at least when they are sober).

No, I hardly drink at all. Yes, I know that is hard to appreciate for some people. No, it’s not because of medical reasons or addiction, it’s by choice. When my wife was pregnant I cut out smoking and drinking as an act of support. I found I missed neither but I did enjoy the feeling of a clear head and improved cognitive functions. In all truth, I just didn’t miss it at all. So I never started drinking again. No, I do not miss it. No, it doesn’t mean I am boring. Yes, I occasionally do have a drink when I fell like I want one. No, I haven’t been drunk for over 12 years and that’s the way I like it. I appreciate that some people struggle with this choice but that’s their problem and not mine. (Oh and yes my wife does drink but not to excess and neither of us started smoking again.)

Being a veggie is a more fundamental life choice and I have not eaten animal-based products for over 20 years. This surprises a lot of people given my rounder appearance! It is even more of an issue for some people than not drinking.

I had never really enjoyed meat and had slowly reduced my consumption during my teens by choice. As a child, I started to suffer from migraines that would cripple my head and eyes. I also suffered from some stomach issues as I reached my late teens. On researching them they could have been caused by an intolerance to meat. At this point, I was only eating it occasionally and so cut it out altogether. Instantly my stomach issues vanished and within 6 months my migraines reduced significantly.

“Oh, what do you eat”, “Do you have to live on Tofu”, “You don’t look like a veggie” and “eating must be really boring” are all commonly said to me. These are closely followed by “I can’t live without meat”, “Don’t you regret it when you smell a bacon sandwich” and “I bet I could tempt you to eat meat again”.

I eat a perfectly balanced diet of salad, veg, Tofu and various vegan-friendly foods. I actually like it. I have never, ever, missed eating a bacon sandwich although the smell of a Donner Kebab still makes my nostrils twitch (that could be more to do with the chilli sauce). Please don’t buy one though as I will not be tempted for anything. I like the smell of certain, scented candles, I don’t want to eat one.

If you are lucky enough to go out for a meal with me or to cook for me I am actually really easy. I do not impose my choices on anyone else. I will eat dairy (cheese mainly) based dishes when out to ease the situation and I am happy with whatever you serve. If we are out as long as there is a burger or something meat-free I am happy.

In much the same way I wouldn’t comment on your chosen religion then there isn’t really much you need to say to me about what I eat.

So there you go. I am a non-drinking veggie with good reasons and who you cannot turn with a Big Mac.

I’m fine with it and so should you be.

C# Extension Methods

Glasses In Front Of Screens

Photo: Kevin Ku - Unsplash

Code is all about readability but in itself is not very readable. One of the common statements you learn about structuring your code is to make it flow like prose. That is improved by clean naming and logical structuring and following clean code. But, languages get in the way of this by making you use operators such as !=. Ironic really.

.Net introduced Extension methods early in the history of the framework. The documents state that:

“Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. “

You can read the full documentation here.

Extension methods are a special type of static method but you invoke them as an instance method. The following is an example of how to wrap up the IsNullEmptyOrWhiteSpace(string) String object method as an extension

    public static bool IsNullEmptyOrWhiteSpace(this String s)
    {
        return string.IsNullOrWhiteSpace(s);
    }

    var testString = "This Is A Test String";
    testString.IsNullEmptyOrWhiteSpace();  // false

The method has to be defined static wihtin a class that is public. It must also take a parameter matching the type it is extending as the first parameter, prefixed with the this modifier. Beyond these requirements, you can do what you want with extension methods.

One of the things that I like to do is negate methods so that you can lose the ugly ! Operator. In the case above I have a matching method defined as

    public static bool IsNotNullEmptyOrWhiteSpace(this String s)
    {
        return !string.IsNullOrWhiteSpace(s);
    }

    var testString = "This Is A Test String";
    testString.IsNotNullEmptyOrWhiteSpace();  // true

I find that this helps with readability and that as a project goes on you find lots of uses for Extension methods. I find you are able to actually define a DSL specific to your project which can be quite powerful. The thing I do not recommend is building business logic into your Extensions. They should be simple, type related methods that add value or improve readability.

Taking this a stage further, I have planned a library of common extensions designed to improve readability. I know that I prefer

    public static bool IsNull(this object o)
    {
        return o == null;
    }

And

    public static bool IsNotNull(this object o)
    {
        return o != null;
    }

Which gives us

    var someObject = new Object();
    if(someObject.IsNotNull())
    {
        // some code ...
    }

Instead of

    if(someObject != null)
    {
        // some code ...
    }

Doesn’t that make reading the code a little easier and clearer?

If you’d like to discuss the use of Extension methods or if you have extensions you would like to contribute, message me via twitter or email.