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.

}

Monday, March 14, 2011

Happy pi day!

{

class Program
{

static double PI(int i, int limit)
{
return (i > limit)? 1 : (1 + i / (2.0 * i + 1) * PI(i + 1, limit));
}

static void Main(string[] args)
{
double pi = 2 * PI(1, 2011);
Console.WriteLine(pi);
Console.WriteLine("Happy pi day!");
Console.ReadLine();
}
}


}

Saturday, January 29, 2011

Primes, Sums of Primes, 2011 in C#

{

I had no idea:

2011 = 157 + 163 + 167 + 173 + 179 + 181 + 191 + 193 + 197 + 199 + 211

After learning that not only was 2011 was prime, but that it was the sum of consecutive primes, I sought to write some C# code that would demonstrate this. I wrote it in a functional style, meaning at some point to port it to F# – you'll recognize my in a forthcoming post on computing primes with F# which is actually an implementation of the algorithm I first worked through in the below C#.

Func<long[], IEnumerable<KeyValuePair<long, string>>> GenerateConsecutivePrimeSums = (primes) =>
{
List<KeyValuePair<long, string>> primeSums = new List<KeyValuePair<long, string>>();
for (int i = 0; i < primes.Count(); i++)
{
List<string> operands = new List<string>();
long primeSum = primes[i];
operands.Add(primeSum.ToString());
for (int j = i + 1; j < primes.Count(); j++)
{
primeSum += primes[j];
operands.Add(primes[j].ToString());
if (!primeSums.Any(ps => ps.Key == primeSum) && primes.Contains(primeSum))
{
primeSums.Add(new KeyValuePair<long, string>(primeSum, String.Join(" + ", operands.ToArray())));
}
}
}

return primeSums;
};

Action primesAndSums = () => {
// generate primes up to 2020
var primes = GeneratePrimes(2020);
// generate arrays of consecutive primes that add up to prime
var primesSummed = GenerateConsecutivePrimeSums(primes.OrderBy(a => a).ToArray());
// go through the summed primes (dictionary of prime as key, added numbers as value)
// Console.WriteLine formatted results
primesSummed
.OrderBy(ps => ps.Key)
.ToList()
.ForEach(ps => Console.WriteLine(ps.Key + " = " + ps.Value));
};

primesAndSums();


 



The code actually calculates sums of the primes it generates, click here for all the other primes which are sums of consecutive primes up to 2020:



}

Monday, January 24, 2011

Interesting Data Points From How Facebook Ships Code

{

I ran across this blog entry which is an unofficial attempt by somoene to glean the internal workings of Facebook’s product and software development processes. There are some very interesting things to be noted, most of which I’ll just quote from the article directly:

“resourcing for projects is purely voluntary”

It’s interesting from this unofficial post that project dynamics are organic and based on what engineers want to work on. It begs the question of how the more difficult and less pleasing problems are solved but I would hedge that by having people who are talented and motivated, difficult and more grimy problems might be what attract people that are always seeking challenges. Indeed, from later in the article: “engineers generally want to work on infrastructure, scalability and “hard problems” — that’s where all the prestige is.”.

“Engineers handle entire feature themselves — front end javascript, backend database code, and everything in between.  If they want help from a Designer (there are a limited staff of dedicated designers available), they need to get a Designer interested enough in their project to take it on.  Same for Architect help.  But in general, expectation is that engineers will handle everything they need themselves.”

This is one of the more fascinating points because it reflects one side of an approach to programming that I’ve encountered: that delivering some functionality is a process of ownership where a person generalizes across disciplines and technologies. The opposing viewpoint sees programming more in terms of division of labor with staff assigned to “tiers” of the technological back end: database developers, “business layer” or component programmers, user interface and so on. I always think of sports with this dichotomy where the former thinks of software development like basketball – a game that involves some specialization but really rewards the generalist who can play any role on a team, and football – a game of specialists where it’s a liability to do too many things since even the physique is developed on a per position basis.

Many people acknowledge generalists for small organization but it appears that Facebook, a company as large as they come, prefers them.

“engineers responsible for testing, bug fixes, and post-launch maintenance of their own work.  there are some unit-testing and integration-testing frameworks available, but only sporadically used.”

There are a few edits and comments to this point but the reason I find it interesting is that they are a corollary to the views that Joel Spolsky expressed on the StackOverflow podcast. Many developers, myself included, berate ourselves on a lack of unit testing with the assumption that “everyone is doing it, why can’t I?” As time passes, however, I realize that TDD and Unit Testing are very important but not essential. It’s a very unpopular opinion and while I admit that I strive to make my code testable and, especially on personal projects where I have enough control, architect my software so that it’s testable from the get-go.

}

Saturday, January 22, 2011

Learning F# with FizzBuzz: Match

