Monthly Archives: November 2007

Door to Door Organics

A few months ago, Leticia and I signed up for a service called DoorToDoorOrganics. Simply put, this is a way to get farm-fresh, organic produce from local farmers, delivered to your door.

How it works is this:

  1. Create an account, decide on the size and type of box you want.
  2. Decide how often you want–every week or every two weeks (or just once)
  3. Friday before the week of delivery, you’ll get an e-mail. You can go to the site and customize the items in your box.
  4. Get the box on Wednesday via UPS. It has ice packs, food is fresh.
  5. Eat. Enjoy good, fresh food.

The quality of food is really high. And you also get occasional things that are new and different. We’ve loved it. We never have to go to the store for this stuff anymore. We go only occasionally now for milk, eggs, bread. Maybe every two months for a huge stocking trip.

Technorati Tags: ,,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

How to really have Middle East peace

With the talks in Annapolis this week between Middle East governments, peace is on a lot of people’s minds. A meeting like this, while it won’t solve anything immediately, does illustrate the point that you can’t reasonably talk and fight at the same time. With that in mind, I have a permanent solution.

How do you avoid getting things done at your job? You have meetings. You have meetings to plan your work. You have meetings to plan the meetings to plan your work. You have meetings about why the last meeting failed. You have meetings to decide to continue the meetings.

So let’s schedule everybody for a meeting once or twice a year for the next 500 years. Send them all a recurring Outlook appointment.

Of course, you’d have to make the punishment for not showing up pretty severe (like getting fired). Maybe just create an elite international team of kidnappers that will retrieve a missing president. (By the way, it wouldn’t do for the presidents to send delegations–they all have to come in person).

But what to do at these meetings? After a hundred years or so, they will have discussed everything in detail, so I suggest moving on to games. We’ll start with something formal and proper, like chess. After a few hundred years of international presidential chess (televised live, of course), they’ll begin to loosen up and we can move on to more interesting things, like Twister or water polo. Anybody who starts a war that year gets picked last.

 

Technorati Tags: ,,

Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

The Benefits of Having Too Much Processing Power

Do an experiment: keep Task Manager or any other CPU activity monitoring program up on your screen for a few hour or days, glancing at it every so often. Do you see it EVER above zero (other than momentary spikes)?

Here’s mine, from a Google sidebar gadget:

mycputime

I’ve got a Dual Core and 2GB RAM. Currently I have open two copies of Visual Studio 2005, Word 2003, Outlook 2007, Paint.Net, RSS Bandit, Adobe Reader, IE, MSDN help, Windows Live Messenger, and Google Deskbar.

So that’s using just over 1 GB of RAM. And ZERO CPU. I’m watching this. The CPU meter goes up a little when I type, open a new program, compile my source code, etc., but most of the time it’s zero, even when I think I’m actually working.

I used to eschew running apps like Google Deskbar, wallpaper helpers like Display Fusion, or other system utilities that continually run. But I had a realization–it doesn’t matter! I could run many more utilities concurrently and still not come anywhere close to creating a slowdown on my computer.

Of course, I’m only talking about non-interfering/non-processor-intensive programs. This immediately excludes anti-virus programs, which interrupt every process to examine system behavior continually, or running video compression (duh) in the background.

But things like desktop searching, system monitoring (if it’s not too intrusive), utilities, and any other independent process–yeah, just throw them on. They won’t make a dent.

They key word in that last paragraph is independent. Independent means they don’t depend on or interfere with other processes.

Technorati Tags: ,,,,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Difference between ConfigurationSettings and ConfigurationManager

If you upgraded a project from .Net 1.0/1.1 to .Net 2.0, and it used application configuration files, you will soon come across the compiler warning message

‘System.Configuration.ConfigurationSettings.AppSettings’ is obsolete: ‘This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings’   

You have to add a reference to the System.Configuration assembly, but once you do, you can just do a search and replace on ConfigurationSettings.AppSettings and replace it with ConfigurationManager.AppSettings and the functionality will remain the same.

The advantage of using ConfigurationManager is laid out in the MSDN docs, but briefly:

  1. Access sections other than appConfig, including connectionStrings, and your own custom sections
  2. Can read and write the configuration.
  3. It can have separate settings for the application and the users
  4. It’s extensible

Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

The Effective Software Developer’s Book List

What books should all serious developers read and study? This is a list of books that I have either found particularly helpful in my own growth as a programmer, or that are popular on various required-reading lists. I have bolded books I consider absolute required reading (probably multiple times) for all software developers.

I’ve gathered these books from multiple sources, beginning with Steve McConnell’s list in the back of Code Complete.

