Culture shock of scaling back from full-time work…

As a blessing, my husband recently got promoted to full-time status at his job.  This meant that I was able to leave my full-time job that was interesting, but demanding and stressful.  After a few weeks of re-calibrating and getting a lot of home projects finished, I found that I really missed writing and coding.  I wrote a few simple things to get warmed up, but I’m really glad that I did because it’s breathtaking how quickly those skills will slip if you don’t use them often.

I, like many young (okay, youngish) moms with kids in school, would like to find something useful and if possible profitable to do in the 6 or so hours a day while the kids are away.  I’m finding that getting part-time or freelance work isn’t as easy as I thought.  The consensus that I’ve read has been that for the most part, you’re going to have to tap all your network of peers/friends/family to get leads then start out working for little to no pay in order to build a reputation.  I pretty much suspected this, but what I didn’t fully realize is something my sister brought up:

“Contracting/consulting can be awful in usually two ways: 1) What is available is the bottom of the barrel, makes no sense, and nobody else will work on it.  2) The project is interesting, but it has become a steaming pile of spaghetti code and they want you to wave your magic mouse and have it all fixed two weeks ago for next to nothing.”

So… here’s my dilemma.  I want to be able to work part-time or telecommute so that I can keep my 10+ years of experience and skills sharp, but I would like to eventually get paid.

What I can’t understand is why more companies don’t allow their IT employees to have more flexibility provided that they well document the work they do and get it in on time.  Especially because office space is a premium and gas prices are through the roof right now.  In Pittsburgh, we have spectacularly bad traffic and highway infrastructure.  If business owners know how expensive it is for employees to commute/park/bus downtown, why don’t they give them the option to take a small pay cut and telecommute?  Both sides win that way.

Anyway, in the meantime while I’m fishing for contract leads, I’d like to start developing some small bite-sized projects.  jQuery and Javascript are good and familiar to me so far, but do the readers have any suggestions?

F# and using strings for method and class/type names

So after about 2 months of jumping back and forth between languages (Including and not only: Ruby, Python, IronPython, Boo, Scala, and a lot more I don’t remember) I’ve finally settled on F# for more reasons than I will put in this post. BUT one thing that kind of blew me away, and I realize that doesn’t take much (I thought the ending to Terminator 3 was great), was the ability to name methods with string representations… eh wha?

Well I was writing unit tests and stumbled onto the FSUnit “library” (and by “library” I mean a single file). Well on the page there was an example that looked a sumthin’ like this:

[<TestFixture>]
type ``Given a LightBulb that has had its state set to true`` ()=  
  ...

Now if you haven’t used F# just replace “type” with “class”. Essential there is a string/text built name. Not something you see, or at least I haven’t, in any old language. So I went forth and was fruitful and multipled… with unit tests… gross.

[<TestFixture>]
type public ``When Adding``() =
    let returnThing = new MethodResult() :> IResult
    let randomTool = new RandomTool()

    [<Test>]
    member public x.``A Warning Message The Success Flag Is Set To True``() =
        returnThing.AddMessage(new MessageItem(randomTool.RandomString(10), MessageCategory.Warning)).Success |> should be True

And using Resharper’s unit test box thingy it gave a read out of:

  When Adding A Warning Message  
     The Success Flag Is Set To True             Success

Now I’m not sure how useful this is for non unit testing but I think it’s obvious it is to reading Unit Test results. Pretty nice.

XUnit and Connection Strings

A real quick one but maybe a good one.

I was running the xUnit.gui.exe program to run units tests using xUnit… duh. Problem I ran into is that xUnit is not a part of the solution that the tests were. This caused issues when trying to run integration tests since the needed app.config wouldn’t be read by xUnit as the config file used when running an app is the config file from the topmost (Usually UI) project/application. So I made a wild guess that if I follow that logic, what little there may be, and add the connection strings to the xunit.gui.exe.config file as if it were an app.config/web.config, it should use the connection strings added… and turns out it did.

And there you have it… it being the solution and not a clown.

Fun with F# and Method Reduction

So the first and second methods came from this book , but I thought for fun I might reduce it further to see
how small a call could get.

Basically take a number and divide it by another number 3 times.

let bytesToGb item =
let itemB = item / 1024I
let itemC = itemB / 1024I
let itemD = itemC / 1024I

Is the same as:
let bytesToGb item =
let x = x / 1024I
let x = x / 1024I
let x = x / 1024I

And more reduction:
let bytesToGb item =
item
|> (fun x -> x/1024L)
|> (fun x -> x/1024L)
|> (fun x -> x/1024L)

Or more:
let divide x = x/1024L
let bytesToGb = divide >> divide >> divide