{

I had previously done a solution for FizzBuzz in F# (from a long time ago) but now that I’m more familiar with the idioms of F# I tend more towards pattern matching instead of if/else logic for quite a few scenarios. Here is an example of my rewriting what could otherwise been a conditional if/else checking for factors of 3 and 5, but using a pattern match instead.

#light 
let fizz num =
match (num % 3 = 0) with
| true -> "fizz"
| false -> ""

let buzz num =
match (num % 5 = 0) with
| true -> "buzz"
| false -> ""

let outform num fizCalc =
match fizCalc.ToString().Length = 0 with
| true -> num.ToString()
| false -> fizCalc

let rec printer nums =
match nums with
| [] -> printfn "-"
| h::t ->
printfn "%s" (outform h ((fizz h) + (buzz h)))
printer t

let numbers = [1..100]
printer numbers




Although I read claims that if/else conditional operations are more readable, I think the pattern match used consistently across scenarios above makes for something I find quite readable.



}

Thursday, January 20, 2011

On Leadership

{

“If you want to build a ship, don’t drum up people to gather wood, divide the work, and give them orders. Instead, teach them to yearn for the vast and endless sea”

- Antoine De Saint-Exupery, author of "The Little Prince"

Originally posted on Chris Sells’s blog but worth some thought.

}

Wednesday, January 19, 2011

Download .NET Source Code

{

I’ve been having a problem that warrants looking at the source which can be downloaded right here.

}

Learning F# with FizzBuzz

{

Eons ago I'd posted on how I’d rewritten how I would solve FizzBuzz to have a more functional style (here is an imperative version), inspired by Tomas Petricek and John Skeet’s Functional Programming for the Real World. Shortly thereafter I had written a version in F#, I’m not sure what prevented me from posting it but here it is:

let numbers = [1..100]
let fizz num =
let res = if (num % 3 = 0) then "fizz" else ""
res

let buzz num =
let res = if (num % 5 = 0) then "buzz" else ""
res

let outform num fizCalc =
let res = if(fizCalc.ToString().Length > 0) then fizCalc.ToString() else num.ToString()
res

let rec printer nums =
match nums with
| [] -> printfn "-"
| h::t ->
printfn "%s" (outform h ((fizz h) + (buzz h)))
printer t

printer numbers


I can already see now areas that are still influenced by the C# - most notably the use of if rather than using match for everything. I will clean it up, hopefully demonstrating an increased facility with F#. If you are looking at the above and know F#, what are some idioms or language constructs that I am neglecting in the above code? Type annotations for one... what else?



}

Sunday, January 02, 2011

Information Diet Planning – Newsletters

{

Given the amount of information that is out there it’s difficult to throttle back and actually digest it. A quick look at any web page will set that context; hit StackOverflow and you’ve got several dozens of links to follow, each a rabbit hole on its own. The same can be said for Hacker News, Channel 9, and just about every other popular developer site that is out there.

What tools exist to process this more efficiently? Perhaps, I hope, one of the oldest tools out there: email.

I came across this solution reading the Washington Post’s email newsletter I subscribe to one morning. I realized I’d get more out of the news by scheduling a more deliberate reading of the newsletter than by going to the site and being bombarded by stories and links that, while no doubt interesting, would result in an overflow of words that would invariably lose their depth.

I decided for the next week not to visit any news site directly but rather to subscribe to several newsletters and schedule the reading from directly from my email inbox. I also turned off the radio so that rather than hearing multiple versions of syndicated stories from the Associated Press, I would be able to dive more deeply and think more at length about what I read. The goal was to buck the trends of the modern day information seeker:

“characterised as being 'horizontal, bouncing, checking and viewing in nature. Users are promiscuous, diverse and volatile.' 'Horizontal' information-seeking means 'a form of skimming activity, where people view just one or two pages from an academic site then "bounce" out, perhaps never to return.' The average times users spend on e-book and e-journal sites are very short: typically four and eight minutes respectively.”

I wanted to get more vertical with my reading rather than surfing through multiple versions of the same piece of news.

I was so pleased with the results that I decided to use the same strategy with technical news and articles. I have long been a member of The Code Project and of the software development newsletters I receive (surprisingly few) theirs is probably the best. Each day, and then in a weekly digest, there is an email in my inbox with a decent roundup of technical articles. Although not all of them pertain to my skills set or interests, there are usually one or two good links to follow up with; in this newsletter received on the last day of the year I could easily spend an hour on The Best Technology Writing of 2010 or catch up with Rob Connery giving his sentiments on BizSpark.

The experiment will continue and although I do admit to “skimming” from time to time, even the tepid commitment that I’ve made seems to make my time online much more efficient. The next steps regarding newsletters is to find some more development related ones that come in a digest form, preferably weekly so that I can spend a full week on the contents.

}