Yes I am Still Here

I am working from home, thru my Elusive Development company, on a side project to develop a windows forms app with several components.

It is a very interesting project so far, but I am only a couple of weeks in to it. In the end it will be a couple of windows forms apps. One for the client and one for the end user, as well as a windows service that will handle timing and the win32 api calls. The service will use the api to create a new desktop and then create a process w/in that desktop and switch to it, effectively taking over the users computer with the new desktop. The user is ultimately forced to deal with the, now in the foreground, application in order to get back to what they were originally doing. Pretty sweet. I am building it using C# for the 2.0 framework. I opted for 2.0 versus 3.5 in order to prevent requiring the user to have 3.5 installed. As folks get the 2.0 framework w/sp3 for xp, I am pretty much guaranteed my target platform for that os on up to windows 7.

For those interested I am using a plugin archictecture that utilizes the Abstract Factory and Template Method design patterns so far. Dll assemblies with new types and usercontrols for those types are loaded at runtime and instantiated using factories. Neato…


Well I finally joined Github, which is the coolest of course.  I started with just a little project for an httphandler to apply filters to images.  Something I need for work.  I have the project’s homepage here at my blog for lack of a better location.  And I also added a page in the wiki at github for good measure.  I am extra jazzed about the whole git thing.  I have been using it for local VC at work and at home.  I am still a little skittish about using git-svn, as it hosed on me the first time i tried to dcommit something, but all in all I am very pleased with Git itself.

You should checkout Github, it is perfect, and you can pay a few bucks a month if you need private repos…

jQuery Plugins

I thought that due to the fact that I have been working on pretty much nothing but jQuery at work for the last month or so, that I could come up with a post about some of what I have been doing.  First I should mention that I have really enjoyed it, challenging and at times frustrating though it was, it was equally if not more gratifying.  I had yet to have worked with any one particular library much, if any one prior to now it would have to have been just a little bit of jQuery here and there, playing really.  And certainly not trying to write my own plugin, of course as I said, until now.

The Task at Hand

My task began as a matching of functionality at minimum and ideally a surpassing of functionality in the area of product thumbnail/image viewing, as compared to one of our direct competitors on the web (our primary product is a print catalog).  One of our competitor’s sites had a zooming feature and the ability to drag around the zoomed image to further examine it.  So off I went.  In the end I have matched the functionality but, I feel at least, surpassed them more in terms of usability and appearance.  Basically ours offers the same features as theirs, for now, but looks a lot slicker I think.  Rather I hope.  Anyways, I decided early on, if not immediately to implement the entire thing in jQuery as a plugin.  I wanted to challenge myself and I did.  And while it took me a bit longer I am much more satisfied and my skills with jQuery are much more improved.  

I should mention that just prior to this project I had been working on an ASP.NET site and model/api for describing reports with an xml schema and matching .NET model and delivering them via a web page.  I used an http handler to turn the xml file that described the report and its query and columns into rendered html with data embedded.  During that process I wrote a pagination plugin in jQuery that was actually my first official production quality plugin in jQuery.  Of course all it did was take in the details of the report and generate the links for next/prev and each page in a desired visible range.  So the image gallery was my  sophomore effort at writing a plugin.

The Process (sort of)

Well, let us just say that I have over twenty-five pages of notes that I wrote while I worked on this plugin.  I worked on it at home probably at least a fourth of the time if not a third, and I learned a whole lot about jQuery and about writing a larger scale plugin.  I basically went out on the web and looked for the different pieces of functionality that I needed, like zooming an image in and out by resizing it, and then dragging, and then the carousel for the thumbnails came at the very end.  There was quite a bit of math to figure out, but I was able to piece it together from scripts I found and really by just writing it out in my notes when I got stuck. 

