4

Lets say I have a string variable FirstName. FirstName can be null. I need to know if FirstName exists or not.

Is it better to just use one variable: FirstName and if FirstName is empty assume that it doesn't exists

OR

Create two variables:
Boolean HasFirstName and String FirstName

The benefits of this is I know positively that FirstName doesn't exist but on the down side it creates two variables.

asked Sep 23, 2015 at 19:12
1
  • 4
    Some languages make a distinction between firstName == null and firstName == "" Commented Sep 23, 2015 at 19:18

1 Answer 1

7

I would argue against both choices and would use an option type instead. An option type either contains no value or, or in this case, a string.

Many languages, especially functional ones, have such types built-in. Even if your chosen language doesn't support it directly, an Option<T> is a simple type to create.

answered Sep 23, 2015 at 19:25
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.