Passion * Technology * Ruthless Competence

Thursday, February 07, 2008

Morning Coffee 144

  • I finished Mass Effect last night. I definitely need to play thru that one again, though I'll probably wait until the new Bring Down the Sky DLC ships next month.
  • Caps won again last night, improving to 20-10-4 since changing coaches at Thanksgiving. They're now at 57 points, taking the lead in the SE division with a full game on Carolina, Atlanta and Florida. Still a ways to go - 27 games left in the regular season - and things are far from "sewn up" but we're a damn sight better off than we were in November.
  • Speaking of a horserace, looks like Clinton and Obama are in one after Super Tuesday. Their estimated delegate counts are basically tied. On the other side of the aisle, McCain opened up what is probably insurmountable lead - even though he has the right-wing media stars and Christian leaders against him. Money quote of the day:

“The real story of the night, when you look at their rallies and their turn-out numbers, is that the Dems have two strong candidates either of whom could lead a united party to victory. Forget the gaseous platitudes: in Dem terms, their choice on Super Duper Tuesday was deciding which candidate was Super Duper and which was merely Super. Over on the GOP side, it was a choice between Weak & Divisive or Weaker & Unacceptable. Doesn’t bode well for November.”
- Mark Steyn, National Review 
(via Carpetbagger Report, lest you think I regularly read National Review)

  • Charlie Calvert is starting a new series on the future of C#. First up: Dynamic Lookup. Probably most interesting is the news that the DLR "will be the infrastructure on which the C# team implements dynamic lookup". Does this mean C# will target the DLR? Sure sounds like it. I think it's a good addition, but I'm not a fan of the proposed syntax. (via Bitter Coder)
  • Brian McNamara saw me present @ LangNET and sent me a link to his blog. He's building up a monadic parser combinator library in C# 3.0. This is basically the same concept that FParsec implements, though C#'s syntax is much less attractive than F#'s for this kind of code. However, Brian does a very good job explaining why monadic parser combinators are useful and making the idea accessible to the C# programmer (i.e. you don't have to learn F# or Haskell to understand what he's talking about). He also points to Luke Hoban's C# 3.0 monadic parser implementation.
Posted By Harry Pierson at 10:05 AM Pacific Standard Time

Thursday, August 09, 2007

Another InitImportantThing Approach

I thought of another approach to the InitImportantThing problem that I blogged about yesterday. I think it's a bit harder to code, but it's certainly explicit and avoids the magic method that Jon dislikes so much.

The crux of the problem is that ServiceHostBase needs a valid ServiceDescription in order to operate. The WCF team chose to provide said description to ServiceHostBase via the abstract CreateDescription method. But as we saw, ServiceHostBase can't call CreateDescription from it's own constructor. So instead, derived classes are forced to call InitializeDescription in their own constructor. Since that call isn't enforced by the compiler, it's easy to forget to include it. Since the exception that gets thrown doesn't really tell you what went wrong, it's easy to spend hours trying to figure it out.

So here's a better approach: since the ServiceHostBase needs a valid ServiceDescription in order to operate, why not pass it in as a constructor parameter?

ServiceHostBase has a protected constructor with no parameters. But since it needs you to call InitializeDescription in your derived class constructor, it really needs the ServiceDescription, a collection of ContractDescriptions (also returned from CreateDescription) and a collection of base addresses (passed into InitalizeDescription). If these were parameters on ServiceHostBase's constructor, it could validate that information directly, without needing abstract or magic methods.

The one problem with this approach is that the creation of a ServiceDescription is non-trivial. ServiceHost's implementation of CreateDescription generates the ServiceDescription by reflecting over the service type. You still need that code, but now you would call it from the base constructor initializer instead. That means it has to be a static method, but otherwise it would work just fine. Here's yesterday's code, updated for this approach:

public abstract class Base
{
    public Base(string importantThing)
    {
        if (string.IsNullOrEmpty(importantThing))
            throw new Exception();

        _importantThing = importantThing;

    }

