Scott Hanselman

Hanselminutes Podcast 145 - SOLID Principles with Uncle Bob - Robert C. Martin

January 8, '09 Comments [8] Posted in Learning .NET | Podcast | Programming
Sponsored By

photo_martin_r My one-hundred-and-forty-fifth podcast is up. Scott sits down with Robert C. Martin as Uncle Bob (@unclebobmartin) helps Scott understand the SOLID Principles of Object Oriented Design.

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is our sponsor for this show!

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET and Windows Forms. Enjoy the versatility of our new-generation Reporting Tool. Dive into our online community. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

Technorati Tags: ,,,,

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Be Aware of DPI with Image PNGs in WPF - Images Scale Weird or are Blurry

January 6, '09 Comments [28] Posted in Windows Client | WPF
Sponsored By

Is that enough TLAs (Three Letter Acronyms) in the title there? I continue to mentally deny the existence of DPI (dots per inch) as a concept. It's my own fault. I have been living on PCs at 96dpi for so many years, I just stopped caring. This problem we ran into is SO obvious after the fact, but it flummoxed us for a half-hour.

I've been helping a buddy on a super-cool super secret WPF application and the window has multiple states - regular, compact, and minimized. We've got three transparent PNGs for these three buttons. The designer created them and sent them along.

However, with one of them, it kept showing up at the wrong size, and was blurry. Even if we set the width and height ourselves, it looked totally wrong.

Here's wrong:

wrong

Unrelated to the scaling issue, I saw that the file was 3099 bytes, which I thought was a little large. I opened it up in the Visual Studio binary hex editor and noticed that I could see strings like "Photoshop ICC profile" in the header. PNGs are lossless (they are like Bitmaps, not JPEGs) and while they compress, there are may ways they can compress, like removing crap from headers.

I like to run PNGOUT on all my PNGs as it'll try different techniques to make the PNG as small as possible without losing any data (without changing the way the PNG looks). I ran PNGOUT on the wrong file and it went from 3099 bytes to 292 bytes. It also just happened it look right afterwards.

Here's right:

right

So why did it look right suddenly? Turns out that PNGOUT also changes the DPI, without asking, to 96dpi. Here's a little C# program I wrote to test:

static void Main(string[] args)
{
Image i = Image.FromFile(@"c:\users\scott\desktop\collapse-wrong.png");
Console.WriteLine(i.HorizontalResolution + " " + i.VerticalResolution);
Console.WriteLine(i.Width + " " + i.Height);

Image i2 = Image.FromFile(@"c:\users\scott\desktop\collapse.png");
Console.WriteLine(i2.HorizontalResolution + " " + i2.VerticalResolution);
Console.WriteLine(i2.Width + " " + i2.Height);
}

The output of this is:

72.009 72.009
56 10
96 96
56 10

Both files are 56x10 in dimension, but the first is 72.009 dpi and the second is 96 dpi. WPF defaults to 96 dpi so when it encounters the 72 dpi image it scales it up. If you can't change the image, there are funky ways around it. Personally, I believe if you are running into this because of a designer/developer mismatch, then just coordinate between each other and decide on a DPI, probably 96. In our case, DPI didn't really matter as the designer was developing pixel-accurate images, so we just run PNGOUT on all the images.

I usually run PNGOUT from Powershell, but there is a nice free .NET app called PNGGaunlet from Ben Hollis that provides a nice GUI frontend.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

ASP.NET MVC Design Gallery

January 5, '09 Comments [13] Posted in ASP.NET | ASP.NET MVC
Sponsored By

My teammate Stephen Walther launched the ASP.NET MVC Design Gallery a few weeks back. This stemmed from an idea ScottGu had to let the community change the default templates that ship with ASP.NET MVC. I was supposed to work on this but headed to Africa for the holidays so Stephen helped me out and took over the project and did a better job than I would have!

You can vote on the designs that you like and that you don't like. You also can contribute new designs of your own.

image

After you download a design from the gallery, you can use the design by following these steps:

1) Copy the files from DesignTemplateCS or DesignTemplateVB folder into your project (use DesignTemplateCS for C# projects and DesignTemplateVB for VB.NET projects)

2) Do a search and replace the string [YourProjectName]. Replace [YourProjectName] with the actual name of your project as displayed in the Solution Explorer window.

This last step is necessary to get the namespaces right. Most of the templates use helper methods to render the menu items so that the selected menu item is correctly displayed.