By the way, just because a book is listed in the Introductory section does not mean you shouldn’t read it because you’re advanced.

Why the Have I Read? column? To keep me honest, and to serve as my own checklist.

(Updated 11/28 – PeopleWare)
(Updated 12/7 – Design of Everyday Things)
(Updated 1/26 – Beautiful Code, Essay section)
(Updated 6/3 – Pragmatic Unit Testing in C# with NUnit)

Introductory

Book

Have I Read?

Code Complete: A Practical Handbook of Software Construction
Steve McConnell
Y
Conceptual Blockbusting: A Guide to Better Ideas
James L. Adams
 
Programming Pearls (2nd Edition) (ACM Press)
Jon Bentley
Y
Facts and Fallacies of Software Engineering
Robert Glass
 
The Pragmatic Programmer: From Journeyman to Master
Andrew Hunt and David Thomas
Y
Object-Oriented Design Heuristics
Arther Riel
Y
UML Distilled: A Brief Guide to the Standard Object Modeling Language (3rd Edition) (The Addison-Wesley Object Technology Series)
Martin Fowler
 
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition)
Craig Larman
 
Refactoring Workbook
William Wake
 
The Mythical Man-Month: Essays on Software Engineering, Anniversary Edition (2nd Edition)
Frederick Brooks
Y
Introduction to Algorithms
Thomas Cormen
 

Intermediate

Book Have I Read?
Software Configuration Management Patterns: Effective Teamwork, Practical Integration
Stephen Berczuk and Brad Appleton
 
Software Creativity 2.0
Robert Glass
 
Testing Computer Software, 2nd Edition
Cem Kaner, Jack Falk, Hung Q. Nguyen
 
Rapid Development
Steve McConnell
 
Software Requirements, Second Edition
Karl Wiegers
 
“Manager’s Handbook for Software Development” (PDF)
NASA Goddard Space Flight Center
 
Patterns of Enterprise Application Architecture (The Addison-Wesley Signature Series)
Martin Fowler
 
Test Driven Development: By Example (The Addison-Wesley Signature Series)
Kent Beck
Y
Pragmatic Unit Testing in C# with NUnit, 2nd Edition
Andy Hunt, Dave Thomas, Matt Hargett
Y
Refactoring to Patterns (The Addison-Wesley Signature Series)
Joshua Kerievsky
 
Head First Design Patterns (Head First)
Elisabeth Freeman, Eric Freeman, Bert Bates, Kathy Sierra
 
User Stories Applied: For Agile Software Development (The Addison-Wesley Signature Series)
Mike Cohn
 
Agile Principles, Patterns, and Practices in C# (Robert C. Martin Series)
Robert Martin
 
Applying Domain-Driven Design and Patterns: With Examples in C# and .NET
Jimmy Nilsson
 
Extreme Programming Explained: Embrace Change (2nd Edition) (The XP Series)
Kent Beck
 
The Design of Everyday Things
Donald A. Norman
Y
Beautiful Code: Leading Programmers Explain How They Think  

Professional

Book Have I Read?
Software Architecture in Practice (2nd Edition) (The SEI Series in Software Engineering)
Len Bass, Paul Clements, and Rick Kazman
 
Refactoring: Improving the Design of Existing Code (The Addison-Wesley Object Technology Series)
Martin Fowler
Y
Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley Professional Computing Series)
Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides
Y
Principles Of Software Engineering Management
Tom Gilb
 
Writing Solid Code
Steve Maguire
 
Domain-Driven Design: Tackling Complexity in the Heart of Software
Eric Evans
 
Working Effectively with Legacy Code (Robert C. Martin Series)
Michael Feathers
 
Agile Estimating and Planning (Robert C. Martin Series)
Mike Cohn
 
Lean Software Development: An Agile Toolkit (The Agile Software Development Series)
Mary Poppendieck and Tom Poppendieck
 
Implementing Lean Software Development: From Concept to Cash (The Addison-Wesley Signature Series)
Mary Poppendieck and Tom Poppendieck
 

Essays

Essay Have I Read?
Teach Yourself Programming in Ten Years
Peter Norvig
Y
They Write the Right Stuff
Charles Fishman
Y
The Humble Programmer
Edsger Dikstra
Y

Management

Book Have I Read?
Peopleware: Productive Projects and Teams
Tom DeMarco
Y

Windows

Book Have I Read?
Microsoft Windows Internals, Fourth Edition: Microsoft Windows Server(TM) 2003, Windows XP, and Windows 2000 (Pro-Developer)
Mark Russinovich and David Solomon
Y
Programming Windows, Fifth Edition
Charles Petzold
Y
Programming Applications for Microsoft Windows
Jeff Richter
Y
Programming Windows With MFC
Jeff Prosise
Y

