# Sunday, December 07, 2008

The following spam message (less the links) skipped through my spam filters somehow. If you are not a native speaker of English, this message may appear to be in order if your skill level with the language is limited. Otherwise, I suspect you will find this text as amusing as I did. I have not modified a single character. Enjoy...

welcome to order,
Our company is one of the largest wholesalers in Asia ,and we sell products to all over the world,we have the authorithed licence issured by Chinese government,all products in our company ranges from varieties of electronic products like mobilephone ,television, laptop,DVD,GPS,MP3/4 to photograph video game ,scanner, motorcycle prohector and so on..
We have earned our reputation in the world through our honesty business practice in the past years,and obtained many compliments from our clients globally.As we are the direct wholesalers for many reputable brands in the world,so all the products purchased in our website are promised to be at a lower price with the high quality,also all the facuty products will be returned within 7 days,exchange within 14 days,repair within 2 years without charge.
We will be right here waiting for your visitation.

I hope that as software architects and engineers we are producing code and other textual artifacts that communicate with greater clarity and understanding of the language and idiom in which we express our ideas. Do we write documentation as badly because we can only communicate clearly in code? Or vice versa? It is something to think about.

posted on Sunday, December 07, 2008 11:19:36 AM (Mountain Standard Time, UTC-07:00)  #    Comments [0]
# Monday, November 24, 2008

I'm experimenting with using the Guid type in databases and applications but I don't like the string format of the Guid. It's not easily read or formatted on a report. I wanted to find a way to represent very large integers such as the Guid (a 128 bit integer under the covers), so I looked around and found a Base 36 type sample on Code Project article by Steve Barker that gave me a great start.

The problem with Base 36 is that several characters are similar to other characters or numbers, so took the code and modified it to use a limited set of 20 characters that are distinctive from numbers and other characters sufficiently to make it easy for humans to read them back or hand enter them in a user interface.

I downloaded the code and went to work making the modifications. Here's the core of the Base 30 struct:

//removed are chars similar to numbers or another char when printed: I, J, O, Q, V, Z
private static List<char> alphaDigits = new List<char>(new char[]{ 'A','B','C','D','E','F','G','H','K','L','M','N','P','R','S','T','U','W','X','Y' });

private static byte Base30DigitToNumber(char Base30Digit)
{
    if(char.IsDigit(Base30Digit))
    {
        //Handles 0 - 9
        return byte.Parse(Base30Digit.ToString());
    }
    else
    {
        //Converts one base-30 digit to it's base-10 value
        if (alphaDigits.IndexOf(Base30Digit) > -1)
        {
            //Handles ABCDEFGHKLMNPRSTUWXY  (these are letters that cannot be confused for numbers)
            int index = alphaDigits.IndexOf(Base30Digit) + 10;
            return (byte)(index);
        }
        else
        {
            throw new InvalidBase30DigitException(Base30Digit);
        }
    }
}

private static char NumberToBase30Digit(byte NumericValue)
{
    //Converts a number to it's base-30 value.
    //Only works for numbers <= 29.
    if(NumericValue > 29)
    {
        throw new InvalidBase30DigitValueException(NumericValue);
    }

    //Numbers:
    if(NumericValue < 10)
    {
        return NumericValue.ToString()[0];
    }
    else
    {
        //Note that A is code 65, and in this
        //scheme, A = 10, Y = 29 ABCDEFGHKLMNPRSTUWXY  use alphaDigits for List<char>
        int index = NumericValue - 10;
        return alphaDigits[index]; //(char)(NumericValue + 55);
    }
}

Now, I have added a couple of static methods to give me a shiny Base 30 guid string and convert that string back to a Guid here:

/// <summary>
/// Convert a Guid to a set of four Base30 string values connected by a dash character.
/// </summary>
/// <param name="g"></param>
/// <returns></returns>
public static string GuidToBase30Set(Guid g)
{
    byte[] b = g.ToByteArray();
    Base30 b1 = BitConverter.ToUInt32(b, 0);
    Base30 b2 = BitConverter.ToUInt32(b, 4);
    Base30 b3 = BitConverter.ToUInt32(b, 8);
    Base30 b4 = BitConverter.ToUInt32(b, 12);
    return string.Format("{0}-{1}-{2}-{3}", b1, b2, b3, b4);
}

