Question

Ok this might be a bit of hack but bear with me :) The background is that i'm tired of methods that that some if-statement to that messes up indention for the whole method, like:

public SomeClass DoStuff(string inputStr)
{
  SomeClass result =null;
  if (IsOpenFilter(inputStr))
  {
    ....
  }

  return result;
}

So I was thinking , wouldn't it be neat if I could do something like this instead:

public SomeClass DoStuff(string inputStr)
{
  Require(IsOpenFilter(inputStr),null);


  ....

  return result;
}

This case might be covered by code contracts in some form, if so please correct me :)

The idea is that if the statement does not evaluates to true it would return null. If there wasn't a return type for the method it would simply be: Require(IsOpenFilter(inputStr));

So I guess there's two questions, can this be done somehow? I'm stumped on how to do a conditional return from calling a method.

The other question is, is this a good idea? It's a bit weird to monkeypatch the language like this but I'd rather like the way the code reads. I would be even cleaner if it could be put into an attribute above the method: [Require(IsOpenFilter(inputStr))]

Was it helpful?

Solution

I agree with Anders: invert the condition and return early. "Single return point" used to be valuable when you needed to explicitly do a bunch of resource clean-up at that return point; with finally blocks, using statements and garbage collection it's not even a useful guideline any more, IMO. Any time you find yourself doing something just to get a single return point, consider whether it's actually making the code more readable.

I almost always find that if at some point in the code I know the return value and there are no other actions I want to take within the method, the best thing is to return immediately. Forcing readers to look through the rest of the method just in case there's another action is neither elegant nor useful, IMO.

I think it's a good thing that a method can't indicate that it should cause the caller to return - that ability sounds like a readability nightmare. Having said that, I have considered a new sort of conditional return statement:

return? expression;

This would return the evaluateed value of expression if it's non-null; you could use it for non-nullable types like this:

int Foo()
{
    return? Bar(); // Where Bar() has a return type of int?
    ...
}

Basically it would be the return statement equivalent of the null-coalescing operator.

This wouldn't help in your case, mind you - you want to return null early...

OTHER TIPS

What about rewriting your first example like this:

public SomeClass DoStuff(string inputStr) 
{ 
  if (!IsOpenFilter(inputStr)) 
      return null;

  SomeClass result =null; 

  // Some code .... 

  return result; 
} 

It looks like you are trying to stick to the old convention of one return point in each function. To make it short: Having one return is a heritage from languages with explicit resource handling. The long story is available in some old SO posts:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top