.Net

Book Have I Read?
Inside C#, Second Edition
Tom Archer and Andrew Whitechapel
Y
CLR via C#, Second Edition (Pro Developer)
Jeff Richter
Y

Technorati Tags: , , ,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Resume Help

FreeResumeReview.org is my latest sponsor to my Buy Me a Lego campaign. The cool thing about them: they actually really are FREE. The tips section is useful on its own, but the really cool thing is that you can upload your resume and cover letter and they’ll review it for free.

Free Resume Review dot org was created by a corporate executive who saw a great need for free, professional, personalized advice regarding effective resume writing. After teaching courses in career planning and serving on a hiring committee he decided to create his own free site where job seekers could learn great tips, connect to other helpful sites, and most importantly, upload their own resumes and cover letters to be reviewed by qualified staff.

I’m bookmarking this site and am going to come back to it soon when I update my resume. Go check them out.

Technorati Tags: , ,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Instant Searching and Filtering in .Net – Part 4

This is the final part of my series on instant searching and filtering using C#. The only further issue that I wanted to cover was efficiently using a ListView when the items  will change so often.

ListViews already have the concept of a virtual mode, where the consumer of the class must supply the items that are displayed.

When the VirtualMode property is set to true, you also need to set the VirtualListSize property to tell the control how many items it needs to think it has.

With this simple starting point, we can have something like the following:

 

   1: _listControl.VirtualMode = true;
   2: _listControl.RetrieveVirtualItem += 
   3:     new RetrieveVirtualItemEventHandler(list_RetrieveVirtualItem);

and list_RetrieveVirtualItem could look like this:

 

   1: void list_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
   2: {
   3:     e.Item = new ListViewItem();
   4:     e.Item.Text = "Item Text Here";
   5: }

Using this, it would be fairly easy to hook up our filtered results and away we go. But we have a few problems:

  • We don’t want to generate a new ListViewItem every time the list control asks us for one. This is extremely wasteful–thousands of objects would be springing to life and dying constantly, which is a very inefficient use of the garbage collector.
  • On the other hand, we don’t want to cache a ListViewItem object for every single line of text that was indexed. Only a few of them will be on the screen at a time, and we’ve already cached the text, so we should use that.

Therefore, we need a way to efficiently cache a limited number of ListViewItem objects and use them as necessary, updating their Text and Tag properties as necessary.

With that introduction, let’s get into the code.

Our own ListView

First things first: there’s a bug in the .Net framework that impacts virtual mode on ListView. So let’s derive our own to resolve it:

 

   1: class QuickList : System.Windows.Forms.ListView
   2: {
   3:     /// <summary>
   4:     /// This is needed to fix a bug in the framework
   5:     /// </summary>
   6:     public new int VirtualListSize
   7:     {
   8:         get
   9:         {
  10:             return base.VirtualListSize;
  11:         }
  12:         set
  13:         {
  14:             if (Items.Count > 0)
  15:             {
  16:                 EnsureVisible(0);
  17:             }
  18:             base.VirtualListSize = value;
  19:         }
  20:     }
  21: }
 

That’s all there is to that. If you would like more information about the bug, Shahar Prish wrote up an excellent description and workarounds.

With that out of the way, most of the action we’re concerned with will take place in a separate class, which I’ll call…

QuickListController

This controller class is responsible for coordinating the filter text, filtered results, and their display in the list control. In my own use of these concepts, I’ve further factored out the filtering mechanism to be outside of this controller, but in this sample it’s better to combine it all for ease of understanding.

So how will this work? We want a cache of only the items that will be displayed. Unfortunately, the ListView control is going to ask us for item 30,775 for example, if we’re scrolled down a huge list. We don’t want to cache 30,775 items when only the surrounding 20 items are displayed. Fortunately, it’s easy to track a minimal number of items if we know the first visible index

