| Richard's profile red hot place BlogLists | Help |
|
February 27 Implicit Typing: To var or not to var?Since the beginning of time, religious wars have been fought over differences in religion and beliefs. I think this is one such war. C# 3.0 introduced the concept of implicit typing back in 2007 with the keyword var. Since then, you are bound to find people vehemently opposed (and disgusted) by the concept, and others that embrace it to the point of no return. I’m going to try to take an impartial look of implicit typing here (but you may conclude that I have failed). First off, the facts. MSDN’s “Implicitly Typed Local Variables” has a very concise explanation, but let’s try to distill it even further.
When a member of my team, CS, started using the keyword var, I despised it. Though the concept is so simple, it’s actually a fairly large paradigm shift. I thought it made code less readable, and also broke my IDE’s autocompletion feature. At first he suggested that we only use it for LINQ queries, initializations with constructors, and casting statements. Trying to keep an open mind, I gave it a shot, and it didn’t take long before I found out that I actually liked it. Code was looking nicer, cleaner, and simpler. Talk about heading over to the dark side!
Which looks better? (In case it’s not obvious, I think the latter is leaps and bounds better.) Or:
At this point though, CS had moved on to using it practically everywhere (except for value types)! What the heck!? It is a slippery slope. I acknowledge that this is probably not as obvious:
... but it’s actually way more readable. Anyway, you can probably tell that I slid down that slippery slope. Now I, too, use it when I can (except for value types, though I would not be surprised before I start using it literally everywhere). There has been backlash though, against this usage. Why? By far the largest argument against the usage of var that I’ve seen is that it’s not always clear from looking at the declaration of the variable what the type really is (though this is certainly not true in the previous two examples):
Actually I don’t use VS either (I use a combination of SourceInsight and vim, neither of which grok var), yet I like var a lot. Why? At the end of the day, I think that argument comes down to the naming of local variables. When you look at a variable being used in code, you are usually looking at its usage instead of its declaration. I find that naming the variable with a descriptive name often helps much more than the declaration statement far above, because you don’t need to search for the declaration at all! For instance:
What the heck is “s”? (Yes, sometimes I will shorten it to “sb” too.) It has been brought up many times before that the benefit of “better naming for local variables” “really means it compels developers to use longer Hungarian style variable names.” –Carnage4Life. Personally I think that is a good thing to follow in general; I don’t find it adding to the “noise” in the code. One other benefit of var that I don’t see purported too often is that when you change a type, property, or return type of a method, you don’t have to go change every usage of that type as well. Take, for instance:
The second variable, value, would be implicitly typed to be an ‘int’ in this scenario. But if I were to change ‘Numerator’ to 9.0 instead of 9, value would be typed as a float, without me having to change that line. Obviously this is somewhat of a contrived scenario, but one could easily imagine changing a property in a class (or a return type of a method) and have all the callers of that property to “just work.” I can’t imagine that the debate to var or not to var will ever end, but I for one, love it. After having used it dutifully for several months, I cringe at the thought of typing out types—it’s so 20th century! I’m completely hooked. Let’s end off with a comment thread from ReSharper:
It’s certainly interesting how something so seemingly trivial could incite such passionate debates among people. February 20 OEMCP vs. ACPOne of the trepidations I had when starting to work on client software three years ago was dealing with globalization and localization. I had heard of horror stories of how much busy work localization involved (they have teams that deal with that issue alone), and it was a black box for me. (Yes, I realize that they’re just strings in resource files.) Fortunately enough, since I have been working on a client platform, instead of the UX on top of the platform, I never had to deal with localization at all. In fact, I was pleasantly surprised when I first heard that sync’ing files like 好嗎.txt actually worked. Effortless! My naïvité lasted up until a month ago, when I investigated a bug report from a customer. I sat on the bug for a while, as I was a little perplexed by the whole report. It was most certainly some sort of localization issue, and it took some head-banging before I figured it out. The details aren’t particularly important here, so let me just get to the gist of the bug. At the command prompt: (Yes, the command prompt. Does that still surprise you? Seven of my 10 previous “code” posts have to do with the command prompt.)
I took French in high school. That’s a cedilla under the ‘c’. Anyway, there’s nothing unexpected here. Echo does its job as asked. Now for some redirection:
Again, nothing unexpected here. Yawn. But wait! (There’s more!)
Notepad will open up. And what does it show?
What the heck?! Blink. Twice. Huh? What’s going on? I did a bunch of digging and found all sorts of interesting information online about code pages. From Wikipedia:
It turns out that on an EN-US operating system, the OEM code page is CP437, whereas the default ANSI code page (ACP) is CP1252. Apparently the command prompt uses a different code page than do GUI programs, so this must be an encoding issue. If you look at the file in a hex editor, you’ll see that the character ‘ç’ is encoded as 0xe7, as expected from the OEMCP: 87 = U+00E7 : LATIN SMALL LETTER C WITH CEDILLA. But in the ACP, 0xe7 is ‘‡’: 87 = U+2021 : DOUBLE DAGGER! So does that mean that the issue goes away if you change the code page the command prompt is using to the one UI apps use?
Check that out! Ah, oui! Bien sûr! Raymond Chen has a bit more on the details and the historical reasons behind the schism. Thanks to Michael Kaplan for a series of excellent posts on the subject. I still know next to nothing about localization, but it’s pretty cool when you figure things out, even if those things were discussed ad nauseam years before you even came across it. (And just to wrap this up, the Win32 function GetACP() will allow you to get the current ANSI code page, but that isn’t scriptable unless you wrap it in an exe.) I’m floating the idea of doing a “Bugs of Live Mesh/Framework” series. (Bonus points for anyone that can figure out how this bug relates to Live Mesh. Hint: It’s related to a previous post I made here over a year ago.) I was thinking of covering some of the more ‘interesting’ bugs: why they exist, what fixes were/will be made, what workarounds there are, or even to solicit feedback. What say you? January 20 The Two-to-Three Year ItchDare’s got an interesting post on “Dealing with the Seven Year Itch ...”. In my experience, and in watching many of the people around me, I think the itch comes along once every two to three years (assuming you scratch the itch when it appears). My first itch almost three years ago was delivered prematurely. I can vouch for this:
If you haven’t done #1 by the time you get the itch, or aren’t doing #2 already (actually, you shouldn’t get the itch if you’re doing #2), then #3 is really your only option. I’m at 4.5 years working at MSFT now. The five year mark is when they give you the five-year service award. Assuming I don’t get laid off in the next six months, I should have some more reflective thoughts later. I never would have imagined that I’d still be around here after five years. When I first started, I scoffed at some that had been around for just two years! Crazy. January 18 CrunchieLive Mesh Awarded Best Technology Innovation Crunchie @ Crunchies 2008 Old news, I know, but I finally got a picture of it.
Those logos are printed out on paper (inkjet?) cut out, and glued on. lol. November 12 He’s Alive!I can’t believe it’s been 4 months since I last posted here. Where has the summer gone? In the past seven months since we unveiled Live Mesh our team’s been working furiously to deliver for the Professional Developers Conference 2008. I wasn’t lucky enough to go (*grumble*) but if you missed it there’s a wealth of material online for viewing (I still haven’t had a chance to go through all of them). There are a ton of links off the official blog that are worth looking through.
There are some things I want to write about, but I just haven’t gotten the chance. Hopefully I’ll be a bit more regular again. More later. July 11 Where is my Desktop?
Some people tend to save their documents and files on a separate drive. This allows the flexibility of flattening the Windows/OS partition at any time, without losing any data. This is one reason I have never been a fan of using the %USERPROFILE% folders (eg. C:\Users\<UserName> on Vista or C:\Documents and Settings\<UserName> on XP). It turns out, though, that you can redirect these ‘special’ shell folders easily on Vista, or with the Tweak UI PowerToy for XP (or hacking up the registry). These folders include things like “My Pictures,” “My Music,” “Favorites,” or even your “Desktop” folder. On Vista, for instance, when you right-click the Desktop folder and select Properties, you get the dialog on the right. If you’ve done this, obviously it’s not good enough to assume that your desktop is in %USERPROFILE%\Desktop. You’ve got to open up the registry to find the location of these “User Shell Folders”:
Notes:
In some cases, you may also find that there is a “DesktopDirectory” value as well as a “Desktop” value underneath that key. I cannot eloquently explain the difference, but perhaps you may be able to interpret Wikipedia’s explanation:
I believe that DesktopDirectory is the correct value to use, but sometimes it is not available. Most of the time the two values are equivalent. I bet you someone on the shell team could explain this properly, if he hasn’t already. I’m sure there’s an interesting story behind it. July 04 Extension Methods and LINQI know very little about the new features of C# 3.0 and LINQ. I’ve been playing around with it a little bit and was puzzled by how it all works. This is probably old news for a lot of people, but it’s all new to me. For instance, let’s take the simplest possible query:
Trying to compile, I got this compiler error:
You might get this error if you’re missing some references (as the error message says). I was getting it due to some other external issue. But in trying to fix/investigate the issue, I first tried to figure out what exactly the compiler was doing behind the scenes. The method ‘Where’ isn’t available on IEnumerable or Array, so it seemed like a valid error. What was I missing? I couldn’t find any good information online as to how the compiler actually takes the LINQ syntax and generates code that resembles something more IL-ish. There are plenty of sample queries online, but I couldn’t find any good site that really explains how it all works. So after a bunch of digging I managed to piece together that the compiler actually translates the above query to something like this:
Interesting. That kind of explains where the error message is coming from. But still, I couldn’t for the life of me figure out how the heck this worked. There’s no ‘AsQueryable’ method on Array or IEnumerable, or ‘Where’ on IQueryable either. Where are these methods coming from? It turns out that the compiler further translates the query to something like this:
How the heck?! Where did the Queryable class come from? Apparently there is this new concept of “extension methods” in C#. You can add methods to existing types without creating a new derived type, or recompiling, or modifying the original type. Wow!! Far out! System.Linq has a static class ‘Queryable’ that does exactly this; it extends IEnumerable and IQueryable<T>.
All you have to do is add the directive “using System.Linq;” to take advantage of this functionality. Check out the rules on how to implement a custom extension method. And now it all makes sense. Very cool! May 22 Entertain the Cat
It's pretty cool to see what people can do (or dream of doing) with the things you've built. I've been trolling the Live Mesh forums on and off, and so far, my absolute favourite thread has been one entitled "Entertain the cat" (reproduced here).
I especially love how Bleak Morn's post is the Proposed Answer. May 06 Hello, World!Mike Zintel's got an excellent post on the history of the computing world, entitled "Why Sync?" It's an engaging read. I'm struggling to properly excerpt. Go read the whole thing. May 04 yang on yahoo!I've never followed Yahoo! much, though I just read Jerry Yang's memo to Yahoo! staff. Apparently he's never heard of the 'Shift' key. Does anybody else feel that it's incredibly hard to read? It turns out this is old news, but I just noticed. Yuck! At least Ballmer can write intelligibly, even if he does prance around on stage like a monkey. April 23 ‘Live Mesh’ Unveiled!Finally! In April 2006, I decided to leave my job on Messenger Server to join a completely new start-up team within Microsoft. It was a pretty big change for me, as I went from a well established team with millions of users, to being one of the first three developers on a team with nothing but a dream of what was to be. I couldn’t talk about what the team’s charter was, as it was all under wraps, even internally within the company. My business card had the somewhat cryptic team name. Exactly two years and three weeks later, we are finally launching a Tech Preview and I can finally talk about (some of) the stuff I’ve been working on for the past two years. What a relief! I can’t believe it’s been such a long time. I won’t talk about what I think Live Mesh is yet. You’ll find a great deal of information on the Team Blog, as well as videos on Channel 9 this week (Ray's interview is a good one). I can’t do justice to what Mesh is by excerpting, so please go read our GM Amit’s post. It gives a good idea as to what exactly Mesh is. We’re only launching a Tech Preview at the moment, so there’s a limited number of invites. Hopefully you’re well connected enough to get someone to share with you. If you’re already provisioned in the system, feel free to share stuff with me by inviting me to a “LiveFolder.” I’d love to see what people are using Mesh for! April 16 RevampedSo 2⅔ years after creating this blog on a whim, I have decided that I should finally update the colour theme and layout. Though I liked the red and black contrast with the flame background, I realize that it made for horrible reading on the eyes. (If you missed it, it kind of looked like this, except the fonts were smaller.) It was pretty fugly. I can't say that I really cared, since I could count my audience on one hand, and most of them were reading in a feed reader anyway. I've picked another one of the default Spaces themes that's a little easier on the eyes, and tweaked it ever so slightly. Of course, I've kept the flames as my profile image too. What do you think? I'll probably continue tweaking over the next several weeks (like adding a module or two). I hear rolling thunder. April 09 Date TimeI do many more things than batch scripts all day, and someday (soon) I'll blog about some of that stuff, but for now, this will have to suffice. I had several lines of batch code I created several years ago that created a unique filename from the current date/time with the help of two environment variables:
So the code I had:
gave a nice:
which would allow you to sort files by filename in chronological order. It worked quite well and I had used it for years. Recently though, I started seeing some weird behaviour from some people.
This left me scratching my head for a while, until I asked one of these people to run the following on their machine:
Gosh darn those locale settings! It's not enough to handle just this specific case, as in Canada for instance, dates are written DD/MM/YYYY as opposed to the US's MM/DD/YYYY. So I went online and found all sorts of strange ways to figure out the format of the date. The best way I read of, to get locale was by crawling through the registry. I wasn't particularly motivated to do this, so left it at that. Fortunately this particular script got deprecated in favour of new one I wrote that didn't require date/time uniqueness. Saved! March 21 Progressive DotsI just found out about this (thanks to Live Search!) from a fellow Canadian's blog entry "batch file snippets" and it's got me super excited. Haven't you ever found it annoying that you can't print to a line without a newline (carriage return/line feed) in a batch script? (At least, I didn't know how to.) Well, now you can! For instance:
would give you:
as opposed to a progressive:
So what you actually want is:
Don't tell me that's not cool! How does it work? The '/p' option given to set is asking for user input:
The key is that it prompts for input *on the same line*! And by redirecting the NUL device into it, you get an immediate return. How absolutely clever. I love it! March 18 To Sync, or not to Synch?I tend to nitpick. I have a bit of OCD when it comes to grammar and spelling. I cringe when people use "it's" instead of "its" or "your" instead of "you're".* I find it distasteful that people allow themselves to butcher a language (though I suppose non-native speakers can be given a bye). So it's not surprising that I have fairly strong feelings about the abbreviation for the word "synchronize" (or "synchronise", as you prefer). It really bothers me when people write "synch". I realize that both "sync" and "synch" are commonly accepted abbreviations; however the latter irks me to no end. Why? I read "synch" as rhyming with "lynch". I realize that there are words that end with -ch with a hard 'k' sound (say, "loch"), but how many words end with -ynch (or -inch) with a hard 'k' sound?! If you're going to cut a word in half, at least have the decency to do it so that it follows general conventions of pronunciation. I do also realize that languages are a fluid subject that are influenced by many factors. (I hate the impact texting has had on the language--'IMO'.) Here's an interesting tidbit from The American Heritage Book of English Usage:
[ Interestingly enough, I don't really like using the abbreviation in the present participle as in, like "syncing". I tend to write "sync'ing" to denote the abbreviation, though I recognize that this is definitely my own oddity. ] * NB. Some smart aleck will point out that periods and commas go inside the quotation marks. I, for one, despise this notation, and as this is apparently only a US custom and not generally followed in the UK or Canada, I take the liberty not to do so for my own edification. December 31 Happy New Year!As if on cue, LiveSide's got an interesting New Year's post. Happy New Year! December 12 CabIt!I hesitated about posting this because I was talking to Steve last week about someone that had a Ruby fetish, much like someone else seems to have picked up a Python fetish. I then realized that one could make a case that I have a Batch script fetish, seeing as how I've made three posts with it. I actually really like Perl, but those skills have gotten rusty as it's not installed natively on a Windows machine. That's why I turn to batch scripts sometimes--those scripts will work on all boxes. Something else that's not natively installed in Windows is the ability to zip a file from a command line. (Please correct me if I'm wrong.) I presume there are legal issues with bundling this functionality with the OS. Instead, Windows gives the ability to create CABinets. I couldn't find any good (quick/dirty) documentation on how to make a single CAB from a number of files, while preserving hierarchy (ie. folders). I was eventually forced to wade through the official SDK to figure out how to do this. So here's a simple script I "whipped" up through trial and error (it actually took a lot of time to figure out what options to give MakeCab.exe). Give it a path and it will CAB up everything under that path into a file "Zipped.cab". Admittedly, there are some 'hacks' here, as I think CABs were originally intended to zip up files to span across disks (like floppies).
To extract the cab file, you would use "Extract.exe", also found natively on Windows. Note that if you view the Cab in Windows Explorer, you don't actually see hierarchy like you do with zipped files. That threw me off for a bit; weird behaviour. Using "Extract.exe" would deflate the file into its hierarchy, or you can use Explorer to unzip specific files. Notes:
Comments welcome. October 24 Fake Steve on the Borg CampusIf you follow the Secret Diary of Steve Jobs, by Fake Steve Jobs (aka Dan Lyons), you'll already know that FSJ was on Microsoft campus yesterday. VM and I headed over the the MS Conference Center to listen to the talk. I didn't really know what to expect. Would he keep in character? Or would it be completely different? It was absolutely hilarious. The guy's a fairly mild-mannered journalist. He talked about how his blog started, and spun into this massive thing. He talked about how everyone was trying to oust just who FSJ was. He talked about his book, Options: The Secret Life of Steve Jobs. He talked about the Borg (and how we actually *get it*--ie. that it's a parody), about Apple and Sun and Forbes, made fun of Scoble (hilarious! He talked about the pic of him coming out of an Apple store with an iPhone held high and I was just rolling over in laughter) and various other people. It was a very enjoyable hour. It was videotaped too--not sure where to get a copy though. And no, I didn't see the Beastmaster there taking notes. That would have rocked the house though. [ Update: Lyons did an interview with the Seattle PI afterwards. Video here. ] September 11 QuestsHaving been linked to by the infamous Mini-Microsoft and quoted (although not credited) by MSFTextrememakeover, I thought I should do a follow-up post to the Company Meeting last week. I thought Mini's post was excellent--it has a bunch of stuff I forgot about. I'm on some internal e-mail aliases where people expressed their opinions on how boring the meeting was; I disagree because I think this meeting was frank, as opposed to the glossy, hand-wavy meetings of years past. MSFTextrememakeover, on the other hand, seems hell-bent on impeaching SteveB. I don't have enough context to decide whether or not he's right, but he does make pretty convincing arguments. I would love to hear someone's arguments for SteveB--how much he's contributed to/guided (guiding) the company (and how "Monkey Boy" is helping our image--sorry, I've been reading too much FakeSteve too). It's a pretty one-sided debate at the moment. Nevertheless, I've been thinking about "Quests" after the Company Meeting. BillG talked about the concept of quests; how they should be our guiding north stars, toward which the company should be moving towards. Long-term visions, if you will. Especially since he's leaving the company this year. First of all, I think the word "quest" was a horrible choice. When I heard "quest" I immediately thought of "Qwest" the company, probably because I watch too much TV. The second thing that came to mind was World of Warcraft or Final Fantasy, probably because one of my previous roommates used to play these games for hours on end. The word "quest" invokes images of knights wearing chain mail, riding on horses in King Arthur's time, on a quest to find the Holy Grail. What I definitely did not think about was a long-term roadmap. Anyhow, forgiving our Chairman's lackluster choice of words, I got to thinking about one such "mini-quest" after seeing the demo of the upcoming Windows Live Calendar. I wonder how many people will start using the Calendar? For that matter, I wonder how many people (tech-adopters aside) use Google's Calendar product or any of the other plethora of calendaring products? We use Exchange at work (by the way, contrary to popular belief, Hotmail does not run on Exchange). Exchange has pretty darn good calendaring support, but it's not great if other people aren't on Exchange too. As someone with a work calendar, I don't want to have to deal with calendar for personal stuff too. What you want is a single place to keep track of all your appointments/meetings. This stuff isn't revolutionary. Sometimes I still use pen and paper to keep track of to-do lists. Actually, funny enough, this kind of comes back to the whole (original) Windows Live theme. You. There should be a single place to go to get everything you need. I should be able to tailor my calendar to my work and to my personal life. I should be able to control my social contexts on Facebook. The world should revolve around me. I should be able to own my data and be able to access that data from anywhere on any device. Back to the Company Meeting. I like the annual Company Meeting because it makes us cogs feel like we're actually a part of something as a whole. It lets the masses know that the company actually has some purpose and direction and isn't a big fat hippo with too much inertia to move. It lets us monkeys drink the kool-aid and, once in a while, gives us a dose of reality that many of us desperately need. What I would have liked though, is more tie-in with these "quests" of ours. We've seen the Quests. |
|
|