I think you are mixing two things in your post, the API and the implementation. In both cases I don’t think there is a strong rule you can use all the time, but you should consider these two things independently (as much as possible).
Let’s start with the API, both:
public void setHint(boolean isHintOn)
and:
public void setHintOn() public void setHintOff()
are valid alternatives depending on what your object is supposed to offer and how your clients are going to use the API. As Doc pointed out, if your users already have a Boolean variable (from a UI control, a user action, an external, API, etc) the first option makes more sense, otherwise you are just forcing an extra if statement on the client’s code. However, if for example you are changing the hint to true when beginning a process and to false at the end the first option gives you something like this:
setHint(true) // Do your process … setHint(false)
while the second option gives you this:
setHintOn() // Do your process … setHintOff()
which IMO is much more readable, so I will go with the second option in this case. Obviously, nothing stops you from offering both options (or more, you could use an enum as Graham said if that makes more sense for example).
The point is that you should pick your API based on what the object is supposed to do and how the clients are going to use it, not based on how you are going to implement it.
Then you must choose how you implement your public API. Let’s say we picked the methods setHintOn and setHintOff as our public API and they share this common logic as in your example. You could easily abstract this logic through a private method (code copied from Doc):
private void setHint(boolean isHintOn){ this.layer1.visible=isHintOn; this.layer2.visible=!isHintOn; this.layer3.visible=isHintOn; } public void setHintOn(){ setHint(true); } public void setHintOff(){ setHint(false); }
Conversely, let’s say we picked setHint(boolean isHintOn) a our API but let’s reverse your example, due to whatever reason setting the hint On is completely differente to setting it to Off. In this case we could implement it as follows:
public void setHint(boolean isHintOn){ if(isHintOn){ // Set it On } else { // Set it Off } }
Or even:
public void setHint(boolean isHintOn){ if(isHintOn){ setHintOn() } else { setHintOff() } } private void setHintOn(){ // Set it On } private void setHintOff(){ // Set it Off }
The point is that, in both cases, we first picked our public API and then adapted our implementation to fit the chosen API (and the constraints we have), not the other way around.
By the way, I think the same applies to the post you linked about using a boolean parameter to determine behavior, i.e. you should decide based on your particular use case rather than some hard rule (though in that particular case usually the correct thing to do is break it in multiple functions).
setHint(boolean isHintOn)as a private method, and add publicsetHintOnandsetHintOffmethods that respectively callsetHint(true)andsetHint(false).setHint(true|false). Potato potahto. At least use something likesetHintandunsetHint.isat the beginning.isValidetc. So why change that for two words? Besides, "more natural" is in the eye of the beholder. If you want to pronounce it as an English sentence, then for me it would be more natural to have "if the hint is on" with a "the" tucked in.