Much as I’ll admit to some spittle-flecked indignation when it comes to Visual Studio 20-anything, it has largely been the development tool that has paid my mortgage and allowed me to eat biscuits and cake. But this one takes the biscuit, cake, mug of tea and plate.

I’m happily working away, writing code when suddenly find in files stops working with the wonderful message “No Files Were Found To Look In. Find Was Stopped In Progress” Ah it’s simple. I’ve obviously fiddled with the search options and not selected the file types to search in or something similar. Nope –  everything looks just fine.

Ah I know. Close devenv and start again. Nope – exactly the same.

So, off for a coffee and moan and someone says: “Press Ctrl+Scroll Lock a few times and it’ll be fine”. This sounds a bit Mornington Crecsent to me, but it worked.

Sometimes I wonder if I should just go back to using notepad.exe

Today seems to be the day for posting useful stuff on the blog. Actually, it’s just so I can quickly useful find bits of code I might need again.

So I’ve a got a web page that displays some text a user has entered that may contain URLs and I’d like them to be displayed as actual hyperlinks.

 

public string ConvertUrlsToLinks(string msg)
{
string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
Regex r = new Regex(regex, RegexOptions.IgnoreCase);
return r.Replace(msg, "$1").Replace("href=\"www", "href=\"http://www");
}

 

Not that this is is going to compete with a full blown keyword or tag service like Google offer, but I needed a very quick way to suggest keywords and tags as users filled out a blog form. There’s no fancy thesaurus or library of data, but it gives surprisingly good results. At least, it gives the user a few hints and ideas about what they should put in as a keywords.

public string GetKeywords(string text, int minCount, int minLength, string additionalWords)

{

string keywords = "";

Dictionary dict = getKeywords(text, minCount, minLength);

foreach (var entry in dict)

 

keywords += entry.Key + ",";

if (additionalWords.Length > 0)

keywords += additionalWords;

return keywords.Trim(',');

}

private Dictionary getKeywords(string text, int minCount, int minLength)

{

text = StripHTML(text);

text = StripCrap(text);

text = text.ToLower();

var stopWords = new string[] {"about","above","across","after","again","against","all","almost","alone","along","already","also","although","always","among","an","and","another","any","anybody","anyone","anything","anywhere","are","area","areas","around","as","ask","asked","asking","asks","at","away","back","backed","backing","backs","be","became","because","become","becomes","been","before","began","behind","being","beings","best","better","between","big","both","but","by","came","can","cannot","case","cases","certain","certainly","clear","clearly","come","could","did","differ","different","differently","do","does","done","down","down","downed","downing","downs","during","each","early","either","end","ended","ending","ends","enough","even","evenly","ever","every","everybody","everyone","everything","everywhere","face","faces","fact","facts","far","felt","few","find","finds","first","for","four","from","full","fully","further","furthered","furthering","furthers","gave","general","generally","get","gets","give","given","gives","go","going","good","goods","got","great","greater","greatest","group","grouped","grouping","groups","had","has","have","having","he","her","here","herself","high","high","high","higher","highest","him","himself","his","how","however","if","important","in","interest","interested","interesting","interests","into","is","it","its","itself","just","keep","keeps","kind","knew","know","known","knows","large","largely","last","later","latest","least","less","let","lets","like","likely","long","longer","longest","made","make","making","man","many","may","me","member","members","men","might","more","most","mostly","mr","mrs","much","must","my","myself","necessary","need","needed","needing","needs","never","new","new","newer","newest","next","no","nobody","non","noone","not","nothing","now","nowhere","number","numbers","of","off","often","old","older","oldest","on","once","one","only","open","opened","opening","opens","or","order","ordered","ordering","orders","other","others","our","out","over","part","parted","parting","parts","per","perhaps","place","places","point","pointed","pointing","points","possible","present","presented","presenting","presents","problem","problems","put","puts","quite","rather","really","right","right","room","rooms","said","same","saw","say","says","second","seconds","see","seem","seemed","seeming","seems","sees","several","shall","she","should","show","showed","showing","shows","side","sides","since","small","smaller","smallest","so","some","somebody","someone","something","somewhere","state","states","still","still","such","sure","take","taken","than","that","the","their","them","then","there","therefore","these","they","thing","things","think","thinks","this","those","though","thought","thoughts","three","through","thus","to","today","together","too","took","toward","turn","turned","turning","turns","two","under","until","up","upon","us","use","used","uses","very","want","wanted","wanting","wants","was","way","ways","we","well","wells","went","were","what","when","where","whether","which","while","who","whole","whose","why","will","with","within","without","work","worked","working","works","would","year","years","yet","you","young","younger","youngest","your","yours"};

var words = Regex.Replace(text, @"[,.?\/;:\(\)]", string.Empty).Split(' ');

var occurrences = words.Distinct().Except(stopWords).Select(w =>

new { Word = w, Count = words.Count(s => s == w) });

return occurrences.Where(wo => wo.Count >= minCount && wo.Word.Length >= minLength)

.ToDictionary(wo => wo.Word, wo => wo.Count);

}

 

