Monday, June 20, 2011

NDC 2011

{

It may be easy to miss amidst all the hoopla around Windows 8 but the Norwegian Developers Conference just released all their session vidoes on the site.

It’s been a while since I’ve gotten to go to a conference but the last few I have attended were put on by Microsoft. Although this has advantages in the sense that it coalesces the “experts” of specific Microsoft technologies, many of the presenters work at Microsoft and therefore don’t have a similar problem set that you or I might slogging our way through building products, often with older (read: more “boring”) software. There are also some talks on development techniques like BDD and DDD that are not product specific.

The good thing about NDC is that it seems a lot of the presenters come from an outside perspective so it’s a good balance for a lot of the MIX / PDC type stuff I’ve been watching of late. Without further ado:

Direct Download from NDC website: Day 1 | Day 2 | Day 3

Roy Osherove’s “unofficial” torrent

}

Wednesday, June 15, 2011

Search, Match, Replace, Generate

{

Many moons ago I got about 0.25 seconds of fame when nRegex, a tool I wrote for evaluating regular expressions of the .NET flavor,  got a bit of acclaim. It was one of the better days of my life, an encouragement that sometimes struggling alone in a South Dakota basement can lead to a little bit of notice.

One of the tricks that keeps me going back to nRegex is being able to generate code by using a regular expressions. Regular Expressions, though often reviled, turn out to be quite handy in a lot of situations.

Let’s say you have a table that looks like this:

	CREATE TABLE PackingList(
PackingListId INT IDENTITY PRIMARY KEY,
PackingItem VARCHAR(50),
Destination VARCHAR(50)
)
GO


 



Let’s say you have a list of values:



Shoes
Camera
Laptop
Wallet


The list is short for brevity but let's say you want to insert them all into your table for a destination called //Build/. You know the syntax for an INSERT but it's a bit of a nuisance to type over and over again. One thing you can do is to use a regular expression to match and then reference the results of your match in a replace. Over on nRegex, we'd paste in our list of items into the main text area and then use the regular expression (.+) to match each item, line by line. Because the items are matched into a group with your parenthesis, you can now write something like the following for your replace:



INSERT INTO PackingList VALUES('$1', '//Build/')


And voila! You now just have to copy the results:



INSERT INTO PackingList VALUES('Shoes', '//Build/')
INSERT INTO PackingList VALUES('Camera', '//Build/')
INSERT INTO PackingList VALUES('Laptop', '//Build/')
INSERT INTO PackingList VALUES('Wallet', '//Build/')



But that's just a tip of the iceberg. There's a lot more nifty regular expressions tricks for working with code. Here's another one I run into quite often. Let's say I have some code that looks like this:



  rs["foo"] = myFoo;
rs["bar"] = myBar;


I want to swap what's on either side of the equals sign to do the opposite type of assignment. Here's my regular expression:



(.+)\s=\s(.+);


And my replacement expression



$2 = $1;


Et voila aussi! You can now copy the swapped values to wherever they need to go.




One final thing: any place that offers you regular expressions you can use these techniques. There are some subtle differences but as long as you have a conceptual understanding of what your goal is it's quite easy to bend to the flavor of regular expressions presented. For example, if you are using Notepad++ and want to accomplish the same thing, you reference your groups with a leading backslash rather than the "$" character - in our first example you would use the following:



INSERT INTO PackingList VALUES('\1', '//Build/')


This is, of course, just scratching the surface. It’s not that hard though! Once you learn the meaning of things like ^ or $ then you can manipulate strings in all sorts of ways that may have once not seemed possible. The best way to learn regular expressions hasn’t changed for many years. The two canonical books I always refer to are Mastering Regular Expressions by Jeffrey Friedl and the Regular Expressions Cookbook by Goyvaerts and Levithan.











 



Although nRegex will allow you to hobble by I also recommend RegexBuddy. Rexv (which inspired nRegex) is a good tool though the Regular Expression engine is not .NET.



Last thing: do you have any nifty regex to code generation tricks you use on a regular basis? The audience of one you have in this space would love to learn them.



}

Tuesday, June 14, 2011

On Features

{

“More features isn’t [sic] better. More features is unfocused. More features means you’ll do them worse. More features means you probably don’t have any differentiation. If you’re doing a startup, you should have less [sic] features than your competitors. If you have more features, you’re probably doing it wrong.”

Nugget from Peter Van Dijck. So the question is: when do you add a new feature? Joel Spolsky (who has written about most interesting things already, way back in the day (although it would be interesting to hear if things are still done the same in the present day FogCreek) ) wrote about how they decided on new features by prioritizing and voting as a group.

As a developer of one I’m interested in techniques on deciding what to put on a roadmap and how to prioritize features. Any insight?

}

Monday, June 13, 2011

Closures, Anonymous: JavaScript influenced C#

