0

Let's say I have a social media website. On this website, users can upload several pieces of information about themselves, including their phone number. To implement this, I have a class called PhoneNumber that represents the users phone number. But phone numbers are optional. I have this PhoneNumber class which is supposed to guarantee access to a users phone number, but it can't. How do I handle this?

My current approach is to, whenever there's a problem, assign a value to an error variable inside PhoneNumber that represents the problem in question (this could be an exception, a string, or an error code, it's irrelevant for this question). I also added a method to PhoneNumber called isPhoneNumberAvailable() that should be called before any of the accessors are used. If one of the accessors is called and the user hasn't added their phone number, I throw an IllegalStateException. But this doesn't seem like a good solution. I'm not sure why, I'm too new at this to explain it, but it seems like this could cause me some problems down the road.

Note that this question is language agnostic, the existence of an IllegalStateException in Java is purely coincidental.

EDIT: The solution is improving the model. I have guaranteed access to something I can't guarantee access to, which is bad design. My other problem was with the use of an error variable in class scope but that's beyond the scope of this question. See my follow up question here.

2 Answers2

1

I would just have the PhoneNumber property on the User object set to null. That way you can test if PhoneNumber == null, then the user can't call the phone number, so your UI doesn't even need to show the option (or show that there's no phone number).

Once phone number is provided, initialize an instance of the PhoneNumber class with the user's phone number and assign it the PhoneNumber property on the User object. The user's phone number can be validated the first time it is provided, and then it would be guaranteed to exist and be called. If the validation fails on the first attempt, it throws an exception during instantiation, and thus the PhoneNumber object is never created.

If at a later stage, you decided to have multiple phone numbers, either you can add more properties to the User (i.e. User.HomePhone, User.MobilePhone), or have a list of PhoneNumbers that is instantiated to empty list. As phone numbers are added, a new PhoneNumber object is instantiated and added to the list. Both these approaches will also guarantee that you never have a PhoneNumber unless it holds a valid number.

In fact, I have done something similar for a PhoneNumber class for multi-factor authentication and this approach worked fine for me.

Omer Iqbal
  • 3,264
0

I think what you're asking is where to handle possible errors, right? Among others you have the ways you described:

  1. The client asks if phone numbers are available. This has the benefit of a clean and exception-free API. But what happens if the client never asked before accessing the numbers?

  2. Your getter (within your PhobeNumber) throws an exception of nothing is available. The advantage here is that clients don't need two remember two API calls.

Both are valid ways to handle this and which you choose depends highly on your application and what you think should happen if no phone numbers are available. I, personally, prefer exceptions as you can't forget them but that is even a matter of style.

Is that what you were looking for?

jhr
  • 473