As it's Friday I thought - let's do some cleaning up. The following exercise is refactoring a class from an app I'm maintaining at the moment. The app's fine (functional etc), and was originally a .NET 1.1 build. A combination of this, a previous, straight conversion to .NET 2.0 (with the minimum of mods) and a variety of historical developers (mostly junior) means that the code has a certain amount of 'odour' in numerous places.
Lack of consistency isn't the main problem, it's more about doing things smartly rather than taking the path of least resistance with your current knowledge. The latter generally means you don't learn much, get much satisfaction or pride in your work, and you may end up a tad confused as to why you always end up with the 'less glamourous' projects.
Here's the original code. A simple collection class containing custom MenuItem objects.
OK - Here's the issues I see with the code.
The MenuItems class is used in a few places:
The first thing is to say...
The following is now in the Menu Control:
Fortunately Visual Studio's find and replace can cope with all of this as the code is largely consistent...
A quick compile should show all the patterns (from the errors) if there's any inconsistencies. There were probably about 200 references to patch here with about 4 different variations of naming and scope - but it took about 5 minutes to patch (including writing this text )
We then have to patch all the 'Item' references. These are almost always setting a 'selected' property (Stopwatch on...)
As there's only a few references here (about 20), I could patch them manually, but this could be done entirely by using regular expressions to do the find and replace. http://msdn.microsoft.com/en-us/library/2k3te2cs.aspx shows all the options available. I tend to only use regexp for large volume stuff where I get a return on my time investment to work out the expression!
I'm just doing it in two steps without regexp as follows:
The code
becomes
For extra points you then use Resharper to rename all of the obj and pal references to something reasonable, e,g, objMenuItem becomes menuItem.
Oh - and use Resharper's 'Find Usages' on the MenuItems class to confirm it can be deleted.
In hindsight I would have used Resharper to 'change signature' on the methods in MenuItems (to patch all the references first), then converted to the generic Dictionary. Could have been a bit quicker.
Hope this gives someone some courage to get in there and improve their code. I firmly believe you've just got to 'want' to do it and then you're biggest problem will be stopping yourself!