While I Compile

… I compile my thoughts about programming

Procedure Like Object Oriented Programming

In a previous post What’s wrong with the Nouns/Adjective/Verb object oriented design strategy, I talked about how verbs should be implemented in their own separate class instead of as a method strapped onto an entity class. In my opinion, it’s an appropriate way to work with processes and pass those processes around, while keeping code flexible, testable, and highly maintainable.

But it has led to comments on Twitter and a link to one of Steve Yegge’s post Execution in the Kingdom of Nouns. Basically, Steve said that turning verbs into nouns was a bad idea (at least that’s what I think he was getting at, there were a lot of metaphors in there :-).

It’s easy to see Yegge’s point of view, if you just leave it at that. After all turning your single line of code accessing those actions

commentData.Insert(cn);

into multiple lines of calling code, when you move the logic into its own class,

using (CommentInsertCommand insCmd = new CommentInsertCommand(cn))
{
    insCmd.Execute(commentData);
}

definitely sucks.

So why not add a static method to the process class so you can access it with a single, procedural like, call?

public class CommentInsertCommand : IDisposable
{
    ....

    public static void Execute(CommentData commentData, int userId, SqlConnection cn)
    {
        ValidateCommentParameter(commentData);
        using (CommentInsertCommand insCmd = new CommentInsertCommand(cn))
        {
            commentData.CommentId = insCmd.Execute(commentData, userId);
        }
    }

    protected static void ValidateCommentParameter(CommentData commentData) {...}
}

This way your call is reduced to

CommentInsertCommand.Execute(data, cn);

I think this has merit and is a clean way to manage your classes.

It brings your object oriented code back to a more procedural level.

One problem I haven’t quite figured out yet is the naming. To be honest, I’m a little uneasy about it. I should probably name it ‘Insert’, but that’s redundant with the class name and I’m not crazy about naming it ‘Execute’ or ‘Run’ either. I chose ‘Execute’ in this example so all XXXXCommand classes would be consistent across the application, and the name is consistent with the SqlCommand naming which is important since this class kind of emulates SqlCommand. However, I’d still love to find a better name.

So, the bottom line is this; why not give all your process classes a procedure like entry point?

Why not give more of our object oriented code a procedural language feel?

Copyright © John MacIntyre 2010, All rights reserved

About these ads

September 10, 2010 - Posted by | C#, Code, Programming

3 Comments »

  1. Interesting points, and I like the consistent “Execute” naming. Would the static methods be thread-safe though?

    Although it takes more code, I like to use some form of Joshua Bloch’s “Builder Pattern” (see point 2 in “Effective Java” – http://java.sun.com/docs/books/effective/) to set up my command class with a cleaner API that optionally allows me to create an instance and execute it in one readable line.

    Comment by Cliff Mees | September 12, 2010

  2. here’s what i came up with doing just a bit of TDD to start with.

    using System;
    using Ninject;
    using NUnit.Framework;

    namespace Comments
    {
    [TestFixture]
    public class PostTests
    {
    [Test]
    public void CommentingOnAPost()
    {
    IKernel iocContainer = new StandardKernel();
    iocContainer.Bind<Persistance>().To<Persistance>();

    var comment = new Comment();
    comment.PostId = 1234;
    comment.Text = “Good post!”;
    comment.TimeStamp = new DateTime(2010, 9, 26);
    comment.CommenterId = 5555;

    var persistance = iocContainer.Get<Persistance>();
    persistance.Save(comment);
    }
    }

    public class Persistance
    {
    private IRepository _repository;

    public Persistance(IRepository repository)
    {
    _repository = repository;
    }

    public void Save(T instance)
    {
    if (_repository.Contains(instance))
    {
    _repository.Update(instance);
    }
    else
    {
    _repository.Insert(instance);
    }
    }
    }

    public interface IRepository
    {
    bool Contains(T instance);
    void Update(T instance);
    void Insert(T instance);
    }

    public class Comment
    {
    public int CommenterId;
    public long PostId;
    public string Text;
    public DateTime TimeStamp;
    }
    }

    Comment by james peckham | September 26, 2010

  3. grr it was suppose to be IRepository

    Comment by james peckham | September 26, 2010


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: