views:

222

answers:

4

I have the following code:

class Foo
{
    public Foo()
    {
     Size = true;
    }

    private bool _size;

    protected bool Size
    {
     get { _size; }
     set { _size = value; }
    }
}

class CrazyFoo : Foo
{
    public void First()
    {
     if (!Size)
      return;
    }

    public void Second()
    {
     if (!Size)
      return;
    }
    public void Finished()
    {
     if (!Size)
      return;
    }
}

What is the best way to implement this sort of pattern, as it drives me nuts to type

   if(!Size) return;

perhaps I can do it with attributes or AOP?

What is the best and simplest way?

Thanks

A: 

Maybe just use one method and an Enum with the values First, Second, Finished etc.? It's hard to tell because, apart from that one check, you don't say what is common. AOP could be a solution, but maybe not, since aspects are usually more general in their conceptional nature.

BTW, maybe choose a different naming for your samples in the future, this may offend some people. (Edited to match new naming)

Lucero
A: 

From a "pattern" standpoint, though, this doesn't seem onerous to me. It seems perfectly reasonable to me to type:

if(!Size) 
    return;

You're explicitly handling the cases you want. In your case, this check is pretty specific to what you are working with, from what I can tell (from your original + edits). I'd personally choose a more obvious name, since it does seem a little strange (even in your original), and not completely obvious what's happening.

Even with AOP, you'd be adding some other information here on each method, to make sure your aspect was handled.

Reed Copsey
A: 

I would not call it "simple" (you have the simplest way), but you could do it declaratively using the Validation application block in Enterprise Library.

JP Alioto
+1  A: 

If you have the same guard statement at the beginning of too many methods, you can create a method called executeWithGuard:

private void executeWithGuard(Action method)
{
    if (HeadSize) method();
}

Then you could do this:

public void ScreenFirstShot()
{
    executeWithGuard(() =>
    {
        // code here
    });
}

public void ScreenSecondShot()
{
    ExecuteWithGuard(() =>
    {
        // code here
    });
}
public void CrazyUp()
{
    ExecuteWithGuard(() =>
    {
        // code here
    });
}

There's no less code doing this... in fact, there's probably more code, but it does allow you to not have to do a find/replace if your guard condition ever changes. I'd only suggest it as a last resort, though. It's very possible that your real problem is that you're doing your validation too far down the call tree. If you can do it at a higher level, you may save yourself from all of this validation.

ALSO

Have a look at the null object patttern. This pattern can be used in some special cases to prevent or simplify state checking.

ALSO (rev 2)

It's hard to know what your intent is since the question focuses on a specific solution, but if you're executing these methods sequentially, you can look at using the strategy pattern, and putting the check in your base strategy class.

Michael Meadows