Question about method naming (most languages)

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

But what do you call the ShowIfThisHideIfThat style method?

Possible choices:
TestForButtonVisibility (sounds like this will return true / false but don't actually do the job)
Testbutton
ShowHideButton (the style I'm currently using)

For several years now, I donโ€™t have the style I like for these types of methods. I work mainly in C # and a bit of 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
source share
6 answers

It may be too complicated, but the reason is that you may have problems because it does two things. Therefore, make sure that you have only a function that performs one thing, as well as a method for determining whether the button should be displayed or not (accepts parameters, returns bool), and then directly sets the value of the button.

  Button.Visibilty = DetermineIfButtonShouldBeShow(...); 
+4
source

What about updateButtonVisibilty() ?

+5
source

My preference was to keep the switching methods, rather than the separate methods for hide / show.

 ToggleButtonVisibility() 

This allows you to put your test code there, and the expected result / output will be a correctly visible / invisible button.

Edit: Toggle is a personal preference that stems from a partial background when working with binary gates, architecture, etc., when the switch can go through several separate gates until it reaches its final state. The word itself can be changed to something else, such as Refresh, Define, Finish, or Steve. It really comes down to what makes sense to you and what is your standard.

+4
source

Edit : now your question is being edited to include an 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:

  • Move the Button.Visible part outside the function, so the function just calculates the logic and returns a bool .
  • Name the function according to its internal logic , regardless of whether it is used for the button or not. Therefore, if your function checks the wedding day, it will be called IsWeddingDay , if it checks the monthly meeting, it will be IsMonthlyMeeting .

Code will be

 Button.Visible = IsMonthlyMeeting() 

and then the logic can be used to control any other widgets, if necessary.

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

If it depends on one condition, for example:

 if (condition) ShowBotton() else HideButton() 

then i would use

 Button.SetVisibility(condition) 

in accordance with the comment of Lazarenko above, or if the language has the properties:

 Button.Visible = condition 

If you have two conditions, for example, that is shown in ShowIfThisHideIfThat, which is equivalent:

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

then the logic, in my opinion, is complicated, and I would not use a single function. Of course the code is equivalent

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

but you lose your comprehension.

+2
source

How about using SetButtonVisibility( )

0
source

The confusion seems to be related to a mix of business logic and user interface logic. The test is not whether the button should be displayed. The code will use a test to decide if a button should be displayed. This probably depends on which feature should be available. Consider:

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

This is the code in which the business logic ( IsFeatureEnabled() ) matches the UI ( ShowButton() / HideButton() ).

0
source

All Articles