Microsoft also is hosting a design competition to encourage members of the community to submit new designs. Submit a new design before January 31, 2009. ScottGu will be selecting the best three designs and announcing them on his blog. Learn more about the ASP.NET MVC Design Competition and submit your designs before January 31st.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

South Africa 2008: NewsFlash - Turns out eating less and moving more causes weight loss

January 3, '09 Comments [36] Posted in Africa | Musings
Sponsored By

image0-1When I left for South Africa I was 193lbs/87kg, my heaviest ever on a 6 foot/1.8 meter medium frame. This was shocking to me because I could totally see 200lbs coming. It was right there ahead of me. As a Type 1 Diabetic getting fat(ter) is deeply uncool. You can literally be a Type 1 (insulin dependant) and Type 2 (insulin resistant) diabetic at the same time. This would mean I'd need to take MORE insulin to do the SAME amount of work. I'd already started seeing this recently as my total daily insulin dosage, usually under 30U (Units) was creeping up to 40U.

Then we headed to Africa for our semi-yearly sojourn. Fast forward roughly a month and I'm 176lbs/79.8kg. That's 17 pounds, or as a newly svelt person me likes to say "I lost, like, 20 pounds!" The trick will be to keep it off, but here's what I think worked (as I lose nearly 20 pounds on EVERY trip to Africa, and nearly every trip overseas. The tragedy here is the re-gaining and re-losing of that same 20 pounds.)

These are totally obvious observations/tips, but I'm a little dense, so I'm writing them down.

That picture to the right is me when I was thinner. I'm also the short one in the picture.

Don't Eat a Serving Larger than Your Fist

I didn't consciously do this, it just happened. As my wife and I discussed in our recent podcast, folks just show up to visit and food is divided appropriately. This is not to say that I wanted for food in any way. I didn't. It's that my hosts in South Africa gave us a reasonable amount of food, not an American Amount of Food.

This just happened to be about the size of one's fist. Kind of unrelated, I mentioned this to the boy's doctor since the older boy wasn't eating that much and he said that most kids will naturally eat what they need to, no more and no less, and that amount was often about the size of his fist. We'd been wasting a lot of food as I was giving him a plate full of food as big as his head! So, it's a fist-sized serving for me.

Eat Breakfast

I am notorious for eating one giant 1pm meal, ahem, then a giant 6pm meal, then a small (read: almost giant) midnight meal. This didn't fly in South Africa, as there was a lovely light breakfast at 7am, lunch at 1pm, and a dinner around 6pm. Each was appropriately proportioned, especially lunch which went on the assumption that you ate breakfast.

I realized that I've been eaten Epic Lunches to stave off death by starvation from skipping breakfast entirely. While eating breakfast does make my diabetes a little more tricky to manage as it adds a new variable, breakfast does support the next tip...

Eat Only Enough to Get You To the Next Meal

or

You Will Eat Again, In Your Life, You Know

My brother, the Ironman competitor and firefighter, taught me this not-obvious-to-me tip. He says that folks who aren't eating often enough eat like it's the last time they will ever eat. Rather, try eating with the knowledge that you will eat again in a few hours. If you realize that this meal only needs to get you to the next meal. This is the single most powerful dieting tip I've got.

Reset Your Full Indicator

My wife calls overeating "Pushing Through" as in "Oh, I pushed through with that last bite." Not recognizing what full feels like is a big problem for me. Basically you can reset your internal gas tank indicator by just thinking about it. "Am I full?" "Do I really need that next bite?" This has caused me to eat about 1/3 less without actually feeling any less full. Actually, I feel less bloated after meals.

Aside: I actually have personal data (and charts!) that reflects this - while I was in South Africa my daily insulin usage (and hence, carb intake) went from 30-40U a day to 20U. Basically I used 33% to 50% less insulin per day.

Move More

I didn't visit the South Africa that a lot of people do. In my South Africa not everyone has a car. In mine you can easily fit 9 people into a VW Golf. Either way, I walked a lot. If there's no car available and you want to go to the mall that's 3km away, you walk. This, along with sweltering heat and a lot of water, is another nice way to lose weight.

This year I've started working out, six days a week, for eat least an hour. I'm alternating cardio and anaerobic exercise. I'm finding that if I time my workouts with the length of certain television shows, I can make it through without going insane. That means, 40 minutes of cardio while I watch, say, Heroes. Then 22 minutes of weights as I watch Arrested Development. I'm actually watching more TV (and enjoying it) but I don't have to feel bad since I'm moving.

