The Poor Man's XLink-Like Thing

by Kofi Sarfo 23. September 2009 01:18

The following StackOverflow post is interesting not so much because of the text but how it's rendered.

Loading Stack Overflow post...

We begin with a <div> tag which contains an expected id attribute format and using a jQuery regular expression we're able to match all div elements with an "id" attribute beginning "RSSContent".


<div id="Container">
<div id="RSSBlock">
<div id="RSSContent200909222115"
runat="server"
title="http://stackoverflow.com/feeds/question/477962" />
</div>
</div>

The "title" attribute for each element matched is used in an HTTP post of content-type application/json to a web service which acts as a proxy to overcome cross-scripting JavaScript constraints. This returns the post above which I found useful recently.

It's not an elegant solution. I've hacked the div element, using the "title" attribute to hold the URL and the directory structure isn't great either. The web service creates a user control which appears to work only in the root directory and the web application project requires a reference to an identical control in another project. The things we do for code compilation!

The end result is that rather than cut & paste, which would have been far easier we're using using an external resource to supply text so that if the RSS feed item was to change, for example, then we'd still display the most current version.

An XLink implementation, this is not. It's just an example of how I thought the web might work sometime soon after 2001... What we're missing here, conceptually at least, is meta to describe the relationship between this post and the one referenced. Well, in this case the meta is only human-readable and quite incomplete.

Tags: ,

JSON | Toys

Xml Namespaces I've Loved and the Nature of RSS

by Kofi Sarfo 20. September 2009 17:02

Yesterday, during yet another interview, we didn't deliver quite  the finest explanation of what an XML Namespace is. Today, we're using them to parse a Stack Overflow feed so clearly we understand them, however, if part of the question's purpose is to assess our ability to express this simple idea simply then shitehawks! #fail

Take this feed, for example, which is the RSS for a useful post on Volatile vs. Interlocked vs. lock. In order to to be able to use XPath with XDocument to retrieve the question we rely on XmlNamespaceManager.

    var xDoc = XDocument.Load("http://stackoverflow.com/feeds/question/154551");

    var xmlNamespaceManager = new XmlNamespaceManager(new NameTable());
        xmlNamespaceManager.AddNamespace("atom", "http://www.w3.org/2005/Atom");

    var postedQuestion 
        = xDoc.XPathSelectElement("/atom:feed/atom:entry/atom:summary", xmlNamespaceManager);

This is another example of Stack Overflow brilliance. The feeds are in chronological order so that we can guarantee that the first item (or entry) is the original post. Following the Volatile trail leads to a Joe Duffy post on Volatile Reads and Writes, and Timeliness. For example, he writes the C# documentation for volatile is highly misleading with reference to the following MSDN documentation:

The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.

Unfortunately, I can't find this specific RSS item within the Technology feed and the same is true for the RSS feed for this blog. Although it's possible to score a reference to the item if it appears within the list of entries as they each have a unique ID, should the category contain too many entries then the older posts don't show up. Soon I'll show why this might matter but I digress.

The quoted paragraph above came as some surprise and a few hours reading suggests that:
  1. This stuff is hard!
  2. It would take *years* at the exclusion of a whole lotta stuff to have a legitimate claim to deep understanding of the intricacies of threading in .NET
  3. A solid handle on this C# Threading series of posts might just be enough (for now).
The real trick, however, might be recall during interview. There's definitely a theme here.

Tags:

C#

The Dojo Prophecy

by Kofi Sarfo 19. September 2009 07:20

Bouncing back and forth between System.Threading and XP, we spot something from Matt Wynne reminiscent of Thursday's Dojo:

Rod has just joined a new team who are using UNIX text editors to do their development in Perl. Rod is used to using the Eclipse IDE and has never really used these text editors before so he feels awkward and clumsy when he gets the keyboard. This is compounded by the fact that his team are using a mixture of editors and operating systems, with some people staunch EMACS users, others using VIM and yet more using Textmate on the Mac.
Personas for Debugging Pair Programming Session

Tags:

Dojo

Picking Locks

by Kofi Sarfo 18. September 2009 18:35

One fact I forget with consistent regularity is that locks in C# and .NET for that matter are implemented using the System.Threading.Monitor class.

static object moveCountLock = new Object(); private int moveCount; private void IncrementMoveCount() { Monitor.Enter(moveCountLock); moveCount++; Monitor.Exit(moveCountLock); }

And if we were to place the Monitor.Exit in a Finally part of the Try/Catch then effectively it becomes the following:

private void IncrementMoveCount() { lock (moveCountLock) { moveCount++; } }

To put this all in some context, the code above is designed to address race conditions though the MSDN article on Concurrency from which I take this summary elaborates on locks. And whilst we're discussing locks it's worth considering the volatile keyword.

For non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lock-statement.

Two things worth pointing out here. It turns out the famous Double-Check Locking flaw previously requiring the volatile keyword no longer needs one. At least not if you're using C# but might as well leave it in for Mono and friends, suggests Phil Haack in: Double Check Locking and Other Premature Optimizations Can Shoot You In The Foot.

A few hours worth of reading ahead I think...

Tags:

C#

Bookmarking Coding Dojo & Code Kata Resources

by Kofi Sarfo 18. September 2009 07:50

Python Coding Dojo

by Kofi Sarfo 17. September 2009 22:34

We've been on a mission since last week to get exposure to the test-driven as opposed to the test-sometime so tonight we find ourselves at the offices of Fry-IT in Southwark, London watching at least two Python gurus doing Code Dojo. It's reassuring to see recognised developers halted by unfamiliar editors and settings on an operating system few here have used regularly, no matter how pretty Mac OS might be.

We witnessed a Randori Kata:

A challenge is set and solved by pair programming (driver and co-pilot). Each pair has a small amount of time to advance the solution using TDD. When the time is up the driver goes back to the audience, the co-pilot becomes driver and one of the audience step up to be co-pilot.

Folks took it in turn driving/co-politing to create a social network graph using Graphviz based on Twitter friend/follower data. For someone who'd seen maybe five lines of Python (and ignored four of them) prior to this evening, this was a nice introduction to the language as things progressed at a nice, gentle pace. Whilst getting up to speed one helpful guy pointed out his Interactive Python Tutorial written in Silverlight. Nice.

It didn't take too long to see out why the language has become so popular. Take Fibonacci:

def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b

Compared to the C# 3.0 implementation it's utterly readable

Func fib = null; fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;

The Python version fares even better than the C# 2.0 version

static int a = 0; static int b = 1; public static int DoFib(int num) { int temp_value; if (num < 2) { a = 0; b = 1; } else { DoFib(num - 1); temp_value = b; b = a + b; a = temp_value; } return a; }

Pretty good for a first code dojo; I took along a proper Python Developer to decipher some of the odd-looking, intellisense-free text appearing onto the projected screen. The next Python Coding Dojo is on in October and by then we may even have written something in Python. Baby Steps.

Tags: ,

Dojo | Talks

Interview Driven

by Kofi Sarfo 16. September 2009 01:03

I've recruited an inexpensive live-in Python / C++ developer of Google Tor Project infamy from one of the Baltic states to pair program with me before he heads off to do a Masters degree elsewhere in London as the gumtree plea for someone to pair with has met with silent derision, I'm sure.

So far, I've shown him ReSharper; during the demonstration I find I don't know my own shortcut keys. Tools > Options > Keyboard suggests I have none setup. Why does this Visual Studio 2008 dialog show me only four items out of thousands at a time? Why does Visual Studio 2010 do the same? First the canonical Calculator.Add() example to make sure we're on the same page. Next, the Reload Countdown problem from the TDD problems list to make sure we can isolate on the process without being distracted by keywords, syntax or an unfamiliar API.

The naming convention for unit tests discussion briefly threatens to spiral beyond the seconds allocated. Found Bryan Cook's useful post on Unit Test Naming convention & Guidelines.

Pausing often to chat about names of variables and methods means noticeably slower progress than writing the code alone without being test-driven. As we do more of these it might be interesting to do some metrics. To tackle the problem with solution familiarity - by that I mean being able to go faster the second time round having already solved the problem once before whether using a test-driven approach or not - we'll alternate between having the first attempt being test-driven and test-later.

It feels a little odd to be doing this now, about forty-two Internet years after everyone else did. Thanks, however, to Benjamin Mitchell for getting us started. We're considering attending the London Python Dojo in two days. This looks like it's going to be interesting.

Notes

Ancient CodeBetter post with .NET Test-Driven Development Resources links.

Tags:

Dojo

Kofi Sarfo modified theme by Mads Kristensen



Content by WIMIRO Technology is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License.

Creative Commons License

Powered by BlogEngine.NET 1.5.0.7

About Me

Director, Wimiro Technology
London, United Kingdom

Writes in third person and first person plural; currently commutes to Moorgate.

Kiva Loans

  • Issa Sarr

    Issa Sarr

    Personal Purchases

    Requested loan: $200

    Amount raised: $75

    Dakar, Senegal

    social needs

    Loan Now »

  • Edwin

    Edwin

    Movie Tapes & DVDs

    Requested loan: $800

    Amount raised: $125

    La Paz, Bolivia

    Buy a DVD burner tower

    Loan Now »

  • Soo

    Soo

    Laundry

    Requested loan: $5375

    Amount raised: $2175

    Queens, New York, United States

    To purchase a new dry cleaning machine

    Loan Now »

 To see more entrepreneurs »

Kiva Loans