minCount the minimum number of duplicate words to catch
minLength the minimum length of word to word on
additionalWords is a set of comma separated word you want tagged(!) to the end of the string. I just use this to force certain keywords and tags

So taking the content of this page (less the code snippet) like this:

string tags = GetKeywords(articletext, 2, 4, "dave");

results in:

“keywords,tags,minimum,word,dave” which isn’t a lot different from what WordPress generated for me automatically, so I used it for my post tags.

I had cause to visit my good friend, Mr Sherlock Holmes, one day last month to discuss a number of matters. Mrs Hudson let me in, took my hat and coat and ushered me to the library where I found Holmes deep in conversation around the table with a rather tall, bearded gentleman with a very shiny hat and oppressively respectable frock-coat. They were studying a large pile of manuscripts. I was about to withdraw, but Holmes quickly beckoned me in, indicating he wanted the door closed.

 “My dear Watson, you couldn’t have come at a better time”, he said cordially.

“You looked otherwise engaged”.

“Very much so”, he replied. “But I should like you to meet Colonel Kerr”. The tall man turned, walked towards me, gave a bob of a greeting and offered his hand.

“Colonel Kerr, this gentleman has been my partner and helper in many of my successful cases and I’m sure will be able to offer some assistance. Colonel Kerr, please meet my good friend Dr Watson”.

We shook hands, he gave a quick little glance from his small, piercing eyes and returned to the table to resumed his studies.

“Watson, I just need to quickly conclude a few matters with Colonel Kerr before he leaves. He has to catch his sailing to South Africa that departs from Southampton this afternoon. Once he has gone, I will explain everything. I know you share my enthusiasm for the bizarre and unconventional puzzles and this promises to be a very interesting one.”

Holmes’s quick eye noticed my questioning glances and gestured towards the side cabinet. “Help yourself to some fruit. Colonel Kerr was kind enough to bring fresh supplies from his travels when he arrived this morning.”

Holmes joined the Colonel at the table and they resumed their study of the manuscript with whispered excitement.

I inspected the large bowl of fruit on the side cabinet with interest. It was well stocked and it had been a while since I had eaten bananas. I’ve always had the suspicion the banana exporting countries conspire to make bananas increasingly difficult to peel. The banana tops refuse to snap open like they used to and one has to resort to a knife. And these bananas were no different. I had the devil of a job with each banana I tried, so decided to abandon my attempts.

“Watson. I’ve decided to accompany the Colonel to the station. We have some more business to complete and the Colonel is late for his train”. Holmes and the Colonel had gathered their papers and were rushing out of the library. “Mrs Hudson will see you out. Meet me here tomorrow at 2pm”.

I heard the gathering of coats, the front door shutting and all was quiet. A sudden still hit the room. Again, Holmes had left me very much in the dark with the promise of revealing all. As I turned to leave, I noticed something on the table. A small pile of banana skins that had been obscured by the mountain of manuscripts was clearly visible. I wandered over to examine the evidence. How on earth had they been peeled?

The banana skins had been torn into many pieces, so it was impossible to fathom any clues. I decided to wait until tomorrow and left Holmes a short note.

It was a quarter-past one when I started from home the following day. I made my way across the park, and so through Oxford Street to Baker Street. I climbed the stairs to the door, knocked and was greeted by Mrs Hudson.

“Sir is in the library,” she said while taking my coat.

“Ah Watson”. Holmes was clearly brimming with self satisfaction as he always did when he’d solved a mystery. “Your note and the pile of banana skins deprived me of sleep last night,” he said buttoning up his pea jacket and taking his heavy hunting crop from the rack. “We’ve solved many mysteries together. Most of them seemingly unsolvable, some impossibly so. However, I confess I had to resort to outside help on this one”.

