Feb/100
Functions Instead of Predicates in Linq Functions
I did not realize that functions can fill in for predicates directly without lambda notation. To illustrate, consider the following:
void Main()
{
var words = new List<string>()
{
"therapists",
"s words",
"slang",
"mustache",
"sean connery"
};
var s_words = words.Where(w => w.StartsWith("s" ,StringComparison.CurrentCultureIgnoreCase));
foreach(var word in s_words)
{
Console.WriteLine(word);
}
}
It works, but it’s a rather long lambda, which is why I broke it out of the for loop. Let’s put that logic into a function, getting this:
void Main()
{
var words = new List<string>()
{
"therapists",
"s words",
"slang",
"mustache",
"sean connery"
};
foreach(var word in words.Where(w => SWords(w)))
{
Console.WriteLine(word);
}
}
public bool SWords(string word)
{
return word.StartsWith("s" ,StringComparison.CurrentCultureIgnoreCase);
}
That’s better. And you might find yourself using your test in other places in the code, so it’s useful to have the function. What I found out recently, is that you can go one step further:
void Main()
{
var words = new List<string>()
{
"therapists",
"s words",
"slang",
"mustache",
"sean connery"
};
foreach(var word in words.Where(SWords))
{
Console.WriteLine(word);
}
}
public bool SWords(string word)
{
return word.StartsWith("s" ,StringComparison.CurrentCultureIgnoreCase);
}
In this example, the savings may not look drastic. But for several chained methods you can gain a lot of brevity and clarity.
Feb/100
MSpec “Hello World” Sample
I’ve been working with MSpec lately. It’s a BDD framework for .Net. Here’s a hello world type example that uses Trace Writing to show a little bit about what it does.
using System;
using Machine.Specifications;
using System.Diagnostics;
namespace UnitedIndustrial.DataImportConcerns
{
[Subject("Sample")]
public class SampleConcerns
{
static string _myString;
Establish _context = () =>
{
_myString = "value from context";
LocationValueDump("context");
};
Because _of = () =>
{
_myString = "value from because";
LocationValueDump("because");
};
It _shouldKnow1And1Equals2 = () =>
{
(1 + 1).ShouldEqual(2);
LocationValueDump("_shouldKnow1And1Equals2");
};
It _shouldKnowANewStringIsNotNull = () =>
{
"hello".ShouldNotBeNull();
LocationValueDump("_shouldKnowANewStringIsNotNull");
};
private static void LocationValueDump(string location)
{
Trace.WriteLine(
String.Format("in {0} _myString is {1}",
location, _myString));
}
}
}
Nov/090
Emacs on Windows Over SSH with Putty Tools
With a little help from StackOverflow, I got emacs over ssh working on windows. This is trivial on mac/linux, but can be a challenge on windows. dired mode works too!
To summarize:
1. Download putty installer with all the tools.
2. Put putty install in the path
3. Generate a key with PuttyGen
4. Copy public key to your server
5. Append public key to your .ssh/authorized_keys file (be sure to remove extraneous puttygen text, just get the key)
6. Load up pageant and add your private key (this can be automated on windows boot)
7. Add the following to your .emacs config
(require ‘tramp)
(setq default-tramp-method “plink”)
As long as pageant is running with your key, you can edit your remote files using the format ssh://user@server:path/to/file
Nov/091
Asp.Net MVC Gotchas
I’ve been working on a project with the Asp.Net MVC framework lately, and thought I’d write up some lessons learned. I’m using Professional ASP.NET MVC 1.0 as a guide. The first chapter is available online for free as a pdf.
One: Be absolutely certain you are avoiding web forms web controls. I took the master page from a demo version of the application that was in web forms, and it still had a couple of form tags and server controls. This causes the dreaded “Viewstate Mac validation” failure, but only in certain cases, like re-rendering a view after a post.
Two: RenderPartial is a great way to keep things clean and simple. Learn it right away.
Three: Do NOT name a partial and a page in the same folder the same thing. RenderPartial will look for pages and user controls, so you can get some surprising behaviour (pages before user controls). Worse yet, this got me in an infinite loop.
Four: This is sort of a linq to sql hint. Say you have a form for the parent when you already now the child. For example, you are adding a line item and you know the product. You can hook on the product, to the line item by id, but not the actual reference. That reference would be nice to allow your form to display information about the product (say product.name). This is a great problem to solve with ViewModels. Create a custom model for your view, where display information is stored in properties.
I know I’ve bashed Linq to SQL in another post on here. It has it’s issues. But for rapid development, it’s pretty handy.
Oct/091
Community and Interviewing
I’ve been sitting in on more interviews at work with my new role. I’m surprised by the lack of answers when I ask how developers keep up to date with technology, and where they seek answers. “Google”, many reply. “Books”.
Maybe I’m asking too much, but here are some creative answers that would imply a real interest in career development.
- User Groups
- Social Networks (Twitter, Linked In, Forums)
- chat / irc
For online answers, I’m surprised at how many developers don’t yet know about StackOverflow. It is simply the best place for answers, on virtually any language or platform.
This criticism is particularly true of unemployed developers. Any unemployed developer should be working on a personal project, or contributing to an open source one. It keeps skills sharp and opens networking opportunities. And if you were doing this kind of development at home, you would naturally seek out some of the above answers.
Finally, not being prepared to answer this question can cause other issues. If you don’t interact with other developers and learn, then do you have flexibility issues? One candidate answered a question, and in order to back it up claimed that he knew the top people in the related field of technology, and they had all given him that advice. That may be true, but he had previously stated in the community question that he did not attend user groups or developer conventions at all. If so, then how do you meet the top authorities in that area of programming? I don’t assume he or she was lying, and I could have pushed with follow up questions. But the credibility would not have been a question if he was more active in the community.
Don’t misread this. I understand that different people have unique comfort zones for engaging other programmers. And life can get in the way of going to in-person meetings. As a new father, I really get that. And in a rough economy, travelling to conventions can be tough. I’m not saying that there is a right answer, but at least have an answer.
Oct/090
New Role
I’ve been promoted at work to what is currently being called “Manager of Development Methodology and Process”. It’s a great feeling, because I had been doing a lot of extra work in the department and this makes it more official. Also, rather than trying to push new technologies and initiatives, I have an easier time doing so with a little authority.
It’s not an org chart kind of role. I will not be managing any emloyees, and I prefer it that way. Basically it boils down to the following:
- Manage projects that are outsourced completely to the company
- Set technology and process standards, and measure compliance. All standards are only in the abscense of client standars
- Evaluate technical abilities of potential new hires
I already see very specific challenges. I’ll be working with clients to merge their standards with ours and present them concisely to our developers. Measuring compliance will be difficult at times. And while screening technical ability can be challenging, I look forward to it. I recently read Joel Spolsky’s Smart and Gets Things Done
and it has some great ideas.
Finally, I think a large part of the challenge will be balancing technologies that are new and exciting with the existing expertise and experience of the staff.
Jul/090
Integrating Internet Services on Your Site (Or Mini-Cloud Web Sites)
Something I’ve been increasingly fascinated with over the last year is the reuse of free internet services that I have. On my site, I have widgets that feed the last 4 posts from this wordpress blog, and my twitter feed. I also have an ajax live bing search box that will return results from my site, or the web in general. Most recently, I setup my default page to show a random image from a set I have on flickr.
Separately, over on linkedin, any blog posts that I tag “linkedin” show up. And I have a google docs presentation integrated into my profile. All of these integrations were free, and pretty easy to do. Many of them involved copy and paste html. A few involved calling api’s, but they were easy to use and well documented.
And I haven’t touched the tip of the iceberg. Last.fm, Delicious, google calendar, and more all provide feeds and widgets that I’m not yet taking advantage of. And there are creative ways to use these differently than intended.
It’s easy to write a twitter widget, that doesn’t appear to be twitter. Drop it on a client’s site, and they can add news blurbs via twitter or sms. It’s a small piece of content management that couldn’t be easier to integrate.
Jul/090
ASP.Net Comments
Here’s a strange one I’ve never run into. I was recently helping a developer figure out why some controls were not binding and showing on the page.
I noticed they were using html style comments in their page around some controls instead of .net style comments. This was the problem. Server side code still runs, and controls still render inside of html comments. No harm in that, right? The controls can’t do anything because they are in comments.
Wrong. These were validation controls. They added javascript events to the page. Those javascript events expected dom elements that didn’t exist.
You can argue that this shouldn’t have come up in the first place, as I think most ASP.Net developers know to use server side comments, however, I thought the result was interesting.
Also, it was interesting to note that if you had some boiler plate comment you wanted in your source, you could use asp to deliver the content…
<!--
#both of these are valid
#response.write version
<%=Resources.Resource1.Copyright%>
#or server control
<asp:Literal Id="ltlCopyright" Text='<$Resource: Resources1, Copyright >'
Runat="server" />
-->



