views:

110

answers:

6

If I have a method that hides a button, I would probably call it HideButton
If I have a method that shows a button, I would probably call it ShowButton

But what do you guys call a ShowIfThisHideIfThat style method?

Possible Choices:
TestForButtonVisibility (this kind of sounds like it will return true/false but not actually do the work)
TestButton
ShowHideButton (style I'm currently using)

Been at this a number of years and still don't have a style that I like for these types of methods. I'm working mostly in C# and a little Java, Ruby, and F#. What do you use for method names in this regard?

// example of the style of method
public void ShouldIShowOrHideButton()
{
  Button.Visible = ((chkSomeSetting.Checked) && (DateTime.Now.Day < 8));
}
+4  A: 

My preference has been to keep toggle methods rather than separate methods for hide/show.

ToggleButtonVisibility()

This allows you to put your testing code in there and the expected result/output would be a properly visible/invisible button.

Edit: Toggle is a personal preference that stems from a partial background in working with binary gates, architecture, etc where a toggle may go through several separate gates before reaching an end state. The word itself could be changed to anything else such as Update, Determine, Finalize, or even Steve. It really boils down to what makes sense to you and what your standard is.

Joel Etherton
But toggle just reverses the setting? It doesn't have any logic in it does it? Like only enable the button if a checkbox is checked and it's the first week of the month.
tyndall
ToggleButtonVisibility() suggests that it toggles the button visibility, not that it shows or hides depending on state.
Andy Thomas-Cramer
@Tyndall: I was thinking it was a good name, but I see your point
Paul Hadfield
@tyndall - toggle could reverse the setting, but toggles are not necessarily binary conditions. If other conditions are not met, it could be that the toggle has no effect, but the end result is that the button is properly visible/invisible. I've even gone so far as to have a singular toggle method for multiple buttons/controls because the logic/settings/etc were intertwined enough that it helped cut down on unnecessary code.
Joel Etherton
Regardless, "toggle" implies the state *will* change. In this case, the state *might* change.
Andy Thomas-Cramer
@Joel Etherton - I like that design. I usually have one method to call to update all the visible/enabled settings on a form. That makes thing much simpler, especially since there are usually several controls to update for any particular setting. If the state changes (or might possibly have changed), then just call that one method and forget about it.
Jeffrey L Whitledge
@Andy Thomas-Cramer - I get what you're saying. His question asked us for what we use. I answered with what I use. Feel free to disagree with my naming convention. I will feel free to continue using it :)
Joel Etherton
My lack of toggle knowledge is showing. I have always used it as a reverse current state in my programming. I have no background in gates or electronics.
tyndall
@Jeffrey, I often work towards that as the end goal. That style works well in greenfield projects or when you get to completely rework a form.
tyndall
+4  A: 

How about updateButtonVisibilty()?

Andy Thomas-Cramer
A: 

How about using SetButtonVisibility( )

Sachin Shanbhag
+3  A: 

Might be overengineering, but the reason you may have problems because it's doing two things. So making sure you only have a function doing a single thing, howabout a method to determine if the button should be shown or not (takes parameters, returns bool), then set the value of the button directly.

  Button.Visibilty = DetermineIfButtonShouldBeShow(...);
Paul Hadfield
Thanks for bringing up this point. Depending on the previous code written in a project - sometimes I use this style. I really do like the use of the word Determine +1
tyndall
+1  A: 

Edit: Now your question is edited to include the example

// example of the style of method
public void ShouldIShowOrHideButton()
{
  Button.Visible = ((chkSomeSetting.Checked) && (DateTime.Now.Day < 8));
}

My answer is neither. I would do two things:

  1. Move the Button.Visible part outside the function, so the function just computes the logic and returns bool.
  2. Name the function according to its internal logic not according to whether it is for a button or not. So if your function checks for a wedding day it would be called IsWeddingDay, if it checks for a monthly meeting it would be IsMonthlyMeeting.

The code would be

Button.Visible = IsMonthlyMeeting()

and the logic can be subsequently used to control any other widgets if needed.

Old Answer: You probably need to explain more what ShowIfThisHideIfThat does.

If it depends on one condition, like:

if (condition)
    ShowBotton()
else
    HideButton()

then I would use

Button.SetVisibility(condition)

as per Lazarenko's comment above, or if the language has properties:

Button.Visible = condition

If you have two conditions like what ShowIfThisHideIfThat seems to imply, equivalent to:

if (cond1)
    ShowButton()
else if (cond2)
    HideButton()
else
    LeaveButtonAsItIs()

then the logic in my opinion is complicated and I wouldn't use one function. Sure, the code is equivalent to

Button.Visible = cond1 || (!cond2 && Button.Visible)

but you lose the understandability.

Muhammad Alkarouri
A: 

The confusion seems to stem from a mixing of the business logic and the UI logic. The test isn't whether the button should be shown. Code is going to use the test to decide if the button should be shown. It probably depends on whether some feature should be available. Consider:

if (IsFeatureEnabled()) {
  ShowButton();
} else {
  HideButton();
}

This is the code where business logic (IsFeatureEnabled()) meets UI (ShowButton()/HideButton()).

Adrian McCarthy