    private string _importantThing;

    public string ImportantThing 
    { 
        get { return _importantThing; } 
    }
}

public class Derived : Base
{
    private object _data;

    public Derived(DateTime dt) : base(CreateImportantThing(dt))
    {
        _data = dt;
    }

    private static string CreateImportantThing(DateTime dt)
    {
        //this is obviously trivial, but could be much
        //more complicated if need be
        return dt.ToLongDateString();
    }
}

This seems like the best approach to me. You remove the un-obvious magic method call requirement when deriving your own service host while still enforcing the data consistency check in the base class during construction. Best of both worlds, right?

So I wonder why the WCF team didn't do it this way? 

Posted By Harry Pierson at 12:49 PM Pacific Daylight Time

Friday, March 09, 2007

Morning Coffee 42

Ever since I got back from vacation, it's been all about the Morning Coffee. I'm happy to be getting a daily post out, but I haven't written anything deep in several weeks now. My one non-MC post in the past two weeks was The Virtuous Cycle of Virtual Platforms which frankly I wrote over a year ago for internal usage and adapted for my blog after reading Dare's post.

One of the reasons for my lack of "deep" posting recently is post vacation re-engagement. Also, things at work that I can't blog about (yet) have been taking my attention. But I worry that this daily MC post is causing me to focus on "shallow" blog topics. Since I'm trying to average a post per day, that means at least two non-MC posts every week. Of course, more than two non-MC posts a week would be just fine.

  • On the XNA Team Blog, Michael Klucher announces the XNA Game Studio Express Update is coming in April. Among the new features are Vista compatibility, 3D audio, bitmap fonts, game icons and most interesting the sharing of compiled XNA games. Currently, the only way to share something you build with XNA with the community is by sharing the source code, which is less than optimal. For more, check out the XNA GSE Overview presentation by Mitch Walker from GDC.
  • Speaking of gaming consoles, Sony's "big" announcement is a Second Life clone? Kotaku thinks "this is going to be one of those features that people didn't realize that wanted until they get it." Personally, I doubt that very much, but what do I know about game consoles? I just play, man.
  • Jafar Husain suggests a way to do Ruby symbols in C# 3.0. Sort of. He defines an extension method that returns the name of the property defined in a lambda function. On the plus side, it's strongly typed. On the minus side, "this.GetPropertySymbol(o => o.Name)" isn't as easy to type as ":Name". (via DotNetKicks)
  • While pseudo-symbol support is fairly verbose, Scott Guthrie goes thru some of the new language features for terser syntax: automatic properties, object initializes and collection initializes. While I like object and collection initializes, I'm not really sold on automatic properties. Personally, I like the VS prop snippet approach, where you automate the creation of the property once time when it's authored rather than leaving the shortcut syntax in the code in perpetuity.
Posted By Harry Pierson at 11:05 AM Pacific Standard Time

Monday, August 21, 2006

Language Features I Wish C# Had - Symbols

Ruby's symbols are often talked about in terms of efficiency - taking up less memory and executing faster. While these are both laudable goals, symbols are more than just performance improvers. The ability to name things is valuable semantically. Take a look thru p&p's Composite UI AppBlock and you'll see strings used as names for things all over the place. It's great for loose coupling, you see. But how do you tell the difference between a string used as a name and a string used for some other reason like user input? You can't.

Rails makes extensive use of symbols - anyone who has Rolled on Rails has seen "scaffold :recipe". That's just the tip of the iceberg. Rails uses symbols extensively across both ActionPack and ActiveRecord (and probably others I'm not familiar with). It's a great approach, but one that's unique to Ruby as far as I'm aware.

Posted By Harry Pierson at 11:05 PM Pacific Daylight Time

Language Features I Wish C# Had - Tuples

