Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6151969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:50:52+00:00 2026-05-23T19:50:52+00:00

I am tired of writing: if(objectA!=null) return; or: if(objectB==null) return; So I was hope

  • 0

I am tired of writing:

if(objectA!=null) 
return;

or:

if(objectB==null) 
return;

So I was hope to shorten this snippet, to something like this:

Returns.IfNull(objectA);

it is pretty match the same length but usually there are few objects to check and adding params as parameter can shorten:

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

to:

Returns.IfNull(objectA,objectB,objectC);

Basically function IfNull have to get access to function one step higher in stack trace and finish it. But that’s only idea, I don’t know if it’s even possible. Can I find simililar logic in some lib?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:50:52+00:00Added an answer on May 23, 2026 at 7:50 pm

    No, you are essentially asking the function to exit the function higher than itself which isn’t desirable nor really possible unless you throw an exception (which isn’t returning per se).

    So, you can either do your simple and concise if-null-return checks, or what you may want to do there instead is to throw a well defined exception, but I don’t recommend exceptions for flow-control. If these are exceptional (error) circumstances, though, then consider throwing an ArgumentNullException() and handling it as appropriate.

    You could write some helper methods to throw ArgumentNullException() for you, of course, to 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 once again, this is exceptions and not a “return” of the previous method in the stack, which isn’t directly possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got tired of writing the following code: /* Commenting out irrelevant parts public
Recently I've gotten sick and tired of writing release notes and I'm really starting
I am writing an application which tests email addresses, for one part of this
I was writing some code today and something was not working as I expected.
Hey folks - I'm writing a pretty simple iPhone application. The data comes from
I post the notifications like this in an operation: DownloadStatus * status = [[DownloadStatus
I am re-writing this question to make it clearer what I need to do.
I've tried writing this code from scratch, coding, and running it but it just
Yesterday I activated compression on my website, like this: void context_BeginRequest(object sender, EventArgs e)
I'm writing a pretty small and overall simple app, and I want to watch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.