Growing with the Web

Monday, 20 May 2013

Change the tab size on GitHub using a custom style extension

As far as I'm aware you can't change the tab size on GitHub when viewing code on the web interface (yet). While I'm sure this feature will eventually be added, in the meantime it kind of sucks to write your code using 4 character tabs and then having it mangled up when viewing on the web UI.

Ew.

But don't despair, there is a fairly easy fix. We can use the tab-size CSS property to define the tab size on the page.
Tuesday, 14 May 2013

Disabling the context menu with Dart

To disable the context menu on an element using Dart, simply add a listener to onContextMenu which calls e.preventDefault() to stop the event from bubbling up.
element.onContextMenu.listen((MouseEvent e) => e.preventDefault());
Sunday, 12 May 2013

One year

Just over a year has passed since I created my blog and I just wanted to reflect a little on how I feel everything has gone, the impact it's had on my life and go over the most popular posts to date.

Getting stuff done

My life has changed so much over the past year, on both a personal level and professional. I created my blog because I wanted to be better at what I do, I found it to be an incredible motivational tool especially since I made a commit to myself (and others) about posting. When I started, I wanted to post at least once a week and I ramped that up to twice a week in the middle as I felt I could do more. I mostly kept my promises which I'm very happy about and it just illustrates the power of blogging and making things public.

If you have been following me for a whole you probably could guess I also promised myself to brush up on my algorithms and data structures also. During the past year I read The Algorithm Design Manual cover to cover which taught me a lot and really changed the way that I develop software, I used to struggle determining an algorithm's time complexity for example but now it's a piece of cake.

I also released my first app to the Play Store, a widget to aid in Korean language vocabulary acquisition built primarily for myself. I have another app which I've been working on but I've put it on hold due to other work.

I became active on Stack Overflow and had a great time in the process, it's such a cleverly designed system how it encourages good/helpful behaviour by making it fun. I went really hard at this for a while managing to rack up a whopping 468 points on one day.

Saturday, 4 May 2013

Using relative directions in content on the web

You've all seen it before, you hit a webpage with a menu on the left and somewhere in the main section it says something like "for more look at the left menu".


Sure it's fine for the majority of us, but it is pretty bad practice for a couple of accessibility reasons. Firstly there is no concept of left and right for people using assistive technologies like JAWS to read the page for them. Secondly what if your website is responsive in some way or your content appears in different forms like an app or mobile site, you need to ask yourself if it is really left or right in all these cases.
Monday, 29 April 2013

Use .Any() in your LINQ to SQL queries

When using LINQ to SQL to check whether either no records exist or at least 1 record exists, be sure to prefer .Any() over .Count() as the SQL will be optimised to only get the information required. .Any() will use EXISTS in SQL which stops as soon as a record is found whereas .Count() uses COUNT(*) which goes through all the records to get the number matching the query.

Consider the following code samples:
var q1 = TableName.Count() > 0;

var q2 = TableName.Any();
They will produce the following SQL:
-- q1
SELECT COUNT(*) AS [value]
FROM [TableName] AS [t0]

-- q1
SELECT 
    (CASE 
        WHEN EXISTS(
            SELECT NULL AS [EMPTY]
            FROM [TableName] AS [t0]
            ) THEN 1
        ELSE 0
     END) AS [value]
As you can see the second sample produces some admittedly uglier SQL but is much more efficient.
Sunday, 28 April 2013

Check out the deleted questions on Stack Overflow

There are a lot of questions on Stack Overflow, probably a lot than you realise. This is understandable when you consider over 7,000 questions are being asked every day. No I'm not just talking about the 5 million odd that you can access via the questions tab (4,930,418 and counting at time of writing). There are also a ridiculous amount of deleted questions which only users with at least 10,000 reputation can see.

Most of the deleted questions are just plain awful questions that don't belong on the site. A good number of them however were deleted not because they were particularly poor quality or bad questions, but because they were off-topic and don't really belong on Stack Overflow, they also may have been too old to migrate to a site like Programmers.

Greg Hewgill created a list of old deleted questions ranked by votes, being outside Stack Overflow so people who don't have the reputation can check them out. A lot of them are great demonstrations of what not to ask on Stack Overflow now that the rules are much more rigorously defined on what is on-topic. They are a lot of fun though :) check out the list here.

Merging Dictionary objects

Say you have two (or more) Dictionary objects and want their contents merged, this can be done with LINQ very conveniently like so.
var mergedDictionary = dictionaries
    .SelectMany(e => e)
    .ToLookup(e => e.Key, e => e.Value)
    .ToDictionary(e => e.Key, e => e.First());
There is a major issue with this method though in that it's quite horrible performance-wise, the reason being because of the amount of conversions and unnecessary work that is happening. Firstly SelectMany is flattening the IEnumerables into a single IEnumerable, then we're converting the new IEnumerable into a Lookup and then we're converting the Lookup into a Dictionary taking only the first value for any key in the Lookup to resolve duplicates.

This is one of the problems with LINQ, everything is so simple and easy to use, but if you don't understand what is going on then a LINQ query can have a serious hit on the performance of an algorithm.