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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:40:14+00:00 2026-05-11T01:40:14+00:00

Suppose I have a method public Patient(int id) { —- } that returns Patient

  • 0

Suppose I have a method

public Patient(int id) {     ---- } 

that returns Patient object given an id.. I could define contract in 2 ways

  1. Method would return null if patient does not exist
  2. Method would throw an exception if patient does not exist. In this case I would also define a query method that returns true if the Patient exist in the database or false otherwise…

Which contract should I use? Any other suggestions?

Update: Please comment on this case too… If it is not an database assigned Id and it is something a user enter in UI.. like SSN .. then which one is better..

Comment about Null pattern from Steve that I think is valid: probably not a good idea here, as it would be really useful to know immediately when an ID did not exist.

And I also think Null pattern here would be somewhat heavy weight

Comment from Rob Wells on throwing exception because its bad Id: i don’t think a typo in a patient’s name is an exceptional circumstance’ IMHO

  • 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-11T01:40:15+00:00Added an answer on May 11, 2026 at 1:40 am

    Keep in mind that going ‘over the wire’ to another tier (whether a database or an application server) is one of the most expensive activities you can do – typically a network call will take several orders of magnitude longer than in-memory calls.

    It’s therefore worth while structuring your API to avoid redundant calls.

    Consider, if your API is like this:

    // Check to see if a given patient exists public bool PatientExists(int id);  // Load the specified patient; throws exception if not found public Patient GetPatient(int id); 

    Then you are likely to hit the database twice – or to be reliant on good caching to avoid this.

    Another consideration is this: In some places your code may have a ‘known-good’ id, in other places not. Each location requires a different policy on whether an exception should be thrown.

    Here’s a pattern that I’ve used to good effect in the past – have two methods:

    // Load the specified patient; throws exception if not found public Patient GetExistingPatient(int id);  // Search for the specified patient; returns null if not found public Patient FindPatient(int id); 

    Clearly, GetExistingPatient() can be built by calling FindPatient().

    This allows your calling code to get the appropriate behaviour, throwing an exception if something has gone wrong, and avoiding exception handling in cases where it is not needed.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 74k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Many projection libraries, such as Proj.4 and libraries using it… May 11, 2026 at 2:12 pm
  • added an answer You need to remove the event listener, or you can… May 11, 2026 at 2:12 pm
  • added an answer using System.Net.NetworkInformation IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); TcpConnectionInformation[] tcpInfoList = properties.GetActiveTcpConnections(); May 11, 2026 at 2:12 pm

Related Questions

Suppose I have a method public Patient(int id) { ---- } that returns Patient
Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose I have a method that takes an object of some kind as an
Suppose that I have a Java class with a static method, like so: class
I am using java language,I have a method that is supposed to return an
Suppose I have a number of related classes that all have a method like
This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
I have a function that I use to add vectors, like this: public static

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.