A method that causes a conditional return of a method that calls it?

Well, it may be a bit of a hack, but carry with me :) The background is that I'm tired of the methods, which some if-statements will interfere with the indentation for the whole method, for example:

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

So, I was wondering if this would be tidy if I could do something like this:

 public SomeClass DoStuff(string inputStr) { Require(IsOpenFilter(inputStr),null); .... return result; } 

This case may be covered by code contracts in one form or another, if so, please correct me :)

The idea is that if the operator does not evaluate true, it returns null. If there was no return type for the method, it would be simple: Require (IsOpenFilter (inputStr));

So, I think there are two questions, can this be somehow done? I do not understand how to make a conditional return from a method call.

Another question: is this a good idea? It's a little strange when monkeypatch is in this language, but I would like the code to read. I would be even cleaner if it could be added to the attribute above the method: [Require (IsOpenFilter (inputStr))]

+4
source share
2 answers

I agree with Anders: invert the condition and return earlier. A "single point of return" was used to be valuable when you needed to explicitly do a bunch of resource cleanup at that point of return; with finally blocks, using statements and garbage collection this is not even a useful guide anymore, IMO. Every time you find yourself doing something to get a single point of return, consider whether this really makes the code more readable.

I almost always believe that if at some point in the code I know the return value and there are no other actions that I want to take as part of the method, it is best to return immediately. Forcing readers to view the rest of the method only if the other action is neither elegant nor useful, IMO.

I think it's good that the method cannot indicate that it should force the caller to return - this ability sounds like a nightmare of readability. Having said that, I reviewed the new conditional return statement:

 return? expression; 

This returns the return value of expression if it is not null; you can use it for types with a null value, for example:

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

Basically, it will be a return statement, equivalent to a zero-coalescing operator.

This will not help in your case, note - you want to return zero earlier ...

+6
source

How to rewrite your first example as follows:

 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 with one return point in each function. To make it short: having one return is a legacy of languages ​​with explicit resource handling. A long story is available in some old SO posts:

+3
source

Source: https://habr.com/ru/post/1305920/


All Articles