Holmes pulled a crumpled telegram from the inside pocket of his pea jacket, tapping it with his crop.

“It seems Watson, we need to observe the animal world more closely. This telegram from Colonel Kerr I received this very morning, has made me realize that sometimes we are not as intelligent as we think we are”.

He continued to tap the telegram with his crop with each syllable, so emphasizing his point.

“Colonel Kerr simply peeled those bananas by watching how monkeys do it. No matter how un-ripe a banana is, a monkey will always successfully peel it from the wrong end.”

Holmes obviously took a certain relief from explaining this and shrugged his shoulders.

“So, remember to always peel bananas from the wrong end,” he retorted.

“Nice retorting!” I retorted.

OO purists have long argued that switching on the type of an object by using some conjured up member enum is wrong. And I agree. Fundamentally, it’s nasty and you can satisfyingly waggle your finger at poor OO design.

However, sometimes things aren’t so easy and straightforward. I’m currently working with a complex financial trade class hierarchy that simply can’t be changed – it just wouldn’t be practical. I needed some new functionality that switched on an instance of the base class of the hierarchy. Ok, so it breaks all the rules, but at least it’s switching on a common base type and not completely unrelated types. Peter Hallam’s article describes the problems with a generalised type switch built into the language and why it’s not practical.

So what follows is stream of software design consciousness that touches on the relative casting performance of C# as vs. is, lambda functions, generics and fluent interfaces (method chaining) that achieves a solution.

Lets assume we have some simple class hierarchy. These classes do nothing, but demonstrate the principles involved. We’ll use these throughout the examples.


// Our base class
public class Trade
{
    public void TradeMethod()
    {
    }
}

public class GenericTrade : Trade
{
    public void GenericTradeMethod()
    {
    }
}

public class SwapTrade : Trade
{
    public void SwapTradeMethod()
    {
    }
}

public class SpotTrade : Trade
{
    public void SpotTradeMethod()
    {
    }
}

A first stab at switching on type might be like this:


public void doSomething(Trade trade)
{
    if (trade is GenericTrade)
    {
        GenericTrade genericTrade = trade as GenericTrade;
        genericTrade.GenericTradeMethod();
        // ....
        // ....
    }
    else
        if (trade is SwapTrade)
        {
            SwapTrade swapTrade = trade as SwapTrade;
            swapTrade.SwapTradeMethod();
            // ....
            // ....
        }
        else
            if (trade is SpotTrade)
            {
                SpotTrade spotTrade = trade as SpotTrade;
                spotTrade.SpotTradeMethod();
                // ....
                // ....
            }
            else
            {
                // Default actions
                // ....
                // ....
            }
}

Well it works, but it has a couple of problems.

  • C# offers the syntactically concise way to upcast and downcast using as and is. Unfortunately as Emilio Guijarro shows, the relative performance of ‘as’ vs ‘is’ when typecasting means ‘is‘ is pretty slow.
  • Somehow, the if…else.. If …else structure seams very clunky when there’s more than a few types.

To get around using the slow ‘is‘ is easy.


public void doSomethingElse(Trade trade)
{
    GenericTrade genericTrade = new GenericTrade();
    SwapTrade swapTrade = new SwapTrade();
    SpotTrade spotTrade = new SpotTrade();

    if (genericTrade as GenericTrade != null)
    {
        genericTrade.GenericTradeMethod();
        // ....
        // ....
    }
    else
        if (swapTrade as SwapTrade != null)
        {
            swapTrade.SwapTradeMethod();
            // ....
            // ....
        }
        else
            if (genericTrade as GenericTrade != null)
            {
                genericTrade.GenericTradeMethod();
                // ....
                // ....
            }
            else
            {
                // Default actions
                // ....
                // ....
            }
}

This requires an instance of each type, but the structure is even more horrible than before. So abandon that way of doing it. What we really need is something that is readable. Something like this would be ideal:


public void doSomething(Trade trade)
{
    switch (trade)
   {
      case GenericTrade:
            // ...
            // ...
            break;
      case SwapTrade:
            // ...
            // ...
            break;
       case SpotTrade:
            // ...
            // ...
            break;
        default:
            // ...
            // ...
            break;
      }
}