{

I’ve taken knocks in the past because of my penchant for closures and lambdas. Syntactically they never looked that strange to me and most of the time when I used them it was because it made more sense to get a sense of the flow of how things were assigned. I thought: this feels so natural, what makes it so different from some of the people I’m around using C#? Here’s an example of how I’d approach something: in the constructor, assign a loading handler, in that handler, assign a click handler, and rather than putting it in some separate method where I’d have to scroll or look elsewhere, just place it here:

        public MainPage()
{
InitializeComponent();

this.Loaded += (o, e) =>
{
myButton.Click += (_, e2) =>
{
myText.Text = "Hello World";
};
};
}


But then the other day I was tinkering with KnockoutJS, writing some Javascript with jquery and it dawned on my why I approach things the way I do.



        $(document).ready(function () {
$('#myButton').click(function () {
$('#myLabel').text('Hello World');
});
});


It's always interesting to make that connection more formally than trying to explain why my approach would be “better.” The advantage of being able to use a closure with the above approach has always been the main rational I’ve given when forced to come up with an answer.



}

Friday, June 10, 2011

Getting Random Rows, Random Numbers with Sql Server

{

The 5 second version of this, should you arrive via search, is that to get random rows, simply use an TOP query with  ORDER BY NewId() expression. It’s really that simple! Take a look:

-- SAMPLE TABLE
CREATE TABLE Keywords(
KeywordId INT IDENTITY(1,1) PRIMARY KEY,
KeywordValue VARCHAR(50)
)
GO

-- SAMPLE DATA
DECLARE @N INT
SET @N = 1
WHILE @N < 101 BEGIN
INSERT INTO Keywords(KeywordValue)
VALUES('key word ' + CONVERT(VARCHAR(5), @N))
SET @N = @N + 1
END

-- A TOP QUERY WITH ORDER BY NEWID()
SELECT TOP 5 * FROM Keywords ORDER BY NEWID()


The longer version of this is that I recently was asked to generate random keywords for a website I was working on with a lookup table. Getting random numbers in TSQL is easy, the RAND() function does all the magic but getting rows is a lot more tricky, especially if you want to make sure you exclude anything you’ve previously retrieved. The above technique worked quite well and made it easy to allow for them to add and remove keywords on demand.



One interesting application of this is that you can combine it with RAND() to get random numbers via multiplier and random rows (RAND gets a random between 0 and 1 leaving you the responsibility to multiply it to control the range you want).



Here is where I thought this could be taken as a flexible way to get random numbers within a bounded range:



-- get a series of random numbers
SELECT TOP 5 Rand() * KeywordId FROM Keywords ORDER BY NEWID()


There’s a lot of flexibility but SQL Server makes it trivial. 



}

Wednesday, June 08, 2011

Silverlight Is Dead…

{

… a title which I hope brings massive attention to this blog and to this rant against those that are rabid in their attacks on Microsoft and the Silverlight platform.

First and foremost: Silverlight 5 has yet to be released. How can the platform be “dead” if we’re on the verge of a new version?

Secondly: Spend some time listening to MVPs and Microsoft people. Even though there is this awkward gag order until //Build/ it seems quite obvious that the platform will continue to be a viable option for developers.

Third: HTML5 is no panacea. Spend a few days looking into the different specifications and the varied support by browsers and platforms. It will become obvious that the W3C is being true to their word when they say it won’t be “finished” for a while.

Fourth: Grow Up. Yes you’ve made this enormous investment in Silverlight or WPF. I have too; the last 3 years of my life have been in the trenches and my employer took a calculated risk to do a significant amount of work in Silverlight. Does that mean that every rumor should shake you to the very foundation and cause emotional outbursts at being abandoned? Stay the course. When you have real information from Microsoft directly then you can have your break down if it’s not what you expected. I don’t consider my time wasted no matter what the case. I can take my knowledge of MVVM elsewhere. I know a lot more than I used to about asynchronous programming.

Fifth: Let’s just say it’s true. Does it still warrant all that drama? You’ll be fine. People are dying in wars, unrest, and natural disasters all over the place but guess what? If you’re a Silverlight Dev and They all of a sudden remove the Silverlight Project Template from your Visual Studio and run a secret binary on your local machine to destroy the plugin removing all evidence of its existence globally you’ll still be alive. You’ll still dust off your trousers and either port your work or begin new and fanciful things in a platform that you find available.

Sixth: Try to make a distinction between people who are vocal versus people who are actually building software. I’m aware of a project right now where I work that makes heavy use of COM. Yeah that “not dead but done” COM. People who build things usually have their heads down creating value. I know there are a lot of talking heads online, especially talkers unsympathetic to the “evil” b0rg in Redmond, but there’s a lot of noise for very little signal. It’s cool that people get passionate (I’m being passionate myself now too) but sometimes it’s a good thing to take a step back and survey the landscape of what is real and what is bluster.

I’ve already admitted I could be wrong. But as significant a decision Microsoft has potential to make, my approach is to take it in stride, try to learn as much as I can and be flexible since in the tech world, no matter who you are, change is inevitable. For some of us change, with all the concomitant turbulence, is fun.

}