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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:47:26+00:00 2026-05-11T15:47:26+00:00

Where do you check if an object that you are passing to a method

  • 0

Where do you check if an object that you are passing to a method is null or not?

Should an object need to be tested before calling a method? or within the method that is using the argument?

public class Program {     public static void Main(string[] args)     {         // Check if person is null here? or within PrintAge?          PrintAge(new Person { Age = 1 });     }      private static void PrintAge(Person person)     {         // check if person is null here?          Console.WriteLine('Age = {0}', person.Age);     } }  public class Person {     public int Age { get; set; } } 

Having a ‘null’ check in both classes seem to be too much redundant code.

[EDIT]: What would be an dis/advantage of checking for null within a caller or a callee?

[EDIT2]: I just ran into Defensive Programming and it seems like it advocates checking null within a callee. I wonder if this is a widely accepted practice.

  • 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. 2026-05-11T15:47:27+00:00Added an answer on May 11, 2026 at 3:47 pm

    You can design a method to work with valid objects only.

    That means you are expect to receive valid objects ( not null in your case ).
    That means you don’t know how to react and what to do with invalid objects:

    • returning silently from the function is not a solution;
    • throwing an exception will mean you move responsibility to the upper methods where they can check the value already before passing to you.

    So if your method don’t know exactly how to handle invalid object and the method won’t follow additional logic in the invalid case you should put

    Debug.Assert( Person ); 

    at the PrintAge begin and this will force you to make checks upper by call stack.

    The lower function in hierarchy is the less checks it should do. The following is disadvantages of doing checks in the functions that do the work.

    • Function that is doing actual work has to be as clear as possible without mass of ifs
    • Function will be called more then many times
    • Such function can call such functions and they can call such functions again. Each of them will perform the same validation
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, $_FILES is a superglobal, which means that it's accessible… May 12, 2026 at 8:44 pm
  • Editorial Team
    Editorial Team added an answer The trick is to make m// work in list context… May 12, 2026 at 8:44 pm
  • Editorial Team
    Editorial Team added an answer I think it would be better and simpler to test… May 12, 2026 at 8:44 pm

Related Questions

My program has a component - dubbed the Scheduler - that lets other components
I recently asked a question about functional programming, and received (good!) answers that prompted
I have ObservableCollection<Foo> that is bound to an ItemsControl (basically displaying a list). Foo
Currently I'm programming a database class which makes a little bit use of PHP's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.