/// <summary>
/// Convert the Base30 set string produced by GuidToBase30Set back to a Guid.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static Guid Base30SetToGuid(string s)
{
    string[] p = s.Split('-');
    if (p.Length != 4) throw new ArgumentException("Invalid Base30Set format.");
    try
    {
        Base30 b1 = p[0];
        Base30 b2 = p[1];
        Base30 b3 = p[2];
        Base30 b4 = p[3];

        uint x1 = (uint)b1.NumericValue;
        uint x2 = (uint)b2.NumericValue;
        uint x3 = (uint)b3.NumericValue;
        uint x4 = (uint)b4.NumericValue;

        byte[] a1 = BitConverter.GetBytes(x1);
        byte[] a2 = BitConverter.GetBytes(x2);
        byte[] a3 = BitConverter.GetBytes(x3);
        byte[] a4 = BitConverter.GetBytes(x4);

        byte[] gb = new byte[16];

        a1.CopyTo(gb, 0);
        a2.CopyTo(gb, 4);
        a3.CopyTo(gb, 8);
        a4.CopyTo(gb, 12);

        return new Guid(gb);
    }
    catch
    {
        throw new ArgumentOutOfRangeException("Invalid Base30Set string.");
    }
}

The code above gets you something like this:

Common Guid string: b908d243-c1ac-4ea4-a954-121e4ab5c334
Base30Set from same: 47PGC95-1S8WCHP-MPTTA1-16CUN8P

You can download the code here (Base30.zip 3.6 KB).

posted on Monday, November 24, 2008 5:27:19 PM (Mountain Standard Time, UTC-07:00)  #    Comments [0]
# Monday, November 03, 2008

One of the latest Apple ads makes fun of Microsoft for spending more on marketing than on fixing Vista. Time for all you fruit computer junkies to face some cold hard facts:

Let's take our most recent SEC filing quarter for both companies and compare spending on sales, marketing and administration versus research and development and then average that spending per employee.

Microsoft spends about $25,000 per employee on R&D and $43,000 on sales, marketing and administration.
A ratio of 1 to 1.72.

Apple spends about $16,300 per employee on R&D and $51,200 on sales, marketing and administration.
A ratio of 1 to 3.13.

So relatively speaking, Apple spends nearly twice as much on sales, marketing and administration as Microsoft does.

And that's one reason why I'm a PC. You can keep your fruit computer.

posted on Monday, November 03, 2008 8:25:45 PM (Mountain Standard Time, UTC-07:00)  #    Comments [1]
# Sunday, November 02, 2008

I just posted this on a linkedin group to which I belong, but I thought I'd also like to pose the question here.

I'd like to get a discussion started that attempts to define enterprise software architecture. My own definition seems to be evolvoing with every enterprise for whom I've worked. In the abstract, for me, enterprise software architure is the art of putting the pieces of multiple puzzles together into one great work of art.

There are many puzzles to choose from and every enterprise has a unique mix. There are multiple teams with various skillsets and experience. There are multiple business processes sometimes with unique and strange business rules. Technology platforms that differ, communications protocols that won't communicate with one another, languages, frameworks, compilers, IDEs, components, and hardware that vary from team to team and department to department. Ours is the task of taking these disparate and often incongruous pieces and molding them into one coherent masterpiece of technology and human resources to get more done, get it done better, quicker, cheaper and easier. And if we do our jobs well, it may be that no one will notice that we did it at all.

What do you think?

posted on Sunday, November 02, 2008 12:54:07 PM (Mountain Standard Time, UTC-07:00)  #    Comments [0]
# Friday, October 24, 2008

I just posted the following note on a LinkedIn group I follow in answer to a post about so called "software factories," which is a nice euphemism for overseas developers working for much less than they deserve struggling to meet the unreasonable demands of their bosses. This represents my opinion on the subject:

Never forget that you get what you pay for. Hiring an overseas or even local "software factory" or consultancy to build your software can be problematic at best and a complete waste of time and money at worst.

First, if you cannot communicate, forget about it. Building software is 99% communication and 1% technology. Okay, perhaps I overstate the case. A little. But you cannot overestimate the importance of clear, effective communication.

Second, unless you have the internal people required to manage such a relationship, your project will fail. This means you need project management and technical people in your own organization that you know well and trust. They need to be supremely competent. This is especially true if you plan to hire a firm outside of your own geographical area.