If you’re familiar with Func/Action, Linq, ect this might actually look a lot like some of the newer “functional like” additions to C#. Well at least the third one. Funny part, and I didn’t know this, is that most of the more interesting additions to C# (Generics, Linq, Lambda expressions) actually came from F#; Which was built to match other existing functional languages like Haskel)  Go figure.

XUnit.gui.exe – this assembly is built by a runtime newer than the currently…

So ran into this while using XUnit (For my new F# adventure) and I ran into this error when I tried to use the XUnit.gui.exe thing:

“This assembly is built by a runtime newer that the currently loaded runtime and cannot be loaded.”

Pretty straightforward in that I am creating projects in 4.0 and one can infer XUnit wasn’t ready for this. At first I was thinking, painful as that is, that XUnit can’t handle 4.0. To my relief, I found this post at:

www.markhneedham.com

Great… except it only talks about the xunit.console.exe.config and there wasn’t a xunit.gui.exe.config. Well in a moment of questionable brilliance, more like a slight moment of clarity, I decided to copy the xunit.console.exe.config and rename the copied file to “xunit.gui.exe.config” and it worked. Go figure.

This is a picture for the slighty less lazy:

And for those more lazy than that:

Here’s the xml in case you’re more lazy than I am.

Into the unknown… sort of.

So after about eh 3? months of not writing anything (Mostly due to the joy slash nightmare of a newborn) I’ve had a real “come to [Religious Figure]” moment… And that moment has taught me that java based off shoot languages are a pain in the ass to even get started with. This is the typical how to guide:

So yeah, I’m back to the Net but with a twist. Not much an M. Night ‘They call me Mr. Glass’ kind of twist, more of the ‘Oh for &$#@’s sake’ The Happening kind of twist. Yes, I am venturing into F#… and maybe never back again… except for work. But never to be back again outside of work. Ah who am I kidding? Who cares. (That includes me)

Unit Testing 4.0 Web Forms Including Event Handlers Like Page_Load Using Dynamic

If you’re too lazy or just plain impatient, you can find the source code here.

So one of the biggest pains in a place that doesn’t lend itself well to be in pain has been trying to unit test web forms like how MVC controllers can be. Before 4.0, this was an aggrevation at the least. Something in between stubbing one’s toe and getting only chocolate icecrfeam when you ask for a @*&*ing swirl. Serious, how hard is it to pull the swirl lever? Really.

Anyways, so there are certain things in .net web forms that were pretty difficult to hande/mock/get around for testing. Let’s take session for instance. Session is a sealed class with a non public constructor. To make things more interesting, the Page class only has a get for the Session property. Now you could go through a lot of hell, mostly reflection, to get aroud this, but who wants to do that? Personally I gave web forms the ole one finger salute, but being that I am working with web forms again… yeah had to find a way to get around this. The answer? Stems from dynamic.

Basically what I had to do is create a class that has a Session property that is dynamic, and then replace the old Session call with a class that calls a property named Session. The effect to the code, as far as the immediate code behind is concerned, is just this simple adding of the class before the Session call. Far as the code to do this, it’s fairly easy too.

First I started with an interface since using an interface will help later if I need to mock.


This is the bread and butter. Basically we have a container that will hold the session object. Making the property dynamic allows it to be set to anything. All I have to worry about is that whatever I set it to has the functionality used by Session. This is started by implementing the interface fo use on the site:


And then one for use with testing:


How are these used? Well if we take a base page:


So what this does is holds two constructors; One for use by .Net when it runs the site and one for the test class when it is testing the page functionality. The second is a way to stop all the stuff that happens in the Page class constructor when it’s time to unit test this [explitive]. This might look familiar to people who have used things such as Dependency Injection with MVC to help with Controller testing. If not, the idea is simple. It basically just gives the Page the test handler we need it to use when testing. If we aren’t testing, then we let it just use the default handler.

You might wonder why I didn’t instantiate the live version in the constructor, and the answer is: Session isn’t ready to be used at constructor time when doing the normal web site thing. Therefore, it will be set the first time the SessionHandler property is called. If it’s a test, well then it’s fine to set it in the constructor since it is bypassing the normal web site… thing.

The unit test looks something like this:


As you can see, a test version of the handler is created and the Session object is replaced with a Dictionary since that’s what Session is… well it has more to it but the most used feature is the key/value match and it happens to be the only part of Session I show in this example.

Since there is the second, test only, constructor on the page, we don’t have to worry about any asp.net stuff getting in the way when instantiating the page. The page will look like this:


As it can be see, the replacement of the Session call with SessionHandler.Session allows the features of Session but also the testability it lacked for so long. It’s true that if you want to use other features of Session, if there are any and I can’t remember, you’ll have to just add them to the test version so they can be called at test time. I can’t do everything for you.

Off Topic: Austin Dudley and Dragon Karate

So every so often I have the need to see where Sport Karate is at this point, mostly because it’s just plain enjoyable to watch. Being a bo guy, there is some difficulty I’ve had in the recent years watching bo competitors. Most of them have exceptional open hand forms that they just kind of hold a bo and make it a weapons form. A far departure from some of the mid to late 90s non-traditional bow work. At the risk of sounding old with my sort “Music just ain’t like it used to be.” rant, it’s nice to know that there are some out there that still carry on the tradition.





Now I could be biased since he’s a student of Dan Cousineau but I think it’s pretty easy to see Austin is the real thing. Any ways, hope you enjoy.

Test Driven Development… A Simplified Start

So I went to a “doctor’s appointment” yesterday and was asked to do some pair(ed?) programming to solve a simple-ish request. Basically it was:

Get a number in a list of integers that is closest to 0.

Sounds easy enough, right? Well there are a few more rules but nothing game breaking:

  • If the list contains a 0 then return 0
  • If the list is null then throw ArgumentNullException
  • If list is empty then throw ArgumentException
  • If there is no 0 then get the number nearest 0
  • If the closest is the same for positive and negative (Say -2 and 2), return the positive

Again, fairly simple thing to work on.

Right off the bat I set about creating the method for doing this since my usual plan is:

  1. Create method
  2. Unit test
  3. ???
  4. #$@%!

The fourth step is actually repeated a couple times.

So after I had done the more brute force sort of way, and had a failing test, it was suggested I start over. This time stick with the TDD like mindset of

  1. Create test
  2. Fail test
  3. Add code to make it work
  4. Watch it succeed

Now the concept of TDD I have known about for a while, but never really had a chance to really jump into it. After all, most companies just want something done now, not done well. Beyond that, I had reservations of how it works and whether I was smart enough to pull it off. After all, I had always thought you have to be able to see the whole method before it’s written in order to create tests. Well after yesterday I can say I was wrong… at least about the second part.

When I started writing test methods I was told to make them each this way:

Always add to a method the most simple way to get a test to run.

Yeah ok, it sounded better when I heard it yesterday. However, I have examples.

So if we take the first requirement:

If listToCheck contains a 0 then return 0

Well what should I write as a test? Easy:


As you can see I am only checking to make sure there is a 0 returned. Side Note: You might notice that the method is red. This is because it hasn’t been created yet. That’s the next step. What should I put in the method? Remember I only want to put in the most simple way to get the test to pass.


Which gives me this guy:


Right now you’re probably thinking that test is the programing equivalent of Master of the Obvious. I mean, of course it passes because it always returns 0. The next unit test might help with seeing the bigger picture:


Ok so now I’m testing:

If the list is null then throw ArgumentNullException

So naturally I have a test that expects an ArgumentNullException to be thrown. I don’t have that yet in the method though so the expected result when running the test is:


Boom. Go figure. Next step is to add only what is necessary to make the unit test pass.


Now you can see that I have added an exception to be thrown if the list is null. Time to run the tests:


Now I know that the method does take care of the first two requirements. From here I go requirement by requirement and create unit tests to make sure the requirements are met and have them all fail at first. After the test fails, add in the minimal amount of code necessary to get the test to pass.

Eventually you will have a working method that is completely covered.

You might be wondering why this site still exists but you might also be wondering why you should go requirement after requirement adding just what’s needed. The answer is actually simple: It lets you know exactly what changes you make breaks any other test. You see, if you were to do the normal “build the method and test” and there’s a test failing, it becomes harder to track down the issue since the method is now a bunch of code. Since there are multiple points of failure when you start writing the tests, tracking down a specific error is hard.

If you go requirement by requirement, test by test, you can easily find anything that breaks the created tests since you are only working one requirement at a time. I the first three requirements are met and tested when you work on adding the fourth and for some reason what you added to the method is breaking one of the successful test, you know exactly where the issue is.

I have the code here if you want to look at it as I’ve I’ve commented a lot of the code. It is in VS 2010 though. However I don’t think there’s anything that couldn’t work in 3.5.

I admit it’s kind of a different way of looking at it, but I found it incredibly useful in it’s simplicity.

Add Nunit Reference through NUGet/ Visual Studios 2010

From the “Just putting this here for me” update desk: NUget

What’s NUGet? It’s a way to help with adding things from templates to Add Ons. In fact, it can even be used to add references to your project like NUnit without having download NUnit by yourself. Basically it’s another way to enhance laziness. I like this.

The page for NUGet is here and you can download it from here.

After that restart Visual Studios (At least I had to) then:


From there:


Now there should be a button in that image but I had already added the NUnit reference. Real easy stuff.

Apparently there’s lot of stuff like add ons and templates… I think I already wrote that but I’m too tired and lazy to bother checking. Anywho a developer should be like candy in a kids store.