Bart De Swet’s article that describes a solution that yields a similar code structure. This is implemented using method extensions which is unnecessarily complicated, so I devised a similar solution using instance methods.

So here’s my take on a switch class – I’ve called it SwitchOnType for clarity:


public class SwitchOnType<T1>
{
    bool _break = false;
    private T1 _object;

    public SwitchOnType(T1 obj)
    {
        _object = obj;
    }

    public SwitchOnType<T1> Case<T2>(Action<T2> action) where T2 : class
    {
        if (_break == false)
        {
            T2 t = _object as T2;
            if (t != null)
            {
                action(t);
                _break = true;
            }
        }
        return this as SwitchOnType<T1>;
    }
    public void Default<T2>(Action<T2> action) where T2 : class
    {
        if (_break == false)
        {
            T2 t = _object as T2;
            if (t != null)
                action(t);
        }
    }
}

And here’s how it’s used with our class hierarchy example:


public void doSomething(Trade trade)
{
    new SwitchOnType<Trade>(trade)
        .Case<GenericTrade>(genericTrade =>
        {
            // It's a GenericTrade
            genericTrade.GenericTradeMethod();
            // ...
            // ...
        })
        .Case<SwapTrade>(swapTrade =>
        {
            // It's a SwapTrade
            swapTrade.SwapTradeMethod();
            // ...
            // ...
        })
        .Case<SpotTrade>(spotTrade =>
        {
            // It's a SpotTrade
            spotTrade.SpotTradeMethod();
            // ...
            // ...
        })
        .Default<Trade>(t =>
        {
            // Default actions
            t.TradeMethod();
        });
}

Although there’s a few extra {‘s and )’s, the structure is close to the ideal. This is mainly possible using method chaining (also known as fluent interfaces), by returning this (well a genericized this) in the case method and passing the code to be executed if the case test succeeds as a lambda function. Note if none of the Case methods succeeds, the Default method will get called. If you want to know more about fluent interfaces read Martin Fowler’s article.

So just accept that sometimes you have no choice and have to accept that it’s right to do what’s wrong – is that software or life. Or both.

“Is a URL a URI?”
“Is a URI a URL?”
“What does URL stand for?”
“What does URI stand for?”
“What does URL mean?”

I’ve been asked so many times I’ve lost count and for the sake of my sanity, I’ve decided to explain their distinquishing marks.

URL  – Uniform Resource Locator
URI – Uniform Resource Identifier

Sometimes these achronyms are used interchangably which is almost entirely wrong. Basically a URL is a specific type of URI because it uses specific protocols like http:  ftp: and mailto: URI defines a larger, more generalized superset.

Effectively, URL is no longer a valid in technical conversations or documents and you should use URI regardless of how big a pedant you are. A URI describes both the way you access something and it’s designated location.

You may see some explainations where U is defined as Universal. This is wrong and the author is just confused.

So, let’s call the whole thing off.

Intrigue is curious thing, much as curiosity is intriguing. What drives it? Mostly just needing to know something for no other reason than wanting to know itself – curious indeed. 

So why would I really be bothered to know what WindowsFormsSyncronizationContext does under the hood? Well dear bleader (if someone who write blogs is a blogger, maybe someone who reads blogs is a bleader) I had my suspicions, but I just needed to find out.

SynchronizationContext and it’s derivatives was introduced in .NET 2.0 as simple .NET language construct to allow a non-GUI thread to call methods on a GUI control. Previously, code had to be written to perform the InvokeRequired, BeginInvoke, Invoke dance and as has been discovered, this has problems and  can have you chasing nasty bugs.

In http://davebrooks.wordpress.com/2007/02/12/begininvoke-the-land-of-confusion/ I discussed BeginInvoke for delegates and controls, some issues and how they worked under the hood.

As I said before, I was suspicious that WindowsFormsSynchronizationContext worked pretty much the same way. And Lo, with a bit of windows message spying on a simple test app, my suspicions were confirmed. WindowsFormsSynchronizationContext.Post() and .Send() end up using PostMessage with registered windows messages, presumably using BeginInvoke and Invoke respectively to push the control method call onto the main GUI thread.  However, it does make the code earlier to write and as we all know, less code means less bugs.

Now to find out what happens in the Freemasons. I’m just curious you understand.

Follow

Get every new post delivered to your Inbox.