Here are the fields and properties we’ll need:

 

   1: class QuickListController
   2: {
   3:     private int _topIndex = -1;
   4:     private int _bottomIndex = -1;
   5:     private IIndexer<string> _indexer;
   6:     private IList<string> _allLines;
   7:     private IList<string> _currentResults;
   8:     private QuickList _listControl;
   9:     List<ListViewItem> _itemCache = new List<ListViewItem>();
  10:     private string _filter;
  11:     
  12:     #region Properties
  13:     public IList<string> CurrentResults
  14:     {
  15:         get
  16:         {
  17:             return _currentResults;
  18:         }
  19:     }
  20:     public string FilterText
  21:     {
  22:         get
  23:         {
  24:             return _filter;
  25:         }
  26:         set
  27:         {
  28:             _filter = value;
  29:         }
  30:     }
  31:     public IIndexer<string> Indexer
  32:     {
  33:         get
  34:         {
  35:             return _indexer;
  36:         }
  37:         set
  38:         {
  39:             _indexer = value;
  40:         }
  41:     }
  42:     public IList<string> AllLines
  43:     {
  44:         get
  45:         {
  46:             return _allLines;
  47:         }
  48:         set
  49:         {
  50:             _allLines = value;
  51:         }
  52:     }
  53:     #endregion
  54:     //methods, etc.
  55: }

Ok, lots of boring code. We track the top and bottom indexes of the visible items, we store our indexer, a list of all the results (for when no text is entered–no point in searching for nothing). _currentResults will point to either _allLines or the actual results. _itemCache is a list of ListViewItem objects that will serve as our cache. And, of course, we need the text we want to filter on.

You’ll notice that the filtered results are now strings instead of bools. In previous articles, we didn’t care about the actual results–just a count of them. Now, we need to display them, so the filtered items are the same as the keys–the lines of the text in a file.

Constructor

Construction is likewise straightforward:

   1: public QuickListController(QuickList list)
   2: {
   3:     _listControl = list;
   4:     _listControl.VirtualMode = true;
   5:     _listControl.CacheVirtualItems += 
   6:         new CacheVirtualItemsEventHandler(list_CacheVirtualItems);
   7:     _listControl.RetrieveVirtualItem += 
   8:         new RetrieveVirtualItemEventHandler(list_RetrieveVirtualItem);
   9: }

Our controller requires an instance of a QuickList object, which it then sets to virtual mode and listens for two events. The first, CacheVirtualItems, is triggered when the ListView needs a new range of objects. It gives us an opportunity to see what the new top and bottom will be and update our cache accordingly.

The RetrieveVirtualItem event is called when the ListView needs to get the item to display. As we’ll see below, handling both of these events is fairly straightforward.

CacheVirtualItems

 

   1: void list_CacheVirtualItems(object sender, CacheVirtualItemsEventArgs e)
   2: {
   3:     _topIndex = e.StartIndex;
   4:     _bottomIndex = e.EndIndex;
   5:  
   6:     int sizeNeeded = _bottomIndex - _topIndex + 2;//extra 2
   7:     int toCreate = sizeNeeded - _itemCache.Count;
   8:     if (toCreate > 0)
   9:     {
  10:         if (_itemCache.Capacity < sizeNeeded)
  11:         {
  12:             _itemCache.Capacity += toCreate;
  13:         }
  14:         for (int i = 0; i < toCreate; i++)
  15:         {
  16:             _itemCache.Add(new ListViewItem());
  17:         }
  18:     }
  19: }

We save the top and bottom indexes (we actually never use the bottom index), and we then calculate how many total items we need, how many new ones we need to create, and then create new ones if necessary.

You’ll notice that there is no mechanism for removing items from the cache. As we know, another name for cache with a bad (no) removal policy is a memory leak, but since the size of this cache is limited by screen characteristics, and in the most dire circumstance shouldn’t be more than a few hundred, we’ll let it slide (just this once).

RetrieveVirtualItem

 

   1: void list_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
   2: {
   3:     if (e.ItemIndex < _topIndex)
   4:     {
   5:         //should never get here
   6:         Debug.Assert(false);
   7:         _topIndex = e.ItemIndex;
   8:     }
   9:     
  10:     e.Item = GetListViewItem(e.ItemIndex, _topIndex);
  11: }

You’ll notice that if block shouldn’t ever be called, but during debugging I did have an instance where that was the case. It may have been a bug that has since been fixed, but I left it in for safety.

The actual work is done another function:

GetListViewItem

 

   1: private ListViewItem GetListViewItem(int itemIndex, int topIndex)
   2: {
   3:     if (itemIndex < 0 || topIndex < 0)
   4:     {
   5:         return null;
   6:     }
   7:     int cacheIndex = itemIndex - topIndex;
   8:     
   9:     Debug.Assert(cacheIndex < _itemCache.Count);
  10:     ListViewItem item = _itemCache[cacheIndex];
  11:     item.Text = _currentResults[itemIndex];
  12:     //could set tag if you have more complex objects
  13:     //item.Tag = _currentResults[itemIndex];
  14:     return item;
  15: }