Several languages, such as Python, have the concept of a Tuple built into the lanugage. One of things it's used for in Python is multiple return values. So you can call "return x,y" to return two values. Of course, C# can only return one. If you need to return more values, you have to define out parameters.

LINQ / C# 3.0 / VB 9 support the idea of anonymous types, which is similar to a tuple. The big difference is that, because they're anonymous, they can't leave the scope they're defined in. In other words, they're great within a function, but if you want to pass them out of your function type-safely, you have to define a non-anonymous type for them.

Interestingly enough, F# supports tuples, though it a bit of a hack. Since the CLR doesn't support tuples, F# basically defines different Tuple classes for up to seven tuple parameters (i.e. Tuple<t1,t2,t3,t4,t5,t6,t7>), For .NET 1.x, it's even worse - they have to define different type names (Tuple2, Tuple3, etc). Ugh.

Update - Robert Pickering pointed out that F#'s tuple implementation is entirely transparent inside of F#. He's right - I was writing from the perspective of a C# developer using F#'s implementation of tuples. Maybe I need to be looking closer at F#?

Posted By Harry Pierson at 11:01 PM Pacific Daylight Time
DevHawk
World Tour 2008
DevDays 2008

Change Congress
Recent Bookmarks
Tags .NET Framework (2) ADO.NET (5) Agile (7) AJAX (3) Architecture (282) Guidance (6) Interop (2) Modelling (61) Patterns (7) Process (4) SOA (93) Web Services (5) ASP.NET (18) Battlestar Galactica (3) BI (2) BizTalk (4) Blogging (113) dasBlog (11) Podcasting (4) BPM (1) C# (5) C++ (3) Capitals (5) CardSpace (3) CLR (2) College Football (10) Comedy Central (1) Community (81) Concurrency (6) Consumer Electronics (1) Database (12) Dependency Injection (2) Development (115) C Plus Plus (1) Embedded (5) Lanugages (36) Media (2) P2P (11) Rotor (1) SharePoint (6) SOP (3) DIY (1) DLR (8) Domain Specific Languages (13) Durable Messaging (5) Dynamic Languages (9) Dynamic Silverlight (1) Education (3) Enterprise 2.0 (1) Entertainment (14) ETech (15) F# (38) Functional Programming (11) Game Development (2) Guidance Automation (3) Hardware (8) HawkEye (3) Hockey (29) Home Electronics (1) Home Network (4) Humor (5) IASA (1) Idempotence (3) infrastructure (5) Instrumentation (4) Integration (2) IronPython (14) IronRuby (3) Java (2) Job (3) LINQ (19) Lost (1) Master Data Management (1) Media 2.0 (6) Microsoft (27) MIX06 (2) Mobile Phone (1) Morning Coffee (165) Object Oriented (4) Office (5) Open Source (4) Open Space (2) Operations (3) Other (135) Art (1) Books (1) Family (30) Games (17) General Geekery (25) Home Theater (1) Movies (22) Music (20) Politics (3) Society (1) Sports (37) Working at MSFT (15) Parsing Expression Grammar (15) patterns & practices (2) Politics (39) PowerPoint (2) PowerShell (28) Presentation (4) Projects (1) HawkWiki (1) Python (3) Quote of the Day (4) Refactoring (1) Research (2) REST (18) Reuse (5) Robotics (1) Rome (5) Ruby (23) Sci-Fi (2) Scripting (4) Security (3) Service Broker (14) SharePoint (2) Silverlight (15) Social Software (1) Software + Services (2) Software Factories (11) Software Industry (1) Spark (1) SQL Server (2) Stephen Colbert (1) TechEd (7) TechEd06 (1) TechRec League (1) Television (6) Travel (5) Unified Client (1) Unit Testing (3) UX (1) Virtual PC (2) Visual Studio (19) Volta (2) Washington Capitals (33) WCF (31) Web 2.0 (64) Web Services (5) WF (20) Windows Live (21) Xbox (1) Xbox 360 (51) XML (7) XNA (13)
Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion.