Friday, March 11, 2016

Making Code Maintainable

One of the things I’ve learned over the years is to recognize when some code should be changed to be more maintainable. I’ll walk you through some code I saw during a pull request that we ended up refactoring to be more maintainable.

We were calling an external API that returns string “yes” and “no” for a bool value sometimes, and other times it sends a “true” and “false”. Obviously we wanted to accept both and convert them appropriately. The first version to accept these values was to have a property that checked the values of itself to let us know if it was true or false, it looked like this:

When we were mapping to our model, we were simply doing this:

When reviewing, we recognized two main issues. The first issue is that our values are duplicated, so if the API started sending us 1 and 0, then we might go update one area and forget to correct the other section. We could write some tests to let us know, but we didn't want to risk it, and of course there's a better way. There's always a better way. The other issue was that if one of the other fields that was sent to us was a "yes", but not actually a bool, then we'd be in trouble.

So, how did we solve the two issues? This is how:

With the fix, you'll notice that we're checking our destination model type now instead of the field's IsABooleanProperty. This fixes our second issue of getting an invalid bool. Our first problem is fixed by having the valid bool values a single time in a convert to bool method, and getting rid of the IsABooleanProperty. If you have other approaches to how to solve this problem or more examples of making code maintainable, please comment!

kick it on

Wednesday, March 09, 2016

Good Ole Refactoring

I was talking with some co-workers about a pull request recently and we all decided we didn’t really like the look of some code.

Here's the code:

Our main issue with the code was that it was difficult to read and immediately understand what was going on.

Here are the things you should know:

  • The SystemClock is a Headspring nuget package
  • IsDayOnWeekend just checks if the current day is a Saturday or Sunday
  • IsHoliday checks whether or not the day falls on a holiday

So we did a small refactoring to make the code more readable.

  • So we renamed the method to include GetPreviousBusinessDay
  • We get the last 365 days, we could've just as easily gotten the last 30, but decided to just get the full year.
  • After we get the last 365 days, we get only the valid business days
  • Then we order those dates descendingly, skip the number of days back we want to go, and then grab the first date

Our refactoring is more readable, has some "clever" code that we like, but aren't married to either. I'm curious to know other opinions, and other solutions that we could've used.

kick it on

Monday, January 07, 2013

What to do when you are locked out of SQL Server

I was recently helping out a fellow Headspringer when we realized we were locked out of his local SQL Express installation. We just needed to start SQL Server in single-user mode, give rights to whatever users we want, and then start back in normal mode. Here’s what we did:
  1. Launch SQL Server Configuration Manager (Run -> SQLServerManager11.msc)
  2. Stop SQL Server (SQLEXPRESS) instance
    (To stop: right-click stop or click then click the stop icon)
  3. Go to the properties of SQL Server
    (To get to properties: right-click click properties or click then press alt+enter)
  4. SQL Server 2012
    Click “Startup Parameters” tab and enter “–m” then press Add, Apply, and close the window

    SQL Server 2008/2005 Unfortunately I don’t have a screenshot of the 2008 or 2005 properties window, but I believe the startup parameters is under the advanced tab and make sure to add separate the parameters by ; (this bit me the first time around)
  5. Start the SQL Server (SQLEXPRESS) service
    (To start: right-click and click Start or click then click the start icon)
  6. Run SQL Server Management Studio
    (To start: win+r and type ssms press enter)
  7. Connect and add the BUILTIN\Administrators or whatever users you prefer to add
  8. Remove the –m startup parameter
    1. Stop SQL Server
    2. Open Properties
    3. Remove Startup Parameter
    4. Start SQL Server
    5. Good to go
View more at

kick it on

Friday, October 12, 2012

Building a Feature Branch Configuration Template in TeamCity

I don’t know about you, but sometimes we have frequent feature branches on our projects. Sometimes we don’t go through the trouble of setting up a build configuration in TeamCity if it’s a quick feature, but sometimes we like to see the pretty green checkbox in TeamCity when our build finishes successfully…especially when more than one person is working on the feature. We also like to deploy straight to our testing environments from a feature branch occasionally for testing, so it’s important for adding a build configuration quickly and accurately in TeamCity. I thought I’d give a demonstration on how to create one.

There are two ways to make a template. From scratch and from an existing build configuration. I will not talk about the “Extract Template” option because it’s literally clicking a button and giving a name from within the “Edit Build Configuration” screen. However, there are two things you’ll need to look at in order to use the extracted template as a feature branch template. So look at steps 2 and 4 below for the details if you have an existing build to use as a template.

