13 Replies - 293 Views - Last Post: 27 July 2018 - 10:55 AM

#1 Peter O   User is offline

  • D.I.C Regular

Reputation: 131
  • View blog
  • Posts: 309
  • Joined: 19-October 13

Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:19 AM

Sometimes there are good words to describe two mutually exclusive states. Let's say I have an object that can be shown on the screen. I might give it a show() function to make it visible, and a hide() function to make it hidden. Now to my question, should I provide both is_visible() and is_hidden() or only one one of them? How do I decide?

Note that I'm not talking about pure negations like is_empty() and is_not_empty(). This would be kind of useless because we can often just use the negation operator.

Is This A Good Question/Topic? 0
  • +

Replies To: Should an interface provide opposite of bool functions?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14685
  • View blog
  • Posts: 58,683
  • Joined: 12-June 08

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:21 AM

In what state would "is visible" and "is hidden" be the same value?
Was This Post Helpful? 0
  • +
  • -

#3 Peter O   User is offline

  • D.I.C Regular

Reputation: 131
  • View blog
  • Posts: 309
  • Joined: 19-October 13

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:25 AM

Never. They are the opposite. What I meant was that the two states are mutually exclusive with respect to each other.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2704
  • View blog
  • Posts: 10,800
  • Joined: 03-December 12

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:29 AM

What are you actually trying to focus on? From my review

show() - is an action and should do something
is_visable() - is a state to check

along with the their opposites. So, if there needs to be a state check, then yes.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14685
  • View blog
  • Posts: 58,683
  • Joined: 12-June 08

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:33 AM

Seems reasonable if the states are never the same then one variable would take care of it.
Was This Post Helpful? 0
  • +
  • -

#6 Peter O   User is offline

  • D.I.C Regular

Reputation: 131
  • View blog
  • Posts: 309
  • Joined: 19-October 13

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:46 AM

My focus is on is_visible() and is_hidden(), and if I should provide both, because only one of them is needed.

If I only have is_visible() and I want to check if the object is hidden I can just do !is_visible().
If I only have is_hidden() and I want to check if the object is visible I can just do !is_hidden().

Is it confusing to have both? I think it could make code cleaner but perhaps some people might wonder if they are two independent properties?

If I only have one of these functions, how do I decide which one? In this case I would probably go for is_visible() because it feels like most "positive", but how would one make this decision in general? Any guidelines?
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14685
  • View blog
  • Posts: 58,683
  • Joined: 12-June 08

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:48 AM

If you have both then yes.. folk will assume that.

If you are unable to pick a name flip a coin. You are over analyzing this to a bad degree.
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2706
  • View blog
  • Posts: 4,314
  • Joined: 21-June 11

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:49 AM

View Postmodi123_1, on 27 July 2018 - 06:33 PM, said:

Seems reasonable if the states are never the same then one variable would take care of it.


I don't think OP considered having two variables, but rather two methods where one returns the variable and the other returns the negation of the variable. Just like hide() and show() wouldn't modify different variables, but rather set the same variable to opposite values.

This post has been edited by sepp2k: 27 July 2018 - 09:50 AM

Was This Post Helpful? 1
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6657
  • View blog
  • Posts: 22,740
  • Joined: 05-May 12

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 09:51 AM

Having been burned in the past by IsDisabled or ShouldNotLoadLatestCLR and other negatives, my recommendation is always choose a descriptive name that is positive. Yes, it's possible that 95% of the time you'll be checking for !IsEnabled and !ShouldLoadLatestCLR to the point that a programmer may accidentally omit the !, but I still stand by my recommendation to prefer the positive over the negative descriptive name.
Was This Post Helpful? 3
  • +
  • -

#10 Peter O   User is offline

  • D.I.C Regular

Reputation: 131
  • View blog
  • Posts: 309
  • Joined: 19-October 13

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 10:10 AM

Yes, these are just functions. The underlying data is not relevant for the discussion.

Sometimes there is no obvious positive. I guess I could flip a coin as modi said. ;)

Other examples:
  • is_sleeping() / is_awake()
  • is_moving() / is_stationary()
  • is_male() / is_female()
  • is_dead() / is_alive()


I appreciate your opinions. I don't mind more opinions though, even if they are all agreeing, because it will help me make up my mind.
Was This Post Helpful? 0
  • +
  • -

#11 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2704
  • View blog
  • Posts: 10,800
  • Joined: 03-December 12

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 10:21 AM

I would drop the "is_" it IS unnecessary. Just go with bool visible(), many are already use to it.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11374
  • View blog
  • Posts: 19,406
  • Joined: 19-March 11

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 10:26 AM

View PostSkydiver, on 27 July 2018 - 11:51 AM, said:

Having been burned in the past by IsDisabled or ShouldNotLoadLatestCLR and other negatives, my recommendation is always choose a descriptive name that is positive. Yes, it's possible that 95% of the time you'll be checking for !IsEnabled and !ShouldLoadLatestCLR to the point that a programmer may accidentally omit the !, but I still stand by my recommendation to prefer the positive over the negative descriptive name.


/hammers on the + button like a rat in a behaviorist's training box

View PostPeter O, on 27 July 2018 - 12:10 PM, said:

  • is_male() / is_female()


Be careful with that one, it's 2018 now. We had to deal with gender in our data model, and in the simplest approach we could come up with it wound up having to account for five different states. There was male and female, plus "other" and "prefer not to state", and an "unknown" state for users who had not yet been through the configuration step and set an actual state. (there is no way for a user to set their value to "unknown")
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 934
  • View blog
  • Posts: 3,740
  • Joined: 13-June 14

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 10:40 AM

I don't think it's such a problem. Sometimes it's more natural to express things one way or the other and having the convenience of both is nice. Scala's collections, for example, typically have isEmpty and nonEmpty methods.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6657
  • View blog
  • Posts: 22,740
  • Joined: 05-May 12

Re: Should an interface provide opposite of bool functions?

Posted 27 July 2018 - 10:55 AM

Sometimes, you do need the an alternate form. For example, IEnumerable<T>.Count(). Once could naively determine if there is at least one item by using IEnumerable<T>.Count() > 0, but realize that Count()] will go iterate over all the items first to return the actual count. So this is when you'll end up adding a custom IEnumerable<T>.Any() which will return true if there is at least one item. But then the question comes full circle: do you need IEnumerable<T>.Empty() as well as IEnumerable.Any(), or do you simply tell users to always use !IEnumerable<T>.Any() ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1