Some things I was able to pull from existing plugins, like the zooming works with a couple of images (one for in and one for out) but it also works with the mousewheel movements (but only when the mouse is over the image view pane.  I was able to utilize the jquery.mousewheel.js plugin in order to get a delta value to use to determine whether to zoom in or out.  I already mentioned that I used a very minimalistic version of jcarousel lite to make the thumbnail slider (that requirement came later of course).  

One of the coolest things, I think, is that instead of placing a toolbar or a list of links below or above or even adjacent to the image being worked on, I went an extra mile (or two) to make a slide out/down toolbar that appears at the top of the view pane when you mouseover the image.  It wasn’t easy, well of course jquery made the slide in/out very easy, but I had a really tough time getting 100% opacity button images and link text over top of a 70% opacity background for the see through toolbar effect.  But I eventually got it to work and I think the end result is a really nice effect.

Project Euler Journey Begins

What, Project Euler?

Project Euler Website
Project Euler Website

This post is an introduction to what I intend to be a series of posts that detail my journey through the problems of Project Euler.  I am hoping to post as I solve, or attempt to solve each of these problems.  My goal is, of course, to solve all of them, but I expect them to become very challenging very quickly so I may find myself stumped early on.  However, this is an exercise in learning so I will have to prepare to be challenged, that is really the whole point.

The intent is for me to use the problems on Project Euler(see previous link) to accomplish several goals I have, or rather have had for a while and am just now attempting.  I can list them as three distinct learning goals.  I say learning because they all have to do with me wanting to either learn something knew or to increase my knowledge/skills in an area. 

My Goals for the Journey

The first goal is to learn how to use a unix shell.  I want to learn how to program for the bash shell as well as how to get around and get things done using the shell command prompt.  I would eventually like to be able to incorporate shell usage into my daily work as much as possible as I really think it is an efficient way of getting tasks done. I have always felt that anything that keeps my hand off of the mouse is a step toward increasing productivity.  Being able to key tasks at a command prompt is always faster than moving your hand to the mouse to search through dropdown menus and lists of icons, atleast it is in most cases.  I do not think that the mouse is unnecessary by any means, I just prefer to use the keyboard as much as possible, or as much as is practical, while I am working.

The second thing I have always wanted to learn more about is higher math.  I never took calculus in my brief stint at university, nor when I was in high school.  I always loved algebra and I am hoping that calculus as well as other higher mathematics areas will be just as interesting.  I also hope they are not beyond me though.  I expect I will have to learn quite a bit in order to solve the problems on Euler, even after the first couple I am already looking up stuff on Wikipedia and learning new things, exactly my purpose.

Lastly, but certainly not least, I want to learn a new language, maybe more than one.  I am going to start with Perl because I have always felt that not knowing perl was a drawback as a professional developer.  I will be attempting to solve all of the problems using Perl as the coding language and consequently have to learn it as I go.  I have already used perl to solve the first two problems.  I also really want to learn Ruby so I may do some problems in Ruby as I go along.  There are plenty of problems so I may be able to learn both languages as I go.

More about Project Euler

Euler is considered to be the preeminent mathematician of the 18th century and one of the greatest of all time. He is also one of the most prolific; his collected works fill 60–80 quarto volumes.
Euler is considered to be the preeminent mathematician of the 18th century.
I stumbled on to project euler somehow, though I cannot remember how for the life of me.  At the time I bookmarked it in my GMarks and went on my way keeping it in the back of my mind as something to look at later.  Well later came and I went to the site and read about it.  

Project Euler is a series of challenging mathematical/computer programming problems.  They involve both the math aspect as well as the coding aspect which is used to perform the calculations needed to get the solution in a timely fashion.  In fact all solutions are supposed to adhere to the one minute rule meaning that they should take no more than a minute to execute and produce the result.  The site’s about page states that its intent is

“to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context”

which is what it does, quite well in my opinion.  There are several cool features about the site that make it fun as well as challenging.  It is also well kept and updated often, which makes it feel like you are participating in something more so than if it was a dead site.  In fact, every time I logon to the site there are dozens of people logged in already, as I write this there are 88 members logged in, so it is definitely an active site.

Some of the Features

There are a few features on the site that appealed to me and that made me want to participate as well as making me think that this was an excellent way for me to accomplish the goals I mentioned above.  Of course the activity level of the site and the fact that it is kept current and that they are still adding new problems, etc. all made it attractive to me.  But, there are a couple of specific features that make it even cooler.

One of those features is the simple fact that you can register and login.  This makes possible the scoring and tracking stats features per user (and per problem).  Once you register and login you start on the problems, I am doing them in the suggested order that they are presented in, but I suppose you don’t have to do it that way.  When you solve a problem or rather, when you think you have solved a problem you can submit your answer.  There is a place to do that for each problem, then you are told whether you are right or not.  I have been right the first time on the first two problems so I am not sure what happens when you are wrong.  When you are right you will see a different view in the problem list from then on.  You now see links to an overview pdf, which explains the solutions and has notes about the math/theory behind the problem.  This lets you see if you went about solving the problem in the most efficient way possible.  You also see a link to the forum thread for that problem where others have discussed the problem in greater detail.  

Screenshot of the list of problems.
Screenshot of the list of problems.

All this extra info is quite informative and contributes greatly to the learning experience.   If not for this extra info and participation by the community I wouldn’t know that although I correctly solved the first two problems, there were better ways to solve them than the ways that I used.  While not wrong, because I got the right answer, I discovered that there were simpler ways, that included more math theory that I did not have, that would find the solution more efficiently.  These extra notes and posts helped me learn much more than I would have by just submitting my answer and finding out it was correct.  I plan to post separately about each problem where I will go into this point in more detail.

Some tidbits about Project Euler

  • Here are some cool facts/details about project euler that I thought might add to this post and were interesting:
  • There are 228 problems so far, all of which have been solved by someone.
  • The first problem has been solved by 48,882 people (including me) and the 228th problem has been solved by 76 people.
  • You can sort the problems in descending ID, ascending Difficult, or ascending Difficulty.  I am solving them in order by ID.
  • From the Stats page, there are 52,922 users whom have submitted a total of 892,601 solutions, an average of 16.9 problems per user.
  • I count 165 countries listed on the stats page w/atleast one solution submitted next to their flag.

The top five(5) languages in number of users are: 

    1.  C/C++ [5798]
    2. Python [5121]
    3. Java [3231]
    4. C# [1758]
    5. Haskell [1255]
    6. Ruby [1185] — i added a sixth because the last two were so close 

      The top five(5) languages in User’s Rating are:

        1. 40% – Magma — (?? never heard of this one, have to look it up)
        2. 39% – Pari/GP — (?? never heard of this one either)
        3. 23% – APL/J/K — (?? okay I must be an idiot)
        4. 22% – Mathematica — (finally one i have heard of)
        5. 20% – GAP — (?? sigh)

          Okay, so there you go, a bunch of stuff about Project Euler and why I am gonna be working on it.  I hope I don’t get discourage right away.  I have little doubt that this is going to get real tough pretty soon, but I am equally sure that I will learn a great deal if I can do it… j

          C# Extension Method and Lambda Expressions

          Recently I have been trying to learn more about the new language features in C# 3.0, and I have enjoyed what I have found thus far.  Especially both the var keyword and extension methods, but I had yet to really implement anything using lambdas.  That is until today…

          One of the things I have done with extension methods was to implement some really sweet method argument validation stuff.  I had read several related blog entries regarding this subject and had whipped up a variation [actually just a much smaller set of what they had already come up with as my needs were much less] of their ideas for use in a project I am working on [and likely in future projects].   And, as it turns out, this same bit of features afforded me the opportunity to check out lambda expressions as well, and to hopefully this time add something of value to build on their work instead of just reusing it.

          Okay so the scene is set, with me at my desk at work, coding a unit test for what I hope will be a new feature on my current project [dynamically executed reports from xml definition files, like rdl but much much simpler]…

          I was about to use the argument extension methods to validate an integer that I needed to be in a certain range and it occured to me how nice it would be if I could just pass in an expression that evaluated to a boolean result similar to what I would do if I was writing in javascript.  Yeah, that would be extra nice!  So, off I went back to GOG to do some research on passing an expression as an argument to a function in C#.  My research led me straight to lambda expressions and exactly what I needed to make my new extension method work.

          My goal was to be able to implement something like the following psuedo-code:

          [sourcecode language=’csharp’]
          public void MyFunction(int myInt)
              RequireThat(myInt).MeetsCriteria(“myInt > 0”);

          Thus being able to use some very smooth and descriptive code to validate my integer argument before using it in the function, or atleast something as close to that effect as I could get.

          Well lambda expressions were exactly the ticket, specifically the Func<T, TResult> delegate type.  Which basically allows me to pass a method that accepts one parameter of type T and returns a result of type TResult as a parameter to another method, without defining a custom delegate type of my own.  A kinda anonymous delegate type construct if you will.  Lambda expressions use this type as an arg for the Expression<T>(Func<T, TResult) type constructor.   I have done some of this preliminary reading on Expression Trees and such and it is heady stuff, but interesting none the less.  I look forward to someday being able to apply it to a real world problem.  

          But today I was able to apply lambdas to my real world problem like so:

          I needed an extension method for my Generic argument wrapper that would allow me to pass in a simple expression predicate with which to validate the argument.  And here is what I came up with:

          Code for MeetsCriteria extension method.
          Code for MeetsCriteria extension method.

          Now this code allows me to pass in a lambda expression that I wish to use to validate an integer argument for my method [or any integer for that matter].  This method was added to my existing argument validation extension methods setup as described in a previous post, already linked to a couple of times above, so it follows similar usage syntax, as such:


          I also added a unit test for this new method into my existing test project for the rest of my validation extension methods, and so I will use that test method to show you a contextual usage of MeetsCriteria…

          My unit test for MeetsCriteria method.  Testing that 21 is between 20 and 22.
          My unit test for MeetsCriteria method. Testing that 21 is between 20 and 22.

          So, now I have actually used lambdas in a project at work, I am so very happy with myself.  And my quest to conquer C# 3.0 features continues… J

          Argument Validation using C# 3.0 extension methods

           After some research into reusable arg validation ideas on GOG (good ole google) I have found something that, after some simplification of course, will serve the projects meager argument validation needs. Actually this is a super cool trick that we can reuse any number of places from here on, if we so desire.  Here are the original posts that I read and have taken ideas from, particularly Roger Alsings articles, much thanks to him.  This technique is, IMHO, a nice way to showcase how to combine some of C#’s new features, specifically Generics from C# 2.0 and Extension methods from C# 3.0, into a nice solution to a frequent problem.  

          Using these posts and the code from a much larger api at the puzzle framework address in the quote above, i have assembled a smaller set of argument validation methods.  Roger Alsing’s puzzle framework has a full compliment of these validation methods if you are interested.  I, on the other hand swiped just a couple of his and added a couple of my own, but am using the same technique to acheive a fluent interface and also the very ingenius idea he had of using a wrapper class for the extension methods rather than extending each .NET type on its own.  Very nice work by him.  This article is simply an effort to explain how I used these articles to put together a much smaller set of validation methods for my own use in a project.  Hopefully it explains things clearly and pays sufficient homage to the ideas originator.


          The basic premise is that now with generics and extension methods features of C# we are able to add functionality to types/classes. In this case all types, for the purpose of validating method arguments. The articles above explore this in a progressive fashion: First presenting the idea of an ArgumentHelper class that would have lots of overloads for validating various C# types, i.e. int, string, double, etc. Under this scheme you would need a separate method for each type of validation for each type. Such as:

          [sourcecode language=’csharp’]
          public void RequireNotNull(int arg, string argName);
          public void RequireNotNull(string arg, string argName);
          public void RequireNotNull(DateTime arg, string argName);


          This is not a bad idea, and certainly is better than writing a multiline if statement for each argument in each method in your project. Second Idea: was to use extension methods to facilitate usage syntax like:

          [sourcecode language=’csharp’]
          public void MyMethod(int argument1, string argument2)
              // validate args
              argument1.RequireInRange(argument1, 0, 10, “argument1”);
              argument2.RequireNotNull(argument2, “argument2”);

          Now this is starting to look pretty smooth, however we go one step further, combining the new features of Generics(C# 2.0) and Extension methods (C# 3.0) to get something that is SUPER smooth. The idea, which was of course new to me, is to a) create a generic type for argmuments. I used one similar to theirs, basically just a container for name and value of an argument the value being the arg itself of the type “T” as defined by the generic. Then b) use extension methods to add validation methods to this generic class, thus making them available to any type. Here is my generic argument container class that is the one extended:

          [sourcecode language=’csharp’]
          public class ArgumentEx
          public T Value { get; set; }
          public string Name { get; set; }
          public ArgumentEx(T value, string name)
              this.Value = value;
              this.Name = name;
          public static implicit operator T(ArgumentEx arg)
          return arg.Value;

          This class simply wraps an argument with a generic container in essence. We set the default operator to return the arg itself (the “Value” member) and create a simple ctor. Now instead of having to write an extension method for each type we want to be able to validate (int, string, etc.) we just write one extension method for this class. First we write an extension method for the generic T type of our generic class and that will give us coverage of all types and this will always return us an instance of our new ArgumentEx type:

          [sourcecode language=’csharp’]
          public static class ValidatorExtensions
              public static ArgumentEx RequireThat(
          this T arg, string name)

                  return new ArgumentEx(arg, name);

          Now we can call this RequireThat method from any argument we pass in to any method and we will get back our ArgumentEx class which we have extended with validation methods such as this:

          [sourcecode language='csharp']
          public static ArgumentEx NotNull(
          this ArgumentEx arg) where T : class
              if (arg.Value == null)
                  throw new ArgumentNullException(arg.Name);
              return arg;     // for fluency

          This method extends the ArgumentEx type rather than the generic T type so we have all of our extension methods hanging off of the wrapper class. This setup is a touch abstract but it allows us to do super pretty things like this:

          [sourcecode language='csharp']
          public ReportInfo(string pathToXmlFile)
          : base(null, null)         
              // validate args

          not bad in the way of readability and extensibility too. Because the extension methods return the arg instance every time you can chain calls as well.


          Below is the class diagram for the validation/argument extensions.  There are a few string extensions too added for convenience...


          Class diagram for the validation extension classes.
          Class diagram for the validation extension classes.




          And here is a screenshot of the unit tests all green and pretty!


          A screenshot of the pretty green unit test results!
          A screenshot of the pretty green unit test results!




          So now I have an easy to use and further extensible system for validating method arguments with out having to write if/throw constructs over and over inside of each method.  This promotes better code because the easier it is to validate my arguments the more likely it is that I will do a thorough job of it.

          Google Base, Reporting Xml, and VS Unit Testing

          I have been working on an interesting project here at work.  I had previously been working on some fixes for a windows service (written in C#) that uploaded our catalog items to Google Base, based on some logging and comparison diffs, every fifteen minutes if there were new items or updates to items.  But, with that seemingly on its way now, I was assigned this new project.  Basically I am working on a system that will allow the definition of sql queries, or data “views” via an XML schema.  Yes, I know there is already RDL for SQL Reporting Services, but that is not what they want.  They want a much, much simpler setup that will make it easier for a developer on the team to make changes to, or add a new report, by editing the XML  definition file.

          So, off I go.  I started with the whiteboard notes that were put up during my initial meeting about this, and the sample XML there-in, and have tried to keep it as simple as possible.  I am using classes for each logical report entity, such as:  Report, Query, Column, Parameter, etc.  I am also using the VS unit testing framework for this project, which is something I hadn’t done previously, but wanted to.  Let me qualify that by saying up front that I am not using TDD strictly speaking.  But I am writing tests for code, classes and methods, as I go.  I must say I am quickly becoming hooked on the idea of unit testing everything, whether you are designing with TDD or not, it just makes sense, and it does honestly help with design decisions.  By testing your code as you go or before hand you get a quick look at how it will be called, which in my case where I am developing what hopes to be an api of sorts that will be extended and re-used often is a great benefit.   Continue reading