search
top

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

1
commentData.Insert(cn);

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

1
2
3
4
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
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

3 Responses to “Procedure Like Object Oriented Programming”

  1. Cliff Mees says:

    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.

  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;
    }
    }

  3. grr it was suppose to be IRepository

Leave a Reply

Your email address will not be published. Required fields are marked *

top