The completion function above in the hierarchy

I'm tired of writing:

if(objectA!=null) return; 

or

 if(objectB==null) return; 

So, I was hoping to cut this snippet into something like this:

 Returns.IfNull(objectA); 

it is very similar to the same length, but usually there are several objects for checking and adding params , since the parameter can be shortened:

 if(objectA==null || objectB!=null || objectC!=null) return; 

in

 Returns.IfNull(objectA,objectB,objectC); 

Basically, the IfNull function should access the function one step higher in the stack trace and terminate it. But this is just an idea, I do not know if this is possible. Can I find similar logic in some kind of lib?

+6
source share
5 answers

No, you are essentially asking the function to exit the function above itself, which is undesirable or really impossible if you do not throw an exception (which does not return by itself).

That way, you can either do your simple and succinct if-null-return checks, or what you might want to do there is to throw a well-defined exception, but I do not recommend exceptions for flow control. However, if these are exceptional (error) circumstances, then consider throwing ArgumentNullException () and handling it accordingly.

You could write some helper methods to throw ArgumentNullException () for you, of course, clean it up a bit:

  public static class ArgumentHelper { public static void VerifyNotNull(object theObject) { if (theObject == null) { throw new ArgumentNullException(); } } public static void VerifyNotNull(params object[] theObjects) { if (theObjects.Any(o => o == null)) { throw new ArgumentNullException(); } } } 

Then you could write:

 public void SomeMethod(object obj1, object obj2, object obj3) { ArgumentHelper.VerifyNotNull(obj1, obj2, obj3); // if we get here we are good! } 

But again, these are exceptions, not the β€œreturn” of the previous method on the stack, which is impossible.

+7
source

You are asking for something that only a language designer can fix. I suggested one thing. . the operator returns the current default value from the current method when the argument left to it is zero.

 return appSettings.?GetElementKey(key).?Value ?? ""; 

Perhaps we will see him someday in C # 6?

+1
source

To perform similar comparison checks, I once defined the following extension method:

 /// <summary> /// Returns whether the object equals any of the given values. /// </summary> /// <param name = "source">The source for this extension method.</param> /// <param name = "toCompare">The objects to compare with.</param> /// <returns> /// True when the object equals any of the passed objects, false otherwise. /// </returns> public static bool EqualsAny( this object source, params object[] toCompare ) { return toCompare.Any( o => o.Equals( source ) ); } 

It can simplify redundant checks, for example:

 string someString = "bleh"; bool anyEquals = someString.EqualsAny( "bleh", "bloeh" ); 

In your case, when you check for multiple null checks, you can use it like this:

 if ( EqualsAny( null, objectA, objectB, objectX ) ) return; 

On the other hand, your code reminds me of Code Contracts , which allows you to define the conditions for pre and post. If this is your scenario - perhaps not the way I don’t understand why you call return - this may interest you. Part of it is available for free in .NET 4.0.

+1
source

You cannot call another method and expect it to return to calling the current method (you could if we had something like a style to continue the transfer, alas, we do not).

You can say:

 if(new[] { objectA, objectB, objectC }.Any(x => x != null)) { return; } 

Or:

 if(new[] { objectA, objectB, objectC }.AnyAreNotNull()) { return; } 

Here is AnyAreNotNull :

 public static class EnumerableExtensions { public static bool AnyAreNotNull<T>(this IEnumerable<T> source) { Contract.Requires(source != null); return source.Any(x => x != null); } } 

But there is really nothing wrong with writing regular code for this situation.

0
source

No, the method cannot return the method described above.

Best of all, you can create a method that returns true if any of its parameters was null, and then if (ReturnHelper.AllNull(obj1, obj2, obj3)) return; but I would say that it is much less readable.

0
source

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


All Articles