代码之家  ›  专栏  ›  技术社区  ›  BuddyJoe

方法命名约定问题(大多数语言)

  •  5
  • BuddyJoe  · 技术社区  · 14 年前


    如果我有一个显示按钮的方法,我可能会调用它


    按钮可见性测试 (这听起来像是返回true/false,但实际上并没有完成工作)
    测试按钮
    显示隐藏按钮

    // example of the style of method
    public void ShouldIShowOrHideButton()
    {
      Button.Visible = ((chkSomeSetting.Checked) && (DateTime.Now.Day < 8));
    }
    
    6 回复  |  直到 14 年前
        1
  •  4
  •   Paul Hadfield    14 年前

      Button.Visibilty = DetermineIfButtonShouldBeShow(...);
    
        2
  •  7
  •   Andy Thomas    14 年前

    updateButtonVisibilty() ?

        3
  •  3
  •   Joel Etherton    14 年前

    我的偏好是保持切换方法,而不是单独的隐藏/显示方法。

    ToggleButtonVisibility()
    

    编辑:切换是一种个人偏好,源于使用二进制门、体系结构等的部分背景,在这种背景下,切换可能会在到达最终状态之前通过几个单独的门。这个词本身可以更改为任何其他内容,如更新、确定、最终确定,甚至是史蒂夫。归根结底是什么对你有意义,你的标准是什么。

        4
  •  2
  •   Muhammad Alkarouri    14 年前

    编辑

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

    我的答案是 也不 . 我会做两件事:

    1. 移动 Button.Visible bool .
    2. 根据函数的名称命名函数 内部逻辑 不取决于它是否是一个按钮。因此,如果你的函数检查婚礼当天,它将被调用 IsWeddingDay ,如果它检查每月的会议,它将是 IsMonthlyMeeting

    代码应该是

    Button.Visible = IsMonthlyMeeting()
    

    如果需要的话,该逻辑可以随后用于控制任何其他小部件。

    古老的答案 你可能需要解释更多什么 做。

    if (condition)
        ShowBotton()
    else
        HideButton()
    

    那我就用

    Button.SetVisibility(condition)
    

    根据Lazarenko的上述评论,或者如果语言有属性:

    Button.Visible = condition
    

    如果你有两个条件,比如什么 展示他的隐藏

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

    那么在我看来,逻辑是复杂的,我不会使用一个函数。当然,代码相当于

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

    但你失去了理解力。

        5
  •  0
  •   Sachin Shanbhag    14 年前

    用这个怎么样 SetButtonVisibility( )

        6
  •  0
  •   Adrian McCarthy    14 年前

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

    这是业务逻辑( IsFeatureEnabled() )满足用户界面( ShowButton() / HideButton() ).