Third, plan for time and budget overages. It is the nature of consulting to promise a low price and quick turnaround and then when you are committed to the project and it is "nearly done," you will be informed that there is much more to do, generally due to legitimate changes in requirements because you did not fully understand what you wanted when the project first began. This is the boon and bane of software development whether internal or external.

Finally, you can have success outsourcing your software development project, but do not make the mistake of thinking that it will save you an enormous amount of time and money, especially for a single application project. It takes time to develop a working relationship with an outside consultantcy, especially one that is half way around the world. If you have multiple projects, long term goals, and a huge budget of time and money, it may in fact be cost effective to have a relationship with a so called "software factory." But if you are a small organization and have one or two projects, you will nearly always be better off hiring a professional locally, usually through one of the many technical recruiting companies, to come into your organization as a contractor to work on-site building exactly what you want as you discover over time what it is you want exactly.

posted on Friday, October 24, 2008 9:56:19 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [1]
# Wednesday, September 17, 2008

In my first attempt at creating a real applicaiton using the new ASP.NET MVC project template (Codeplex Preview 5), I found that when I clicked a new tab I'd created linked to a specific controller and an action I'd decorated with the [Authorize(Roles = "user")] attribute, the "out of the box" Login action did not redirect me to the that controller/action combo once I had successfully logged in.

Here's my solution to the problem. First, I added a hidden value in the Login.aspx form. Second, I added a parameter to the Login action in the Acount controller (AccountController.cs). Let me know if you've found a better way.

Here's the code for the form:

<form method="post" action="<%= Html.AttributeEncode(Url.Action("Login")) %>">
    <div>
        <table>
            <tr>
                <td>
                    Username:
                </td>
                <td>
                    <%= Html.TextBox("username") %>
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                </td>
                <td>
                    <%= Html.Password("password") %>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <input type="checkbox" name="rememberMe" value="true" />
                    Remember me?
                </td>
            </tr>
            <tr> <!-- Added to handle the returnUrl -->
                <td>
                    <%= Html.Hidden("returnUrl", ViewData["ReturnUrl"].ToString()) %>
                </td>
                <td>
                    <input type="submit" value="Login" />
                </td>
            </tr>
        </table>
    </div>
</form>

Here's the code for the Login action:

public ActionResult Login(string username, string password, bool? rememberMe, string returnUrl)
{
    ViewData["Title"] = "Login";
    string url = (string.IsNullOrEmpty(returnUrl) 
        ? Request.QueryString["ReturnUrl"] ?? string.Empty 
        : returnUrl)
        .Trim('/');
    ViewData["ReturnUrl"] = url;

    // Non-POST requests should just display the Login form 
    if (Request.HttpMethod != "POST")
    {
        return View();
    }

    // Basic parameter validation
    List<string> errors = new List<string>();

    if (String.IsNullOrEmpty(username))
    {
        errors.Add("You must specify a username.");
    }

    if (errors.Count == 0)
    {

        // Attempt to login
        bool loginSuccessful = Provider.ValidateUser(username, password);

        if (loginSuccessful)
        {
            FormsAuth.SetAuthCookie(username, rememberMe ?? false);
            if (url != null && url.Length > 0)
                return RedirectToAction("Index", url);
            else
                return RedirectToAction("Index", "Home");
        }
        else
        {
            errors.Add("The username or password provided is incorrect.");
        }
    }

    // If we got this far, something failed, redisplay form
    ViewData["errors"] = errors;
    ViewData["username"] = username;
    return View();
}
posted on Wednesday, September 17, 2008 8:20:31 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Thursday, September 11, 2008

I upgraded to the latest version of dasBlog a few days ago and inadvertently allowed comments without requiring approval. A spambot comment got through and while I quickly turned on the "require approval" feature, it was too late. Since then I've been bombarded with stupid link spam comments. I even deleted the one post that seemed to be the bot target and created a new post with the same content.

No luck. After many similar spam comments today being posted to the most recent post on my blog, I'm giving up. I'm taking a comment holiday. It won't bother anyone really because I don't get many real comments. I'll enable the comment functionality some day in the future.

Meantime, if you have a comment, feel free to email me and I'll post it as an addendum to the relevant post.

