RSS feed blog search engine
 

Peter Hallam's WebLog  
Released:  3/7/2009 11:46:23 AM  
RSS Link:  http://blogs.msdn.com/peterhal/rss.aspx  
Last View 2/4/2012 2:28:05 PM  
Last Refresh 2/5/2012 3:17:48 AM  
Page Views 226  
Comments:  Read user comments (0)  
Report violation Report a violation or adult content
Save It  



Description:



C# Compiler


Contents:

Peterhal - Last day at Microsoft

I've been promising myself an extended vacation for a long time now, but working at Microsoft has been so much fun I've kept postponing it. Well, this is it - my last day at Microsoft.

 

Feel free to email me at 'hotweird' at hotmail.




What Makes a Good Programmer?

I just read two salon articles about Scott Rosenberg's new book Dreaming in Code. His thesis is that "programing is hard" and uses the experiences of the development of the Chandler project to frame the discussion. Scott identifies a common problem in large software development - inconsistent terminology use across the software development team. At the same time he complements the members of the development team as being talented programmers. These two ideas struck me as incongruous. How can a bunch of "ace programmers" end up with such a basic problem? Should programmers encountering this problem really be deserving of the accolade "ace"? And what skills would I expect from an "ace" programmer anyhow?

Before I go any further, I want to make it clear that I don't want to talk down Scottt or the Chandler team. Scott makes a number of great points that reflect my own experiences. Virtually every large software project I've seen has had problems akin to those Scott describes. Especially every large project I've worked on. I'm certainly as guilty of these difficulties as any programmer. But the examples that Scott describes are good grist for discussion about software development.

When people talk about talented programmers they are usually refering to their ability to write correct code, whch solves problems quickly and elegantly. I suspect that that is what Scott was implying by describing the Chandler team as ace programmers. Raw coding skills vary widely across individuals. When it comes down to it some programmers are just more talented than others at writing code. Large team projects - projects with more than 3 programers with schedules of more than a few months - require a whole set of skills beyond raw coding. On large teams, excellent coding skills are a prerequisite. Alone, they qualify you as a beginner, not an ace.

From Beginner to Good

For me, the mark of a good team programmer is someone who does not suggest rewriting everything from scratch. Scott makes the very valid point that programmers like to program - and that they don't like to understand code written by others. Truer words were never spoken. Time and time again I've seen smart novice programmers suggest rewriting code produced by other programmers. I've noticed an inverse relationship betwen the esteem I hold for a programmer and the number of times I've heard them suggest a rewrite of a large software project.

Good team programers are capable and willing to work with a codebase that is too large for a single person to rewrite within the project's schedule. They recognize that the existing codebase has shortcomings and they have the maturity and discipline to know that attempting to 'fix' everyone of those shortcomings will almost certainly kill the project. Instead they identify those areas which must be improved on to deliver the project, and they have the skills to make significant changes to a large and ugly (because every large piece of software is ugly - but that's another story) codebase without causing the whole thing to destabilize.

And From Good To Great

If the mark of a good team programmer is the ability to work (usually grudgingly) with the code of others, then the mark of a great team programer is the ability to produce code that other programmers on the team will gladly use. Programmers are the most fickle sort, and if you can produce code that other, less skilled, coders will use without suggesting a rewrite - then you've elevated yourself to the top of the programing heap. Great programmers produce code that is so good that it will prevent the cross programer problems that plague most large software projects.

 




Code Review: Double Checked Locking Code

A friend of mine recently sent me some code to review:

 

Hi Peter,

Do you have any suggestions on how to clean up this code?

In particular the creation of a particular singleton type doesn't look very pretty in this model:

 

private DoubleCheckedLock<SingletonClass> myInstance = new DoubleCheckedLock<SingletonClass>();

public SingletonClass Current {

    get {

        return this.myInstance.GetSingleton(

            delegate() {

                return this.CreateSingleton(OrderContext, "third");

            }

        );

    }

}

 

Im sharing my comments on the code, as it demonstrates some important design principles. Heres the original code:

 

public class DoubleCheckedLock<T> where T : class

{

    private volatile T myInstance;

    private static readonly object lockObject = new object();

 

    public T GetSingleton(CreateObject c) {

        if (this.myInstance == null) {

            lock (lockObject) {

                if (this.myInstance == null) {

                    this.myInstance = c();

                }

            }

        }

        Debug.Assert(this.myInstance != null);

        return this.myInstance;

    }

 

    public delegate T CreateObject();

 

}

 

public abstract class DoubleCheckedLock2<T> where T : class

{

    protected abstract T CreateObject();

    private DoubleCheckedLock<T> myInstance = new DoubleCheckedLock<T>();

 

    public T Instance {

        get {

            return this.myInstance.GetSingleton(this.CreateObject);

        }

    }

}

 

And here are my comments:

 

  • The name DoubleCheckedLock describes the implementation. Names should reflect how the client will use a symbol, not how something is implemented. For clients, the class implements a lazily created, thread safe singleton object. Aka, a Singleton object that does the right thing. Naming the class 'Singleton' will make client code read much better.

 

  • I'm not a fan of public nested types, so I'd recommend moving the CreateObject delegate to a top level type. That's a style choice, but I've found it to wear well in practice.

 

  • Names of types, including delegate types, should be nouns, not verbs or verb phrases. CreateObject should be renamed Creator.

 

  • Passing in the creator delegate is necessary for this pattern so don't sweat it. However, passing in the delegate to the 'GetSingleton' method allows for some surprising behavior. It allows different calls to 'GetSingleton' to supply different values which will surely cause confusion. This confusion can be designed out by passing in the creator delegate as an argument to the Singleton constructor.

 

  • Once the GetSingleton parameter is removed, it can become a property named 'Value'. Again, this makes your client code read better.

 

  • Add the 'sealed' and 'readonly' modifiers unless you are specifically expecting to use a class as a base or modify a field after construction. The additional modifiers self document the code, and make you think through your design more fully.

 

  • The DoubleCheckedLock2 abstract base class is a classic example of inheriting for containment - representing a 'has a' relationship between the derived class and the DoubleCheckedLock2 base class. Inheritance should always represent an 'is a' relationship, never a 'has a' relationship. You will save yourself many headaches by avoiding base classes which force you to use your one precious base class to represent a 'has a' relationship. Get rid of the DoubleCheckedLock2 class. The fact that it has such a crummy name, and that there is no good name to give this thing is a clear indication that something is wrong with it.

    Home  
 
 




Privacy Policy