Matt Casto's .NET Journal RSS 2.0
 Friday, January 26, 2007
While searching for the VS 2005 CMD Prompt Here shell extension I stumbled upon some other tools that I really like. First of all, the Clean Sources application does exactly the same thing as a command line application that I whipped up two days ago, except it provides better feedback and is integrated into the explorer shell.

But the best find was by Omar Shahine, the same author of Clean Sources - Clipboard To Image. This application takes care of the manual steps I constantly find myself doing when I take screenshots of application or diagrams, and it even defaults to the PNG format! (yay)

Now I just have to integrate it into SlickRun and move on to being more productive.

** Note:
After playing with Clipboard to Image for a bit, I noticed that if you try to overwrite an existing image, the overwrite never actually happens. So you just have to create a new image each time.

Labels:

Friday, January 26, 2007 11:50:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [2] -
tools
You would think that calculating the age of a person based on their date of birth would be simple, but it's more tricky than that. Like most problems in programming, things are never what they seem at first glance.

The .NET framework has some greate objects for working with dates and time - System.DateTime and System.TimeSpan, and it's trivial to do a calculation like the following.
public int CalculateAge(DateTime birthDate)
{
return DateTime.Now.Year - birthDate.Year;
}

Our return value has the correct calculation of the difference in years between the current date and the birth date, but that isn't what a typical person considers an age. The following test cases will show that this function isn't correct.
[TestMethod]
public void TestAgeCalculation()
{
DateTime dob = DateTime.Now.AddYears(-1);

// born a year ago yesterday
dob = DateTime.Now.AddDays(-1);
Assert.AreEqual(CalculateAge(dob), 1);

// born a year ago today
dob = DateTime.Now.AddDays(1);
Assert.AreEqual(CalculateAge(dob), 1);

// born a year ago tomorrow
dob = DateTime.Now.AddDays(1);
Assert.AreEqual(CalculateAge(dob), 0);
}

The third test case fails because age doesn't automatically equal the difference in years. Another solution that comes quickly to mind is to get a TimeSpan which is the difference between now and the date of birth, but there's no method that converts a TimeSpan to the number of years. What you can get from a TimeSpan is the number of days, which you can then divide to get years.
public int CalculateAge(DateTime birthDate)
{
TimeSpan ageTime =
DateTime.Now.Subtract(birthDate);
return Convert.ToInt32(Math.Floor(
ageTime.Days / 365.25));
}

This fails too because we consider ourselves to be a year older on our birth day, even though we're actually not a year older until the day after our birthday. Also, I don't really like the division by 365.25 because it's not entirely accurate. Of course, that's all ignoring the time of birth in the equation.

Here is what I finally came up with to accurately calculate an age from date of birth.
public int CalculateAge(DateTime dob)
{
int years = DateTime.Now.Year - dob.Year;
if (dob.AddYears(years) > DateTime.Now)
years--;
return years;
}

Definitely not all that complicated, but more tricky than I imagined it would be when I started to tackle the solution. This is one of those things that all programmers run into at least a handful of times during their career and, I'd hope, take some time to make sure their code is accurate, but it's easy to just throw code out there that works 98% of the time.

Labels:

Friday, January 26, 2007 7:47:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [2] -
c#
 Friday, January 19, 2007
I was walking down a empty hall, not sure where I was. Ah, there's the room I was looking for. As I walked through the door, the people inside turned around and looked at me. Some had clothes with penguins on them, or carried devices with logos of partially eaten fruit. "Where you from, cuz?" the one up front said. Oh no, I'm dead now!

Okay, so maybe that's what I imagined it might be like walking into a session focused on Python. I was definitely outside of my Microsoft comfort zone. But that's what CodeMash is supposed to be about, so I figured I'd go out of my way to see a presentation on something almost totally unknown. It turned out to be nothing like my overactive imagination, and I don't think I stood out at all. I'm not sure why I expected anything different, really.