Now that you understand how this is working, this function is really simple. Again, we do a simple sanity check. We calculate the index into our cache, which is simply the offset from the top index. After retrieving the pre-created item, we set its text (you could also set the item’s tag, color, or any other property here).

Now, some of you are wondering, “but wait, where do you set the VirtualListSize property?” Yes, that is rather important, isn’t it? How is the ListView control supposed to know how many items to ask for? For that matter, where is the actual filtering done?

The Filter

We have one more function, this one a public interface to the class’s consumer:

 

   1: public void RefreshList()
   2: {
   3:     if (string.IsNullOrEmpty(_filter))
   4:     {
   5:         _currentResults = _allLines;
   6:     }
   7:     else
   8:     {
   9:         _currentResults = _indexer.Lookup(_filter);
  10:     }
  11:     _listControl.VirtualListSize = _currentResults.Count;
  12: }

If the filter text is empty, we set our current results to the full set of lines. Otherwise, run the filter. Then we set our control’s virtual size to the number of results.

Conclusion

If you’ve struggled this far, congratulations. If you find anything in any of these articles useful, please leave a comment and let me know! If you find bugs or problems with my approach, I’d love to hear about better ways of accomplishing things.

The project download contains a GUI that hooks up these classes to an easy-to-use interface.

Also, if you’ve found this useful, please Buy me a Lego!

Download the project. (run the GuiSearch project–I changed TestSearch to a library file)

Download Les Misérables for testing.

 

Technorati Tags: , , , ,

Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Tip: Mouse back and forward work in Visual Studio 2005 too

You know how you can use the extra mouse buttons to move back and forward in Internet Explorer?

The same shortcuts work in Visual Studio. Suppose you right-click on a function call, and select Go To Definition. Once you’re done looking at the definition, hit the back button on your mouse: You’re taken right back to where you came from!

(The keyboard equivalents are Ctrl+ – and Ctrl+Shift+ -)

Technorati Tags:


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

digg for developers

I was thinking the other day that there should be a digg-like site that is only for software developers.

Well, it turns out there is! I was analyzing my web traffic and the list of recent referrers. A few were coming from dzone. Apparently, somebody has been submitting some of my articles to this site (thank you!). The site works in much the same way as digg, but the subtopics are programming-related. You can vote up or down various posts. It doesn’t look like it has anywhere near the traffic that digg has, but hopefully it continues to grow.

So if you’re a developer and like to stay on top of new links and articles, get yourself an account on dzone and start contributing and voting.

Technorati Tags: , , ,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order:

Thoughts on Writers Guild of America Strike

This strike is a big deal, but it’s much bigger deal for Hollywood, the producers and writers than for the rest of us. I think they are facing some specific dangers that they had better think about before they drag on too long. The actual points the union and producers are arguing about are probably valid, some on both sides, but whatever–that’s irrelevant for my point. For the viewing public–I don’t think we’ll notice or care as much.

I’m thinking about hockey–they used to be the #4 sport, then they had a strike. Now, more people watch POKER on ESPN than hockey.

And airlines–when their workers go on strike, I think it definitely hurts the airline as a whole, because in the end we the people don’t get our service. We choose something else. And we might not come back.

I think Hollywood needs to examine some facts: TV viewership is going down, way down. There are only a handful of shows that really do well. The 90% of the others will be what’s hurt by this strike. People will come back to Leno, the Daily Show, The Office, and the Simpsons, etc. The other ones are in danger. TV has very stiff competition from the Internet, DVDs, video games (video game industry is MUCH larger than Hollywood), satellite radio, YouTube, ipods, and tons of other little distractions. Sure, lots of online content is derived from traditional content, which comes from Hollywood, but much of it is user-generated or at least independent. If TV goes out, it’s so easy for people to find other avenues for entertainment. I wonder if the same writers that are striking will lose their jobs a week after they get back to work when their shows are canceled because nobody’s watching them anymore. (Of course, they’ll just move on to the next project, but it’s still disruptive.)

I’m also curious (with any union)–how many of the members are really happy with how the union does things. I really don’t have any idea–but at some point a union might not be the best way to do business, especially in today’s global economy.

The strike by itself isn’t anywhere close to enough to doom the TV/movie industry, but put together with all of these other forces, they could be in real trouble. Meanwhile, we’ll just look for the next big star on YouTube rather than Hollywood… (more people watch Ask a Ninja than some TV shows, after all)

Technorati Tags: , , , , , , ,


Check out my latest book, the essential, in-depth guide to performance for all .NET developers:

Writing High-Performance.NET Code, 2nd Edition by Ben Watson. Available for pre-order: