Refactor This 02: To The Cloud

by Terry Hughes Friday, April 29, 2011 07:04 PM

Refactor This

Solutions are due Friday, May 06, 2011 at 05:00:00 PM PDT. Source Code

Hi and welcome to team Amazing Electronic Code Cluster (Blue-Cyan). As you know, we are a large provide of things that exist solely in a nebulus mass of stuff (like tubes, interconnected tubes, a whole series of them). Anyway, you're not the astronaut in charge of designing it, we hired you to do useful work. Here's a run down of the existing functionality.

  • A region can contain zero to many availability zones
  • An availability zone can contain zero to many instances
  • Instances need to have valid account numbers when supplied
  • Instances with an availability zone must be uniquely named
  • Instances can only be inactivated in certain situations (the actual situations are not important, but inactivation needs to be prevented when they occur)

We recently experienced ... well let's say something that is statistically distinguishable from perfect. An availability zone was not available yet we continued to try and add instances to it and update existing instance. It wasn't a good situation. This caused us to think that we should handle that a little better. You're first task is to implement the following functionality.

  • Don't save the instance when its availability zone is not available

We've decided to allow for three levels of changes (so please specify which level you are solving for). Remember that whichever path you choose the existing functionality must be maintained and the new functionality must work correctly.

  • Free rein, you are allowed to change everything within the current system
  • Restricted, you are only allowed to change the Save method in InstanceController (this includes changing the content and the signature). You can add new classes and extension methods but you cannot change any other existing class (with the exception of the Program class of course)
  • Compatability, you are only allowed to change the contents of the Save method in InstanceController. Again, you can add new classes and extension methods but you cannot change any other existing class (with the exception of the Program class of course)

Yes, this was adapted from real production code.
No, it has nothing to do with cloud technology.
Yes, I am familiar with the offending code.
No, I didn't originally write it.
Yes, I have added to it's nastiness.
No, I don't feel good about it.
Yes, you are awesome and want to impress me.
No, you are not solving a problem for me (it's just a piece of crazy code I was looking at recently and thought it'd make for a good refactoring exercise).


Lightsaber IDE 002: Basic improvements

by Terry Hughes Saturday, April 09, 2011 08:15 PM

Lightsaber IDE

Now that I have something that works, let's make it better!

The first thing I'd like to do is make the feedback cycle better. I had setup a build file initially

REM make.cmd

IF EXIST bin RMDIR bin /S /Q

"%windir%\Microsoft.NET\Framework\v4.0.30319\csc.exe" /out:bin\DevPad.exe /target:winexe /recurse:src\*.cs /nologo


but I soon realized that a simple mistake would lead to me wiping out my IDE and forcing me to go back to notepad or keep a working copy of the IDE around some other place. Also, I would run the make.cmd and then I'd have to run bin\DevPad.exe. I decided to update the build file to stage the executable and only when the compile succeeds do I overwrite the actual IDE.

REM make.cmd

IF EXIST obj RMDIR obj /S /Q

"%windir%\Microsoft.NET\Framework\v4.0.30319\csc.exe" /out:obj\DevPad.exe /target:winexe /recurse:src\*.cs /nologo


IF EXIST bin RMDIR bin /S /Q

COPY obj\*.* bin >> NUL


To make it a one step process I added an upgrade file

REM upgrayedd.cmd

CALL make.cmd
START bin\DevPad.exe


For tidiness I added a clean script

REM clean.cmd

IF EXIST obj RMDIR obj /S /Q
IF EXIST bin RMDIR bin /S /Q


One annoyance I was running into a lot, was getting prompted with the save dialog AND the confirmation that it was okay to overwrite the specified file when I wanted to save. By storing the file name as a variable I'd be able to know if I'm working on an existing file or something new.

I also wanted to make my IDE smarter. If I wasn't careful I could lose unsaved changes. I wrote the logic of remembering if there were usaved changes and prompt for when there were out in its own class, UnsavedChanges and then wired it up inside MainForm. I don't like how UnsavedChanges has to have a IWIn32Window and Action (the Save() method) but it works for now.

The final bits of improvements I wanted to add are to show the current file on the form, set the default size to 800x600, to start maximized, and to set the default font new Font("Consolas", 10, FontStyle.Regular, GraphicsUnit.Point, 0).

Lightsaber IDE 001: First things first, self-hosting

by Terry Hughes Saturday, April 09, 2011 07:32 PM

Lightsaber IDE

The first thing you'll want to do is get your IDE self-hosted. The best way to figure out what the most important thing to work on is by dogfooding. I also suggest that you use the most basic tools to bootstrap yourself. This way you are more likely to use your IDE rather than the one you usually do.

I've chosen to use notepad with a goal of switching to my IDE ASAP.

So it seems like the first step is to get a form to show up. Obviously, this is rather trivial:

// Program.cs
internal static class Program
    private static void Main()
        Application.Run(new MainForm());

// MainForm.cs
using System.Windows.Forms;

internal class MainForm : Form

I'll need something to type into, a RichTextBox should do for now. I put all the code to add it to the form in MainForm.designer.cs and set it's Dock property to DockStyle.Fill. Next, I need to be able to open and save files. Instead of the traditional File, Edit, ..., Help menus I decided to just have the three items right there as root menus: Save, Open, and New. Some things I ran into: I had to add the STAThread attribute to the Main() method as I wanted to use the built-in OpenFileDialog and SaveFileDialog; the RichTextBox control does not have carriage returns ("\r"), so I had to do a Replace on new lines ("\n") with Environment.NewLine when writing the file; and finally I had to use the Encoding.Default encoding option, so that © et al. will get displayed on the form and saved to the file properly. With that I have a self-hosted IDE that I can begin using.

Refactor This 01: Gilded Rose

by Terry Hughes Friday, April 01, 2011 05:19 PM

Refactor This

I sent around a little challenge at work. Bobby was nice enough to post about it so I'll leave it at that. I'm happy to report that it got picked up by a software craftsmanship course.

Lightsaber IDE 000: A long time ago in a galaxy far, far away....

by Terry Hughes Friday, April 01, 2011 04:45 PM

Lightsaber IDE

So after getting fed up with people complaining about problems with their *favorite* IDE, it occurred to me. Why don't we just make our own? You know, like how a Jedi builds their own lightsaber. Talking about it at work and thinking about it on the commute I came up with a list of big ticket items that would need to be addressed.

  • Editing a file
  • Multiple files
  • Compiling
  • Debugging
  • Test runner
  • Syntax highlighting
  • Find usages
  • Go to definition
  • Intellisense
  • Snippets (prop, propg)
  • Source code integration
  • TeamCity, StyleCop, etx integration
  • ReSharper refactorings
  • Underscoring

While there is a ton more and it's a big task (and most likely a tremendous waste of time) it seems like a fun thing to do, at least for a little while. I also came up with a name, DevPad.

Harmony Hackathon Retrospective Notes

by Terry Hughes Friday, April 01, 2011 04:15 PM

Harmony Hacks Retrospective Notes

In case my handwritting is unintelligible.

  • Justin stepping up to be the customer rep.
  • Liked the environment/facilities
       same room
  • No negative interactions
  • New phrases
  • Good/committed product owner
  • "C# is better than Java" -Scott
  • Good learning experience
  • Made the product owner think about the workflow, rethink the process
  • Demoing often
  • Having a central place to demo
  • Backbone derailment
       (communication problem?)
  • Needed a skeleton/starter kit
  • Not much to do @ first
       a smaller group for a new project (4~5)
       a larger group for adding features to an existing project
  • Needing a benevolent dictator
  • Conflict between getting an understanding of the problem & solving the problem
  • Didn't solve the file backup project
  • Needed more formality
       actually standing up
  • Loving the CRUD
  • Very painful to watch the rubber band effect
  • Didn't do it the way we KNOW we should have

We also thought we should have had a time-lapse camera.

Cropper 1.9.3

by Terry Hughes Friday, November 27, 2009 11:25 PM
Earlier this month I was added to the Cropper project (thanks Jeffrey). After working through most of the easier work items I felt it would be a good time to issue a new release. I'm looking forward to digging in to those tougher issues and implementing the requested features. So go give it a try and feel free to provide some feedback.


Access VisualSVN Server and TeamCity through IIS7

by Terry Hughes Sunday, October 11, 2009 11:20 AM

I run IIS, VisualSVN Server, and TeamCity on the same machine and everything works great, except for one small thing: they all need their own port to listen too. I ended up with IIS on 80, VisualSVN Server on 81, and TeamCity on 82. Yuck! The URLs look horrible and you have to remember to explicitly prefix the non 80 ports with http://. What I really want is for everything to be on port 80.

Googling the interwebs, I learned that what I wanted to do was have IIS be a reverse proxy for VisualSVN Server and TeamCity. I also learned that there is an extension, Application Request Routing, for IIS7 that does this very thing. I found an article that outlined how to do what I wanted, but there are a couple of special things that need to be done to get VisualSVN Server and TeamCity working.

Application Request Routing
I enabled the proxy, which is explained in the Configuring Rules for the Reverse Proxy section of the previous link, and took this opportunity to add a rewrite rule to the Default Web Site that will prefix www to any request that does not have it, the rule configuration can be found a little over halfway through the video on the URL Rewrite Module page. As an aside, after adding the rule, Google Reader showed that existing postings where new entries. I think this is because I had FeedBurner pointing to not I'm not sure if there would have been a way to prevent the duplication of posts since the URLs are technically different.

At first I tried to proxy TeamCity from a subdirectory, but it returns link references to items at the root URL and I was unsuccessful at correctly rewriting the links. Since I run other applications within the same web site, I couldn't have TeamCity use directly, so I decided to add a TeamCity subdomain in my DNS settings. I updated my prefix www rule to have a second condition so it didn't try to change to I then added a rewrite rule to the Default Web Site that matches any url with the condition that the HTTP_POST is to rewrite to http://localhost:82/{R:1}.

VisualSVN Server
Since TeamCity had it's own domain I wanted to do the same thing for VisualSVN Server, only it didn't work. Remember how VisualSVN Server will automatically append svn/ when you type in just the root address? There is a reason for this, I'm not sure what the reason is, but it is how VisualSVN Server works. So VisualSVN Server has to be proxied at a subdirectory. At first I set the rewrite rules to use a nonexistant directory but found out that IIS blocks files that have certain extensions. Some of the blocked extensions are the very same as the file extensions that I want VisalSVN Server to serve. In order to work around that and not expose those file types in other applications that are running in the same web site, I opted to have a physical directory so I can override the default IIS settings. I created a new folder called svn within the wwwroot folder on my server. I then added a rewrite rule to the svn folder that matches any url to rewrite to http://localhost:81/svn/{R:1}. I couldn't find a way in the UI to change the file extensions that IIS served but this post shows the xml configuration syntax needed to tell IIS that it's okay to serve certain extensions. I updated the web.config in the svn directory to allow for the extensions .config, .cs, and .csproj. This isn't all inclusive, just want I've ran across so far. Then i noticed something. If I typed in the links went to{Repository} and would give a 404 error if clicked. Realizing that the ending slash was needed, I played around a bit and figured out that by adding a rewrite rule to the Default Web Site that matches ^svn$ to redirect to I could always ensure that there was an ending slash. The other thing with VisualSVN Server is that it expects it's images and styling files to be at the root of the URL. So I had to copy the files under %ProgramFiles%\VisualSVN Server\htdocs\ (minus index.html) to the wwwroot folder.

The order in which the rules are processed matters. If the rule for TeamCity happened after the rule to append the ending slash for VisualSVN Server, would take me to, not exactly what I wanted.

A side benefit to all of this is that now I can close the non 80 ports on my router and disable the firewall rules on the server. So now I have what I wanted. It's not exactly perfect, I'd must prefer TeamCity to be in a subdirectory like VisualSVN Server, but I can live with it. For now.

When to remove hacks?

by Terry Hughes Friday, April 24, 2009 04:15 PM

A coworker of mine ran into a bummer of a problem and asked for advice. Over lunch we talked about what to do, I personally felt that option 2 was the safest solution. I didn't really like adding bad code to cover up bad code and I began to think about how you would know when the hack could be removed. Then I threw out the idea of writing a test to verify that the bad condition is present. When the test fails you will know to remove (or at least re-evaluate) the hack. He thought it was a neat idea, can't wait to see what he comes up with.




by Terry Hughes Saturday, May 03, 2008 10:58 PM

I attended the last event with a co-worker a couple weekends ago. This was my first open space experience, which was amazing to see in action. I wasn't sure what to expect but left realizing that it is more about the conversation, interaction, and socialization/networking than learning. I left refreshed and energized about what I do and I'm looking forward to new possibilities.

About Me

TerryH   I'm a senior software developer currently working on a foreign exchange portfolio management system for a financial company with a bunch of awesome people.


The opinions and viewpoints expressed on this blog are not necessarily those of a cultured, tactful, or well-balanced person. This blog is a product of me, who is solely responsible for its content. Me - Stealing From Smart Developers.

Copyright © 2008-2011 Terry Hughes. All rights reserved.