I was at Mark Ramm's presentation on SQL Alchemy. Honestly, I wasn't following all of the details and I'm not clear on what I took away from it. At the very least I gained a basic understand, exposure or awareness of something that I wouldn't normally see. It was definitely interesting, but the Q & A was totally over my head so I ducked out early.

There are a few things I've noticed about CodeMash so far. As the conference has progressed, I've noticed people being a lot more comfortable asking questions or giving comments without being prompted. So far, the sessions I've been in where the audience contributed have been the best experiences.

Also, I never imagined I'd be live blogging, but today I found that I had a lot to say. I still don't like the term and wouldn't really admit to doing it.

Oh yeah, and I sat near a guy who totally looked like Don Knotts.

Labels:

Friday, January 19, 2007 12:21:00 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
codemash
Today's sessions that I plan on attending definitely focus on data access, which actually has me excited after the ridiculous models at my last job.

I just got out of Scott Guthrie's keynote on LINQ. LINQ definitely has me excited, and I need to play with the release from last year.

Right now I'm in Dave Donaldson's session on NHibernate. Code with no more stored procedures? Excellent! I'll follow up later with my impressions.

Impressions:
Dave has written an interesting framework called NHibernateRepository that I might look into using. Its not integrated into the NHibernate project, but Dave said that he's going to eventually look into getting it incorporated into a contrib project.

I was suspicious of what the performance would be with an O/R mapper, but since NHibernate uses parametrized queries, its virtually no different from stored procedures in that respect. From Dave's demo I got the impression that if you need to do tricky things, the way to go about it is using a syntax called HQL, which looks just like hard coded queries, and weren't we supposed to get away from that?

Differences between NHibernate and LINQ were also on my mind. LINQ has a nice visualizer that shows the actual query when you're debugging, which is a lot nicer than having to use the profiler or read a log file. It occurred to me when NHibernate's insert/update/delete features were being demonstrated that I haven't seen any examples of updating data with LINQ. Is LINQ supposed to be used solely to query data, and not modify it?

