Monday, December 27, 2010

Quickly Testing and Changing Code

Sometimes when running software it is hard to make a particular code path execute.  Like if you want to ensure your code behaves correctly when bad data is passed to your function.  I love Visual Studio and the debugging capabilities are one of the things I love about it.  Using the watch window the values of variables can be changed to check those code paths, and by right clicking on a line one can set the next statement of execution wherever they want.

If we have code like this

int customerId = getCustomer("bob");
bool log = Settings.ShouldLog();
if(customerId < 0 && log)
{
    log.LogError("bob is invalid");
}

We can make the log code run by setting a breakpoint on the if line and then changing customerId to -1 and log to true in the watch window.  If we want to change the code in the if block and retest changing these values every time is repetitive.  Changing the breakpoint on the "if" to a conditional breakpoint allows additional code to execute when the breakpoint is evaluated.  We can leverage this feature to change the breakpoint condition to "customerId = -1."  Each time the breakpoint is evaluated customerId will be set to -1.  This is great, but in our example we have two variables which need to be set properly to satisfy the if condition.  To change both variables the conditional breakpoint can be changed to a lambda like this: () => { customerId == -1; log == false;};

I've found this technique can help me rapidly iterate on code changes to ensure edge case scenarios or hard to reproduce data scenarios execute properly for code that isn't under test.

Monday, December 20, 2010

How hard is it to change a column datatype?

One of our clients initially specified one of their columns as an int, and after being deployed for a month realized it needed to support other characters.  I found a couple of ways to do this in SQL Server 2008.

We'll use this sample table:

CREATE TABLE dbo.Orders
      (  OrderId int NOT NULL IDENTITY (1,1),
         OrderNumber int NOT NULL
      ) 
INSERT INTO Orders
      SELECT 234
     
SELECT * FROM Orders



The way I normally do things is by generating a change script in SQL Server Management Studio.  Here is the SQL output by that process.

CREATE TABLE dbo.Tmp_Orders
      (
      OrderId int NOT NULL IDENTITY (1, 1),
      OrderNumber varchar(50) NOT NULL
      )  ON [PRIMARY]
GO
ALTER TABLE dbo.Tmp_Orders SET (LOCK_ESCALATION = TABLE)
GO
SET IDENTITY_INSERT dbo.Tmp_Orders ON
GO
IF EXISTS(SELECT * FROM dbo.Orders)
       EXEC('INSERT INTO dbo.Tmp_Orders (OrderId, OrderNumber)
            SELECT OrderId, CONVERT(varchar(50), OrderNumber) FROM dbo.Orders WITH (HOLDLOCK TABLOCKX)')
GO
SET IDENTITY_INSERT dbo.Tmp_Orders OFF
GO
DROP TABLE dbo.Orders
GO
EXECUTE sp_rename N'dbo.Tmp_Orders', N'Orders', 'OBJECT'
GO


I wasn't thrilled about copying the entire table for our whole dataset.  I thought the change script should create a new column, copy the data to it, remove the old column, and rename the new column to the old column's name.  Googling about for a way to do this I found it was even easier in SQL Server:


ALTER table dbo.Orders ALTER Column OrderNumber varchar(50)


It's a good old one liner :)

Monday, December 13, 2010

Oh how I love Visual Studio

Over the past month I've been working with a lot of JavaScript and I was really missing these features from Visual Studio:

  1. No find in solution - I was actually opening up Visual Studio and using the find feature to search my JavaScript directory structure to find things.
  2. No solution explorer - I had to keep switching over to Windows Explorer to see the files I wanted to open.
  3. No AhnkSVN - I love having source control integrated, so I don't have to worry about adding files, ignoring files, etc.
I found out I can get all my Visual Studio love by creating a web project at the root of my JavaScript Solution.  It gave me back all the features I missed without any problems.

Huzzah!


Monday, December 6, 2010

OpenID on DotNetNuke

I hate remembering passwords and I don't like creating accounts on sites and worrying they are storing my password incorrectly.  I love OpenID because it solves those two issues for me by enabling OpenID sites to use an already existing account for login.  For example I use my gmail account on Facebook and StackOverflow to login.

This post walks through enabling OpenID after installing DotNetNuke.
  1. Click login and login as host
  2. Admin Menu --> Extensions menu option
  3. Authentication System section
  4. click the pencil icon next to 'DNN_OpenIDAuthentication'
  5. check the Enabled? checkbox
  6. Click the 'Update Authentication Settings' to apply the settings.
  7. logout
  8. click the login button
  9. select the OpenID tab
  10. In the OpenID: field enter the url for your OpenID provider (google's url is https://www.google.com/accounts/o8/id)
  11. Click the Login button
  12. Under the 'Register a new account' section change the first name, last name, display name, and email address.
  13. Save the changes, logout, and log back in to ensure it worked.
DotNetNuke leverages the ASP.NET membership provider infrastructure and I plan on incorporating it into future projects I work on.  The biggest issue I see with the technology is that most implementations today require entering your OpenID provider URL.  I think this is bad user interface.  I like how StackOverflow shows pictures of many providers allowing a user to select their provider without needing to memorize or lookup a URL.