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

  • Home
  • SEARCH
  • 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 7697363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:59:41+00:00 2026-05-31T21:59:41+00:00

I am trying to find the right way to write code regarding checking for

  • 0

I am trying to find the right way to write code regarding checking for the invalid values. Invalid value, in my case, would be null. The thing with other questions in SO is that they fall under specific circumstances and I am interested in a more general solutions.

I have a code like this:

public class SomeClass
{
    private readonly object m_internallyUsedObject;
    private ThirdPartyObject m_user; // Third party object.

    public SomeClass(object internallyUsedObject)
    {
        m_internallyUsedObject = internallyUsedObject; // We just want to ensure that the object will remain the same throught the life time of SomeClass object.
        m_user = new ThirdPartyObject(); // This object is not yet needed here.
    }

    public void DoSomething()
    {
        m_user.DoSomethingElse(m_internallyUsedObject); // Now we're using it and we are not sure whether null value is tolerated.
    }
}

Since we take the internallyUsedObject in constructor, we probably know the semantics of this object and how it should be used. On the other hand, we just relay this object to a third party object during calls.
Our SomeClass object will work just fine regardless of whether the value is null or not.

Now, the problem is that we do not know whether null will always work for the ThirdPartyObject – it might work in one version (in which case it’s OK to omit null check) and do not work in another.

One could say that we should not care for checking for null if our class can handle it. But when I write the code documentation, I would like to tell the user the behavior and expectations of our own class.

As I mentioned above, this simple check might not be useful or even invalid in particular third party object’s versions:

if (internallyUsedObject == null)
{
    throw new ArgumentNullException("internallyUsedObject");
}

Is it valid according to the OOP to take the internallyUsedObject from the code above inside the constructor when we are not going to use it directly? Doesn’t it violate the “fail fast” principle, since it might appear that we are just deferring the problem to the later stage of the object’s life time?

  • 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-31T21:59:43+00:00Added an answer on May 31, 2026 at 9:59 pm

    As a rule I’ve tended to use the method you’ve described in the second code block, but in combination with sensible groupings of checks. By this I mean that certain checks for particular methods should be performed and actions taken before an logic is performed e.g.

    public void MyMethod(MyObject input) {
    
        // 1. perhaps security checks, if an in method security is required
        if(security.AccessLevel < requiredLevel)
            throw new CustomSecurityException("Insufficent Access");
    
        // 2. data checks
        if(input.Property == null)
            throw new ArgumentNullException("Input didn't contain the value I wanted");
    
        if(input.Property2 < someImportantLevel)
            throw new CustomBLException("Input didn't meet required level for something");
    
        // 3. perform BL
        // ...do whatever
    
    }
    

    It’s probably not ideal and often things like method attributes could be used for the security side of things but in quite a few of our apps this has seemed like a sensible grouping of checks. You know that all the checks are done up front rather than lengthy blocks like this:

    if(input != null) {
        //do logic
    } else {
        throw new Exception();
    }
    

    Where the checks could be hidden or nested and harder to locate.

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

Sidebar

Related Questions

I am trying to find the right way to get the data from a
I'm trying to find a way to perform an indirect shift-left/right operation without actually
I have searched left and right. And I am trying to find a script
Trying to find the best way of create an overlap/overlay layer to fill the
Trying to find a way to send a POST HTTPS request from Python to
I am trying to write some code that will open a file, read its
I'm trying to write some code to look through an image file for groups
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to find the sqlserver adapter for rails on windows. I have tried getting
Trying to find an XML file I can use in lieu of a look-up

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.