You have to write a lot of configuration and code by hand with NHibernate .... unless you use a code generator. Dave demonstrated a CodeSmith template that would (have if it hadn't crashed) create entity classes and config from your database. But the code still wasn't pleasing to look at. I wondered if, like Dave's NHibernateRepository library, if anyone has adapted the Data Access Application Block to work with NHibernate, or another O/R Mapper for that matter.

Another, maybe totally unrelated, thought was "where is the c# in stored procedures?" C# embedded in stored procedures was a feature that was highly touted in .NET 2.0, but I have yet to see anyone use it other than code in MSDN documentation. Is this feature really something people want to use? Don't we want to stay away from business logic in stored procedures? Yeah.

I'm in Mark Ramm's session on O/R Mapping with SQL Alchemy right now, which is definitely outside of my comfort zone. This is what CodeMash is supposed to be all about, so we'll see how I like it.

Labels:

Friday, January 19, 2007 10:00:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
codemash
 Thursday, January 18, 2007
I made to CodeMash with about 10 minutes to register before the morning keynote. I'm really glad I made it to the keynote, which was about domain specific languages. Neal Ford did a really good job and I can't wait to go back through his presentation and compare it to my notes about ways I could incorporate DSL into some various ideas I have. I'm not ready to share those ideas yet, but when I feel like they're not totally silly I'll be posting them here.

Other sessions I attended today were ...
  • Smart Clients - more like an introduction and didn't see anything I didn't already know.
  • Ruby on Rails - This was a pretty cool session on Active Record and talking about how Rails works. My only complaint is that I should have sat closer because I couldn't read the text on the code examples.
  • TDD - a session about benefits from TDD beyond the main idea of units tests - very good.
  • An excellent keynote by Bruce Eckel that ... well ... I'm not sure what the focus was, but it was very entertaining and sparked some interesting thoughts.
I'm not sure what to expect tomorrow, but if its anything like today I'm going to be rocked. I know it starts off with Scott Guthrie doing a talk on LINQ, which I'm stoked for.

Labels:

Thursday, January 18, 2007 11:15:00 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
codemash
 Friday, January 12, 2007
I always feel a little bit guilty when I read Microsoft newsletters through gmail.

I think Google knows this, and is trying to help ...

Labels:

Friday, January 12, 2007 7:57:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
humor
 Friday, January 05, 2007
Some of the things I read in R. Aaron Zupancic's blog post New Operator in C# 2.0: ?? caught me off guard. I swear I'd read through several articles listing changes and additions in .NET 2.0 and C# 2.0, including MSDN, but never ran across the ?? operator. I even checked the C# Operators documentation and didn't see ?? Operator until the second pass, because it's grouped in the assignment category. I expected it to be grouped in the conditional category with ?:, but now I'm wondering why ?: isn't considered an assignment operator?

While the null coalescing operator is cool and looks nice in code, I'm not sure how much I'll use it. Most of my code where I'm checking whether a variable is null throws an exception or takes some other action instead of using another value in place of the null variable.

The most interesting thing that I noticed was the following comment from Owen Blacker:
It's even cooler than that. The null coalescing operator does type forcing, too.

Imaging you have this:

public int? GetUserID() { ... }

int userID = GetUserID() ?? -1;

The ?? operator not only provides an alternative value if GetUserID returns null, but it also does type conversion, from the int? result of GetUserID to a non-nullable Int32, if it returns a non-null result.
I was really thrown off by the int? part of the example code in the comment above. I eventually found that it's the same thing as NullableType. That's really nice syntax to replace using a nullable value type. Before I knew what it was, I immediately assumed that it was something like a nullable type. The question mark following the value type suggests that you're not sure that the returned value will be integer or not. I will definitely get some mileage out of this information. Finally, a way to represent a value from a SQL bit column in code.

I'm not sure why I forgot about Nullable Types. I do recall reading about them in the feature list for a .NET 2.0 beta, but I don't remember seeing anything about them in any "what's new" articles. Maybe I just missed it.

Back to the operator, I think the next example reads a lot cleaner than the following one which uses the NullableType's GetValueOrDefault() method. Of course, its probably better to use that method so you aren't hard coding a value type's default value in code (not that it would change).

Using the ?? operator:
int? x = null;
int y = x ?? 0;
Using the GetValueOrDefault() method:
int? x = null;
int y = x.GetValueOrDefault();
It's interesting that the null coaelscing operator post is making rounds now, almost exactly a year after it's original posting.

Labels:

Friday, January 05, 2007 7:43:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
c#
 Thursday, January 04, 2007
Yesterday I posted about input devices, and mentioned that I couldn't configure the thumb button of my Microsoft USB Comfort Optical Mouse 3000 to enter the Alt+Tab keystroke. So what did I set as the functionality of the thumb button? I just had it configured to the default Back button functionality, which I found myself never using, and left it at that. Then I found a utility that lended itself very well to being mapped to the thumb button.

Clipboard Recorder is a utility that stores the last 100 snippets of text that have been copied to the clipboard. Among it's several interfaces, there is a pop-up menu that can be activated with the keystroke Ctrl+Alt+V. I mapped this keystroke to my mouse's thumb button and, viola! My favorite mouse configuration ever!

Clipboard Recorder Pop-up

I don't know how I lived without a clipboard history up to now. As a software developer, it is indispensable. Copying and pasting sections of code is something done every day, and it's very nice to be able to copy code knowing that you'll need it later, but not worrying about overwriting it in the clipboard, and maybe having to store it in a temporary text file. Also, our defect management software at work leaves a lot to be desired, and I find myself copying and pasting the same 3-4 pieces of information into several places, including VSS.

I love Clipboard Recorder and that's why I registered it on "Support Your Favorite Small Software Vendor Day" last month.

Some other utilities that I use daily:
And some utilites that I use every now and then:
Also, I have del.icio.us utilities links to tools that I want to check out some day.

Update:
I'm counting this as my official "Tools Day" blog post, in support of Jim Holmes and James Avery's new book "Windows Developer Power Tools".

Labels:

Thursday, January 04, 2007 10:26:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
tools
 Wednesday, January 03, 2007
I love ThinkGeek. I get their newsletter and get a kick out of checking out what new products they're carrying. I also maintain a wishlist there and occasionally add items that I think are cool. That way, whenever anyone asks me what I want for Christmas or my birthday, I can just tell them to check out my wishlist on ThinkGeek and Amazon (I keep one there too).

I often forget what I added to the wishlist just because I thought it was cool at the time. At Christmas this year, my brother in law surprised me by getting me something from there that I'd forgotten about. The StealthSwitch!

Hiding windows at work is pretty juvenile. When my father saw what I'd gotten, he said that some people at his office had installed a program called "the big cheese" or something like that, which would display a fake spreadsheet when they hit F1. He had the IT department un-install it and limit his employee's rights so they can't install applications now. Of course, he was concerned that I was doing something at work that I wanted to hide. But that's not the case!

I wanted the StealthSwitch foot pedal to play with as a third input device. I'm not a keyboard Nazi, but I am particular about the input devices that I use. I use the Microsoft Natural MultiMedia Keyboard and love the over sized Delete key arrangement, but really don't like what they did with the function keys. I am really a mouse guy and I fall back to using the mouse even when I know keyboard shortcuts for some things. For instance, I know that Ctrl+B builds in Visual Studio, but I have a custom toolbar shortcut that I use instead.

I used to have a cheap mouse with a thumb button that I had configured to enter the Alt+Tab keystroke. I found it to be VERY convenient since my right hand is on the mouse so often. When that mouse went bad, I got a Microsoft USB Comfort Optical Mouse 3000 because it has a thumb button and the scroll wheel features the horizontal tilt. While the mouse looks good, the horizontal scrolling isn't useful, and I'm disappointed by it because the Intellipoint software doesn't allow me to enter Alt+Tab as a keystroke for the thumb button. Tab isn't captured by the keystroke form, and I even tried to enter it directly into the registry but the driver didn't recognize it.

Enter the StealthSwitch. The software installed from the CD that came with the device only allowed for hiding windows, and even then it was limited to hiding all windows, the current window, or all but windows with a short list of keywords in the title. Even if I was going for the window hiding functionality, I would want to specify particular windows to hide and this didn't allow that. I figured I was going to have to write some software to use the pedal's input.

Then I found a newer version of the software on the (incredibly bad) StealthSwitch website. The latest version not only allows you to list the windows you do want to hide, but also has a screen to configure custom actions. Eureka!
StealthSwitch Custom Configuration

Now I can have my Alt+Tab functionality again, and I don't have to worry about where my hands are ... I use my foot! Maybe I should look into the specialized foot pedals meant to replace the mouse or augment game play.

Or, I could give the Zero Tension Mouse a try ... nah, never mind.

Labels: ,

Wednesday, January 03, 2007 8:37:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
devices | tools
 Tuesday, January 02, 2007
CodeMash
I just registered for the CodeMash conference and I can't wait to go!

I held off registering in order to make sure it would fit our schedule and budget, and missed the early registry discount and also the block of rooms at the resort, but that's okay. I'll probably only stay there one night anyway.

woot!

Update: There were still rooms available in the block for the conference, even though the CodeMash site didn't reflect that.

Labels:

Tuesday, January 02, 2007 10:53:00 AM (Eastern Standard Time, UTC-05:00)  #    Comments [2] -
codemash
 Friday, December 29, 2006
I found the shape of my lunch today to be very pleasing, so I decided to snap a picture with my cell phone. I may decide to keep a picture log of my lunches ... we'll see.

From Miscellaneous

Labels:

Friday, December 29, 2006 4:51:00 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
humor
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Matt Casto
Sign In
All Content © 2008, Matt Casto
Theme based on DasBlog theme 'Business' created by Christoph De Baene (delarou)