Don Demsak

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
PatternExpert - Not a .Net Patterns Expert

I happen to be reading the current issue of Redmond Developers News and ran across the article, .NET Toolbox Picks: Part II by Leonard Lobel and Brian Schmitt.  In it they mention some of the usual must have 3rd party development tools (ReSharper, Reflector, RegexDesigner.NET, etc.), and one that I've never heard of, PatternExpert.  It sounded like it had some good potential, trying to add a little guidance on using software patterns, so I figured I'd download it and give it a whirl. 

There is no installer, just an executable, which would be fine if the product was open source or free, but at $79, I expect an installer.  First thing I noticed, no support for Visual Basic, which is unfortunate, since this is a major .Net language, and an area that needs some patterns guidance.  The product itself has a tree view listing 22 major patterns, and a wizard or Powerpoint-like frame, where the pattern is explained.  I like the style of the pattern documentation, and it seemed easy to understand.  But, when I got to the sample code, that is where things totally fell apart.

The first (and only) code I checked was for the Singleton Pattern.  The fact that there were 2 implementations of a Singleton, simple, and limited number (isn't Limited Number of instances a Multiton not a Singleton?) let me know that something was amiss here.  Then I looked at the Singleton Code.  Since the Code Samples where done in .Net 2.0, I expected the commonly accepted implementation (Jon Skeet has a great tutorial on this topic):

public sealed class Singleton
{
    static readonly Singleton instance=new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    {
    }

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

But instead the PatternExpert code looks like this:

namespace SingletonApp
{
                /// <summary>
                /// Singleton, with added reference count and release call
                /// </summary>
                public class zZ00singletonZz
                {
                                private static zZ00singletonZz     oInstance = null; // Single instance
                                private static      int iReferenceCount = 0; // Reference count

                                protected zZ00singletonZz()
                                {
                                }

                                // Create or get the singleton object
                                public static zZ00singletonZz getInstance()
                                {
                                                if (iReferenceCount == 0) // See if the object is around yet
                                                {
                                                                oInstance = new zZ00singletonZz();// Create the object
                                                }
                                                iReferenceCount++; // Increase the reference count
                                                return oInstance; // Pass the object back
                                }

                                // Release or delete the singleton object
                                public static void releaseInstance(zZ00singletonZz inst)
                                {
                                                // Exit if already deleted or if it is not our instance
                                                if (iReferenceCount == 0)
                                                                return;

                                                iReferenceCount--;// Decrement the reference count

                                                if (iReferenceCount == 0) // See if this is the last user
                                                {
                                                                // No one needs this object, garbage collection will delete it
                                                }
                                }

                }

That code is so bad, it is scary that it is in a tool designed to teach developers about how to write good code.  IMHO, it looks like something a C++ programmer that hasn't bothered to learn how to write managed code would come up with.  Reference Counters?  In Managed Code?

But, the tool did get me thinking that something like this (but done right), installed in the IDE would be cool.  Yeah, code snippets and the Guidance Automation Toolkit cover some of this area, but they don't quite make it.


Posted Tue, Nov 13 2007 12:34 PM by DonXML

[Advertisement]

Comments

Greg wrote re: PatternExpert - Not a .Net Patterns Expert
on Tue, Nov 13 2007 1:33 PM
From a quick look at it ... its not a bad thing ... They are just taking laziness a bit further. The singleton examples shown on skeet's blog consider the singleton to always be instantiated which depending on usage can be a good or a bad thing. This example allows the Singleton to be destroyed/reloaded. Each has their place... To me with such a tool I should be able to *say* which one I want though. On the bad side this Singleton implementation is not thread safe which can cause some pretty serious problems with the reference counting. In practice (if you follow best standards) I think this singleton will create/destroy a lot of objects. Just to be clear though this Singleton is a well known pattern ... whether or not it should be used really depends on what exactly you are trying to do. I would personally harp on their assumption that I want *this type of singleton*.
Jimmy Bogard wrote re: PatternExpert - Not a .Net Patterns Expert
on Tue, Nov 13 2007 2:55 PM
My favorite part is the Hungarian notation and the camelCase method names. I would love to see this code in action on a multi-threaded ASP.NET app. And couldn't "getInstance" check for null and lazy instantiate it? It doesn't look like the ref count is actually doing anything. And "releaseInstance" takes the instance as a parameter? Doesn't it already HAVE a reference in the private field? This is wacky, but nice WTF material. I think I would ask for my money back.
Bil Simser wrote re: PatternExpert - Not a .Net Patterns Expert
on Wed, Nov 14 2007 12:14 AM
I took a look at this thing a couple of months ago and then quickly uninstalled it as fast as I could after seeing what garbage it generated. Yeah, maintainable code is not on the high list of features for this tool. It gave me chilling reminders of Rational Rose and how it would mangle C++ code when you round-trip engineered UML diagrams. What a mess that was, this isn't much better. No, I can code up these patterns in a much better way than this tool does (and with a little work, just as fast).
Daily Dose of Links - 20071114 « Daily Geek Bits wrote Daily Dose of Links - 20071114 &laquo; Daily Geek Bits
on Wed, Nov 14 2007 8:49 AM
Pingback from Daily Dose of Links - 20071114 « Daily Geek Bits
Colin Basterfield wrote re: PatternExpert - Not a .Net Patterns Expert
on Wed, Nov 14 2007 4:12 PM
Hi, I agree that looks ugly, but a little while ago I found this http://www.yoda.arachsys.com/csharp/singleton.html This is well worth looking at. Regards Colin
DonXML wrote re: PatternExpert - Not a .Net Patterns Expert
on Wed, Nov 14 2007 7:34 PM

Colin, the link you gave, that is to Jon Skeet's site (which I linked to in the post).

Loren wrote re: PatternExpert - Not a .Net Patterns Expert
on Wed, Nov 28 2007 8:24 PM
Thanks for the post... You saved me the time of looking at the product. I'm no pattern expert either, but I do have enough exposure to know that the singleton code you posted is so bad I don't need to look any further.