TypemockIsolator.LegacyRefactoringExample

=Background= This work derives from an earlier presentation in Java from Berlin: @http://vimeo.com/31927512

The general outline follows the one here: []

=Overview= Imagine you have a bit of legacy code you need to use to get your work done. For example, say you've been slated with finding currency conversion "deals". That is, given a number of currencies, there is a path that makes money due to asymmetries in conversions. Here's some C# code to get currency conversions: code format="c using System; using System.Collections.Generic; using System.Net; using System.Text.RegularExpressions;

namespace LegacyRefactoringHtmlScreenScraping {   public class CurrencyConversion {       private LinkedList _allSymbolsCache; private DateTime _lastRead = DateTime.Now;

public LinkedList AllSymbols {           if (_allSymbolsCache != null                 && DateTime.Now.Subtract(_lastRead).TotalMinutes < 5) {               return _allSymbolsCache; }

var client = new WebClient; string url = "http://www.jhall.demon.co.uk/currency/by_currency.html"; var result = client.DownloadString(url);

_lastRead = DateTime.Now; var foundTable = false; _allSymbolsCache = new LinkedList ; foreach (var s in result.Split('\r', '\n')) {               if (foundTable) if(Regex.IsMatch(s, "\\s+ [A-Z]{3} ")) _allSymbolsCache.AddLast(                           new Regex(" ").Replace( new Regex(".*top>").Replace(s, ""), "")); if (s.StartsWith(" Currency Data")) foundTable = true; else continue; }           return _allSymbolsCache; }

public Decimal ConvertFromTo(string fromCurrency, string toCurrency) {           if (!AllSymbols.Contains(fromCurrency)) throw new CurrencyDoesNotExistException(fromCurrency); if (!AllSymbols.Contains(toCurrency)) throw new CurrencyDoesNotExistException(toCurrency);

string url = String .Format("http://www.gocurrency.com/v2/dorate.php?" +               "inV=1&from={0}&to={1}&Calculate=Convert",                        toCurrency, fromCurrency); var client = new WebClient; var result = client.DownloadString(url);

var index = result.IndexOf(""); var theGoodStuff = result.Substring(index); var startIndex = theGoodStuff.IndexOf("") + 3; var endIndex = theGoodStuff.IndexOf(""); var importantStuff = theGoodStuff.Substring(startIndex, endIndex); var parts = importantStuff.Split('='); string almostValue = parts[1].Trim.Split(' ')[0]; return System.Convert.ToDecimal(almostValue); }   } } code