posted on Thursday, September 11, 2008 10:29:40 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Tuesday, September 09, 2008

I've just installed the plugin blogged about by Mike Ormond and here's an example of it's output taken from code in the Atrax project I've just published to Codeplex.

namespace Atrax.Library
{
   [DataContract, Serializable]
   public class QueryResult
   {
      /// <summary>
      /// The original query sent by the client.
      /// </summary>
      [DataMember]
      public Query Query { get; set; }

      /// <summary>
      /// Status code sent back to query client's callback url.
      /// </summary>
      [DataMember]
      public string StatusCode { get; set; }
      
      /// <summary>
      /// Status description sent back to query client's callback url.
      /// </summary>
      [DataMember]
      public string StatusDescription { get; set; }
      
      /// <summary>
      /// The XML schema for the result XML.
      /// </summary>
      [DataMember]
      public string ResultSchema { get; set; }

      /// <summary>
      /// The result produced by the query processor, usually XML.
      /// </summary>
      [DataMember]
      public string Result { get; set; }
   }
}
posted on Tuesday, September 09, 2008 12:22:07 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Monday, September 08, 2008

I've run into a little snag with my blog. I installed (rather copied) the latest release of dasblog to my bin (and other directories) and ended up not being able to edit or post new blog entries because of some installation (probably some config) issue with FreeTextBox. So while I figure out the problem or wait for someone else to solve it, I decided to try Windows Live Writer.

Here's the FreeTextBox and the text it displays now when attempting to edit or add a post:

I've tried the web.config change, including the one suggested by Scott Hanselman regarding the dependent assembly as follows:

<dependentAssembly>
    <assemblyIdentity name="FreeTextBox" publicKeyToken="5962a4e684a48b87" culture="neutral"/>
    <bindingRedirect oldVersion="3.0.5000.0-3.0.5000.6" newVersion="3.1.6.34851"/>
</dependentAssembly>

Well, time to publish this to see what it looks like.

(And second post with some edits.)

posted on Monday, September 08, 2008 4:05:55 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Sunday, September 07, 2008

Two and a half years ago I wrote an implementation in C# of an algorithm published in 2003 in a short academic paper by Yutaka Matsuo and Mitsuru Ishizuka in the International Journal of Artificial Intelligence Tools. Of course, the algorithm is not a perfect implementation of the algorithm published in the "Keyword Extraction from a Single Document using Word Co-occurrence Statistical Information" paper. I made a number of decisions to make the algorithm as effective as possible while keeping it as fast as I could.

The code was written for Provo Labs, my employer at the time. I've recently obtained written permission from Provo Labs to release this code as open source under the Apache 2.0 license. You can get the code in the Atrax.Html project, a part of the entire Atrax project which I've just released, at http://www.codeplex.com/atrax. Here's the core of the code.

string[] terms = new string[termsG.Count];
termsG.Values.CopyTo(terms, 0); //gives terms array where last term is the MAX g in G
foreach (string w in terms)
{
    decimal sumZ = 0;
    for (int i = 0; i < terms.Length - 1; i++) //do calcs for all but MAX
    {
        string g = terms[i];
        if (w != g) //skip where on the diagonal
        {
            int nw = termNw[w];
            decimal Pg = termPg[g];
            decimal D = nw * Pg;
            if (D != 0.0m)
            {
                decimal Fwg = termFwg[w][terms[i]];
                decimal T = Fwg - D;
                decimal Z = (T * T) / D;
                sumZ += Z;
            }
        }
    }
    termsX2[w] = sumZ;
}

SortedDictionary<decimal, string> sortedX2 = new SortedDictionary<decimal, string>();
foreach (KeyValuePair<string, decimal> pair in termsX2)
{
    decimal x2 = pair.Value;
    while (sortedX2.ContainsKey(x2))
    {
        x2 = x2 - 0.00001m;
    }
    sortedX2.Add(x2, pair.Key);
}

//now get simple array of values as lowest to highest X2 terms
string[] x2Terms = new string[sortedX2.Count];
sortedX2.Values.CopyTo(x2Terms, 0);

I have not spent much time on this algorithm in the past two years and would like to find others with similar interests to help me improve and perfect it. If you have an interest in this kind of research, please join me at the Atrax project page on Codeplex.

posted on Sunday, September 07, 2008 2:12:27 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]