Drink Water

Yes I know if you drink WAY too much water you can mess up your salt/electrolyte balance, but a few liters a day are what I've found is the right amount for me to lose weight. I can't overstate how water intake directly affects weight loss. It's really amazing. Everyone I know with 6-pack abs carries a water bottle around with them. While I was in Africa I really upped my water intake because of the heat, to the point where drinking water at every turn was second nature.

What are your weight loss tips, Dear Reader?

Technorati Tags: ,

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Back to Basics: Explore the Edge Cases or Date Math will Get You

January 3, '09 Comments [21] Posted in Back to Basics
Sponsored By

Disclaimer: I don't work for the Zune team and I don't know anyone on the team. I think that Z2K9 was a bummer, but I don't have any inside knowledge. Everything here came from the public interweb.

Dates will get you every time. Further more, it's all about Edge Cases. This is one of the things you'll think about when doing Test Driven Development and it's one of the things that everyone learns in Computer Science 101. You really have to hit those edge cases, be they dates, or number overflows, or buffer overflows.

The news reports:

"A bug in the internal clock driver related to the way the device handles a leap year affected Zune users," said the company in a statement. "That being the case, the issue should be resolved over the next 24 hours as the time change moves to January 1, 2009."

Disclaimer*2: I have no idea if the following is 100% true, only that it seems quite plausible. I present it for educational purposes, nothing else. It's very interesting. Again, I'm just a caveman.

A Zune Fan poked around in the source code from the vendor (Freescale Semiconductor) that made the real time clock in the Zune (The vendor's source for rtc.c is here) and with the benefit of hindsight, noted that there's the opportunity to get stuck in an infinite loop.

The Zune 30 shows the date and time, as do many devices, as the number of days and seconds since January 1st, 1980 at midnight.

year = ORIGINYEAR; /* = 1980 */

while (days > 365)
{
if (IsLeapYear(year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}

The days variable is read out of the memory location managed by the Real Time Clock (RTC). When the value of days == 366, you break out of the inner loop, but you can never get out of the outer loop.

A number of folks have blogged about the bug, their analysis and how they'd fix it. Programming Phases has a good post and folks have twittered suggestions). The basic problem is that since there are 366 days remaining when the calculations are reached for the year 2008, there will never be another subtraction to bring the total below 365, so the loop continues. The value of days is stuck at 366. It IS a leap year, but days is not > 366, so the loop continues.

In working with banking software for years, I can tell you that dates'll get you. When dealing with dates and date math you can't underestimate the value of really good code coverage. Also, even if you have 100% coverage, as I learned in my interview with Quetzal Bradley, 100% coverage just tells you a line of code DID run, it doesn't tell you that it ran correctly.

I noticed also when I visited my Live home page on Dec-31 that it said today was Jan-1, likely a Time Zone glitch. However, when I clicked the date, I was taken to a page with historical facts about Dec-31. ;)

jan1

dec31

Dates, especially when TimeZones are added in, are notoriously hard.

To this day there are a half-dozen bugs in DasBlog where we have a devil of a time with Time Zones. Omar spent weeks fighting with them before he just gave up. We have to reconcile the local time of the visitor, the time on the server, and GMT time (the time we store everything in). It usually works, but when the Server isn't on GMT we get into all sorts of trouble. We also have problems with clients that call the XML-RPC APIs, some of which use UTC (GMT) time and some use local time. Other than keeping an internal table to wrong-headed clients, there are no good solutions.

In 2007 CNN talked to a Major General about a bug in some F-22's that caused them to malfunction when going across the international dateline. The Unofficial Apple Weblog had an interesting post on Apple Date/Time bugs through the years. If you're interested in working with and maintaining legacy code, I recommend Michael Feathers' Working Effectively with Legacy Code.

As a random slightly-related aside, I was over at Hollywood Video buying a used copy of Mirror's Edge yesterday and some folks were talking about the Zune problem. The manager said, wow, I'm running the music in here on a Zune right now, and produced his brown 30G Zune from behind the counter. He either hadn't turned it on today fresh or the clock was wrong so he was able to weather the whole day without an incident. He seemed pretty pleased about his "survival."

I wonder how many other less widespread devices are running this real time clock and if any of them had trouble as well.

Do you have any personal Date/Time stories, Dear Reader? Please share in the comments!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

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