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 7962103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:14:23+00:00 2026-06-04T05:14:23+00:00

When should I guard against null arguments? Ideally, I would guard against null everywhere,

  • 0

When should I guard against null arguments? Ideally, I would guard against null everywhere, but that gets very bloated and tedious. I also note that people aren’t putting guards in things like AsyncCallbacks.

To keep from annoying other people with lots of unidiomatic code, is there any accepted standard as to where I should guard against null?

Thanks.

  • 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-06-04T05:14:24+00:00Added an answer on June 4, 2026 at 5:14 am

    One approach which I have used a lot is the null object pattern.
    For example, if a have a factory class which returns different implementations of an interface based on an argument, and the provided argument is not mapped to any of the implementations, I would return a NullObject,
    e.g.

       public interface IFoo{
             void Bar();
       }
       public class NullFoo{
           public void Bar(){
              //null behaviour 
           }
       }
       public class FooFactory{
            public IFoo CreateFoo(int i){
                  switch(i){
                      case 1:
                      return new OneFoo();
                      break;
                      case 2:
                      return new TwoFoo();
                      break;
                      default:
                      return new NullFoo();
                      break;
                  }
            } 
       }
    

    When I want get an IFoo from CreateFoo, I don’t have to check whether the returned object is null.

    Obviously, this is just one of the many approaches. There is no “one size fits all” since null can mean different things.

    Another way to guard against null arguments is to use CodeContract preconditions.
    e.g.

      public void Foo(Bar x){
          Contract.Requires<ArgumentNullException>( x != null, "x" );
          //access x
      }
    

    Using Code Contracts allows you to run static code analysis against your code and catch bugs such as Foo(null). (more here)

    One more why to do it would be to use a very simple generic extension method:

    public static class Ex
    {
        public static void EnsureNotNull<T>(this T t, string argName) where T:class
        {
            if(t == null)
            {
                throw new ArgumentNullException(argName);
            }
        }
    }
    

    Then you can check your arguments like this:

     public void Foo(Bar x, Bar y){
         x.EnsureNotNull("x");
         y.EnsureNotNull("y");
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Should model objects that go to the view be checked for null before going
Should we guard against unanticipated user input from dropdown lists? Is it plausible to
should be simple right? but have not found any anwsers :
Should functions that act on and object, say to fill it out from xml,
Should a highly dynamic website that is constantly generating new pages use a sitemap
I have a problem with a function that should only return the tail of
I believe that in properly coded systems - errors (as errors or exceptions) should
A Practical Introduction to GNU Privacy Guard in Windows recommends DSA and ElGamal, but
I use rspec-guard to run my tests continuously, but sometimes the tests fails because
We currently have a website that has user account functionality, but we are looking

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.