Bioshock

I downloaded Bioshock from Steam for a measely $19.95 a while back.  If you’re one of the six or seven people who haven’t played it already, it’s a very cool game.  Definitely a five… out of five.  It’s not a very long-lasting game, though, as is typical of the gaming industry these days.  And it’s not the hardest game in the world, either.  I played it on all three difficulty settings, and on the hardest setting, it started out kind of hard, but I got used to it after a while and it became rather easy to beat down the splicers.  On the easy setting, it was ridiculously easy.  Fun to whack everyone with the wrench, though.

When It’s Okay To Use C# 3.0 var

I ragged a little bit on “var” a while ago, but there are some cases where I think it’s a handy shortcut.  Those being when it’s abundantly clear what the underlying type is, like when creating new objects.  For example, it makes perfect sense to change this:

CustomerDataContext db = new CustomerDataContext();

To this:

var db1 = new CustomerDataContext();

It’s clear to me at a glance what’s going on when I’m reading it, and there’s no needless redundancy.

(By the way, you can’t use “var” in a class-level declaration, only in local declarations.  I don’t know why, either… it feels like a completely arbitrary restriction.)

The problem area for “var” is when you use it with method calls, ie. changing something like this:

FileInfo[] dirs = GetFiles();

To this:

var dirs1 = GetFiles();

You suspect it’s returning a collection of files by looking at the method name, but you don’t know for sure and you don’t know exactly what kind of collection.  It could be an Array or a List<T> or an ArrayList or an IEnumerable or an IEnumerable<T>.  In that situation, I would probably discourage people — especially less experienced people* who tend not to write descriptive variable and method names – from using a var shortcut.  (You can always hover the mouse over GetFiles to see the method signature and return type, but that’s an extra step.)

I also wonder about using var as the return type for LINQ queries.  Sure, we all know from experience that you’re getting back an IEnumerable<T> collection of one type or another, but what about someone who’s just out of college*, looking at a LINQ query for the first time?  That person probably won’t understand the LINQ query syntax or “var” (since I doubt it is taught in any school), but I would think they’d know what to do with an IEnumerable.

Maybe I’m being a little too alarmist there.  I guess using LINQ queries at all should imply a certain level of knowledge to work with the source code.

* The older I get, the more I find that a lot of my code crafting efforts go toward making the codebase ready for less experienced programmers to work with.  Which basically means:  Simplify, simplify, simplify.

Corporate GMail

I didn’t realize this, but you can actually brand Google apps.  The place where my wife works got some new computers, and their new web-mail system is actually GMail with a different logo up in the corner, which they get to through their own domain name.  After a little digging, I found that it’s “corporate GMail,” apparently a component of Google Apps.

So now, not only can your boss read your work emails, but all the kids at Google can read them, too.

LINQ to SQL and Entity Framework Classes

I’ve been starting to work a little bit with LINQ to SQL and now the ADO.NET Entity Framework* (to implement ORM** for UvMoney, my home banking system).  There’s something that troubles me about both frameworks.  Both require you to build a “model” of your database with a designer, from which strongly-typed classes are generated, which you can then use in your LINQ queries.

This is quite similar to the designer-built strongly-typed DataSets from .NET 1.1 and 2.0, which I never enjoy using very much because there always seems to be something just a little bit wrong with them.  With ADO.NET, my preference has always been to build my own ORM classes.  Which makes me wonder:  How do I take advantage of LINQ to SQL and the Entity Framework while still building my own classes?  I suppose I should give up on that line of thought before I start, because implementing my own ORM classes would essentially be duplicating what LINQ to SQL and the Entity Framework do.

* According to rumor, Microsoft will be concentrating on the Entity Framework going forward, so there is little point in spending much more effort learning LINQ to SQL classes.

** ORM is Object-Relational Mapping, which is a fancy term for the process of sticking database values into object properties and vice versa.

CookieContainer, HttpWebRequest and Secure Cookies

I ran across a troublesome problem that took several evenings to debug.  I couldn’t find a solution with Google so maybe somebody else will benefit from this.

My app sent an HTTP GET to a secure site using an HttpWebRequest and a CookieContainer (to capture the site’s cookies).  Two cookies were returned in the response header, one of which was marked by the server as “Secure.”  Both cookies were in the CookieContainer (the Count was 2 and they were both returned by GetCookies(url)).  However, I subsequently sent an HTTP POST using the same CookieContainer to the same site (still https), but HttpWebRequest did not place the secure cookie into the POST header.

This might be by design, a misunderstanding on my part, or a problem with the site’s cookies, but in any case it was not working.  To get around the problem, I had to manually add the missing cookie to the CookieContainer before calling HttpWebRequest.GetResponse.  Only after doing that was the cookie added to the outgoing request header and subsequently sent to the server.  Something like this:

CookieContainer cookieContainer = new CookieContainer();

 

HttpWebResponse response = DoHttpGet( cookieContainer );

 

// Manually add the missing cookie.

Cookie cookie = new Cookie( “Name”, response.Cookies[“Name”].Value );

cookie.Secure = true;

cookie.Domain = “www.domain.com”;

cookieContainer.Add( cookie );

 

DoHttpPost( cookieContainer );

Alternatively, I found that this worked also:

HttpWebResponse response = DoHttpGet( cookieContainer );

// Alternative to the above.

cookieContainer.Add( new Uri( “https://www.domain.com” ), response.Cookies );

DoHttpPost( cookieContainer );

However, a plain cookieContainer.Add( response.Cookies) — a frequently-referenced Google solution – did not work.

HttpWebResponse response = DoHttpGet( cookieContainer );

// This did not work.

cookieContainer.Add( response.Cookies );

DoHttpPost( cookieContainer );

By the way, Fiddler was invaluable in debugging this issue.

Less Mergers, Less Bailouts

I’ve been thinking about this bailout thing.  I’m no economist, but it seems to me that instead of the government bailing out large companies that fail, the government should instead work to prevent companies from getting so large that their failure would impact the nation’s economy.  So I would suggest that whichever government agency is responsible for regulating corporations start cracking down and don’t allow big corporations to gobble up smaller corporations anymore.  (You’d think that’s what anti-trust laws would be for.)

Fed up with SyncToy

I’ve been trying to use Microsoft’s SyncToy for my directory synchronization tasks, and I’m just about fed up with it.  For reasons I can’t fathom, it routinely “forgets” what to synchronize.  I frequently synchronize to and from a USB stick, and I find that SyncToy usually wants to copy far more files than have actually been changed.  Like I’ll make a couple of changes to some files on the stick, and then for some reason SyncToy thinks it needs to re-copy all kinds of files from both the stick and the computer.  So I end up having to copy files manually from one side to another.  It’s a bit aggravating.

I guess I’m going back to TreeComp, a freeware program I first used in about 2003.  It looks godawful but still works better and faster than anything I’ve seen.  We’ll see how it goes on Vista.

Disabling Pluralization in Visual Studio

I noticed with some concern that the Visual Studio 2008 designer puts an “s” on the end of table names when it builds LINQ to SQL classes.  If you want to disable that, open Tools/Options, go to the Database Tools O/R Designer page, and change “Pluralization of names” option to false.

Note that if you have already generated pluralized classes, you have to force the designer to regenerate the classes by actually changing the names to some temporary value, build, then change them to what you really want and rebuild again.  Otherwise, if you leave the name the same, it assumes it doesn’t need to regenerate the classes.

Azure and C# 4.0 at PDC

Most of the buzz from Microsoft’s PDC is about Azure and C# 4.0.  I don’t usually pay too much attention to bleeding edge Microsoft technology until it might actually be used in real-life situations, but I thought I’d break from tradition and peek at these two shiny new things.

Windows Azure Services

There isn’t a lot of concrete information available on Azure yet, so there isn’t much to say about it.  Conceptually, it’s a specialized web hosting service from Microsoft for your ASP.NET web apps.  It’s specialized because it’s designed for the large numbers of processors and mass storage requirements needed for large-scale enterprise applications (large-scale as in, for example, Amazon or Facebook or Google).

From a programmer perspective, I gather that the main difference from a regular web app is that the details of the OS and hardware your application is running on is abstracted away from you.  For example, instead of your web app reading and writing files with the System.IO namespace, you’ll use SQL Services to read and write “blobs.”

I personally can’t think of any applications I might use Azure for.  (Do we really need another Amazon?)  It would have to be something mandated by a pointy-haired boss-type.

As for the “cloud computing” part of it; well, I’ve said before that I don’t much care for cloud computing.  Besides all the privacy and data loss concerns, the bottom line is that “the cloud” is meant to make life easier for developers, not users.  As a user, I might store a copy of something in the cloud for convenience, but I would never use the cloud as my main work area.

C# 4.0

Recall that C# 3.0 gave us query syntax, extension methods and the ”var” shortcut, and tried to change us to functional programmers with lambdas.  With the possible exception of LINQ, I personally have not yet encountered a situation where I thought any of those things would make my code better.

So what can we expect from C# 4.0?  Among other things, dynamic types.  That’s right, in the continuing effort to put back all the bad things we didn’t like about VBScript and C, the variant and void* is back.  For all those people who simply can’t decide how to declare your variables, you can now circumvent all pre-planning and simply write, “void myfunction( dynamic myparam )”.  Is that parameter a string?  Is it an int?  Is it a custom object?  Is it an array?  Who knows?  Guessing is half the fun!

I kid of course.  You could assume it’s an object of some kind.

I’m sure there are situations where it is more convenient to use “dynamic” than an interface.  (Just like there were plenty of occasions to use void* types and variants.)  Offhand, I can’t think of any, but I’m sure they’re out there somewhere.

The problem I see with dynamics is that, as with any “advanced” language feature that circumvents the “KISS” principle, it leaves the door wide open for the inexperienced to abuse it unmercifully.  When you go too far down that road, you end up with Perl, a language almost entirely composed of symbols.

It looks like almost all of the new C# 4.0 features were designed to improve COM Interop, which is something I don’t have much use for.  I have exactly one COM component project, and if I ever need to use it again, I plan to upgrade it to managed C++.

I do like the idea of finally having optional parameters, though.  That’s something I’d use (sparingly!), as long as it compiles down to the .NET 2.0 CLR.

Post-Election Musing

I have many post-election thoughts.

  • McCain’s concession speech was very classy, and went a long way toward repairing his tarnished reputation in my eyes.  I feel like he was devoured by the Republican Party Campaign Machine, and for whatever reason wasn’t able to stick to his previously-established principles.  (Prior to the RNC convention, I could still have voted for him, but his selection of Gov. Palin as a running mate was just too much of an affront.)
  • I’m expecting a competant but not extraordinary administration from Obama.  I don’t expect him to be able to fulfill many of his campaign promises (he started downplaying expectations even in his victory speech), and I anticipate that by the time he leaves office he will have disappointed many of the people who had high hopes for him.  (I don’t feel like I’m going out on a limb with that prediction — what president hasn’t?)  I hope I’m wrong, but I’m sure you can understand how a person could be cynical.
  • We arrived at our polling place about 5:30 AM, and there were about 20 people ahead of us.  We finished voting about 6:15 AM, and there had to have been 200 or more people waiting in line by then.  When I drove by there again after work, it was deserted.  Doh!
  • (By the way, just for the record, I voted for Bob Barr — but that’s a subject for another post, if I ever get around to it, which I probably won’t, so the short version is that I’m pro-third-party right now.)
  • I still find it very disturbing that I don’t get any kind of receipt or anything after I press the big red “vote” button on the touchscreen machines.  Also, it creeps me out to think about everybody else’s germs crawling all over those touchscreens.
  • I’m your basic WASP, born after the civil rights era, so I can only appreciate the racial significance of this election from an academic standpoint.  Like Stan on that one South Park, I “don’t get it.”  I think Obama still has a lot of hard work to do, though.  I don’t think it’s enough to just be the first black president — he has to be the first black president and do a good job, or it might be for naught.  (Fortunately, Bush has set the bar pretty low for him.)
  • As I type this, it looks like the Democrats will fall short of a super-majority in the Senate.  I have mixed feelings about that.  I’m leaning toward thinking it’s good that they won’t have free reign, but I’m also mindful that Congress hasn’t gotten much of anything done in, like, forever.  But that could be a good thing:  I’m not sure we want politicians “getting things done.”
  • I found it pretty hilarious to watch all the newspeople trying to make it sound like the outcome of the election was in doubt so they could keep viewers tuned in to see all their fancy flying graphs and maps.
  • I’m very curious to see how The Daily Show and The Colbert Report handle the new administration.
  • Driving up 95 North, I saw a brilliant ad on the fancy-schmancy electronic billboard next to Phillip Morris, which I’ve attempted to recreate below. I couldn’t read who sponsored or created it.  I’d like to think that we live in an America like this, but again, it’s really hard not to be cynical.