Step 1: Click “Create template” under “Edit Project Settings” (I’m using version 7.0.4 of TeamCity btw)


Step 2: Fill in the name…I called mine Feature Branch Template (original I know). If you have a different build number per feature branch, which we do on this project, you can put %BuildNumber%.{0}.0 for the “Build number format”, otherwise, just leave it as the default (like below). Specify your “Artifact paths”, ours usually looks something like “build\latest\*.zip”. Click “VCS settings”.


Step 3: Here’s where the fun comes in…click “Create and attach new VCS root”.


Here you will select your VCS and fill in the appropriate information. The only VERY important thing you need to do is input “%BranchName%” into the Branch name. What this does is creates a parameter that will be required by your build configurations that use this template. Obviously we are using Mercurial, but you would just do the same as above for the equivalent of a branch in your source control.


When you’ve completed filling out the required information and tested the connection, Save. (Pro Tip: Don’t put %BranchName% in that field until after you’ve done “Test connection” with a real branch name.)

You should see this after saving:


Step 4: I’ll just leave all the defaults on the “Version Control Settings” screen, but if you require any of those settings, please set them if they apply to all your feature branches. Click “Add Build Step”.


Step 5: Configure your build steps. We only have two steps because we use psake…so basically we call the build.bat in our repo root with specific params and the other step just runs our unit tests with coverage.

Step 6 (optional): Configure fail conditions if needed…the most common condition I’ve seen is looking for specific text in the build log.

Step 7: Configure Build Triggering…we use the VCS Trigger, which detects when code is checked in. After clicking “Add new trigger”, you should see this:


I just use the default values. Click Save.

Step 8: Click Build Parameters – You should see BranchName <value is required> and BuildNumber if you specified that one in step 1.

If no BuildNumber specified:


If BuildNumber specified:


Okay, you’re done. Now you can start using the template…here’s how:

Under “Edit Project Settings”, click “Feature Branch Template”. In the bottom right, you should see this:


Step 1: Click “Create Build Configuration from Template”

Step 2: Give it a name…typically I name it the branch name.

Step 3: Now click Build Parameters on the right

Step 4: Click the <value is required> for the parameters you specified above and set the value. Click Save.

You’re done.

The way we were handling this before was by copying the build configuration and then creating a new VCS root with the appropriate branch name and add to remember to change the build number…now it forces us to tell it and we don’t have to create the new VCS root.

Please provide any feedback or post a comment if you have any questions.

kick it on

Sunday, September 23, 2012

Export to Excel with MVC

I’m sure you’ve all had to export to Excel at one point or another. I created a small wrapper around the EPPlus library for MVC. Basically my wrapper contains a few helpers for common formatting, an ActionResult, and a column definition. Here’s what the end result looks like in the simplest form:

Basically this exports all the columns and records in the SampleInfo collection. So if you want to specify the columns you want, you can do this:

The ExcelColumnDefinition.Create<> is kinda ugly, so you could create a small helper method for readability like this:

Now the above example looks like this:

So this is what the ExcelColumnDefinition looks like:

Here’s the actual ActionResult:

So that’s it. If you have any questions, please feel free to comment below.

You can browse or download the source on GitHub. The sample project has examples.

Shout it

kick it on

Thursday, August 30, 2012

Simple Way to Toggle on Hover with CSS

Here’s a really simple way to toggle actions or more information to keep your pages clean.

View Demo

kick it on

Saturday, August 25, 2012

A Method for Populating a Dropdown List in ASP.NET MVC

I’m asked quite a bit how to bind a dropdown list in ASP.NET MVC. So there are about 10,000 ways to do it, but here’s one.

First, we’ll create a simple interface to return a list of SelectListItem so we can use the built-in DropDownListFor() later on.

Then we can create a SelectListProviderAttribute to take in the Type of list provider we want. You can see in this example I'm instantiating the object with could do this with Activator or something else.

Now we just implement an ISelectListProvider and add the attribute to our model like this:

Sample property on view model:
public int CommunityId { get; set; }
Now we just call our new HtmlHelper in our view and we're done. So here's the helper:

And now we just call it from the view like this:

@Html.DropDownListFor(x => x.CommunityId)

I'm sure you all realized that I used some additional helpers in code samples above. So for the curious few, I uploaded the full sample project to github. Browse the source now

Shout it

kick it on

Related Posts Plugin for WordPress, Blogger...