定义方法的好方法

发布于 2024-09-19 02:06:21 字数 1308 浏览 1 评论 0原文

实现方法调用的最佳/好方法是什么?

例如:以下内容通常被认为是最佳实践。如果两者都不好,那么什么被认为是最佳实践。

选项 1:

   private void BtnPostUpdate_Click(object sender, EventArgs e)
    {
        getValue();
    }

    private void getValue()
    {
        String FileName = TbxFileName.Text;
        int PageNo = Convert.ToInt32(TbxPageNo.Text);

        // get value from Business Layer
        DataTable l_dtbl = m_BLL.getValue(FileName, PageNo);

        if (l_dtbl.Rows.Count == 1)
        {
            TbxValue.Text = Convert.ToInt32(l_dtbl.Rows[0]["Value"]);
        }
        else
        {
            TbxValue.Text = 0;
        }
    }

选项 2:

    private void BtnPostUpdate_Click(object sender, EventArgs e)
    {
        String FileName = TbxFileName.Text;
        int PageNo = Convert.ToInt32(TbxPageNo.Text);

        int Value = getValue(FileName, PageNo);

        TbxValue.Text = Value.ToString();

    }

    private int getValue(string FileName, int PageNo)
    {
        // get value from Business Layer
        DataTable l_dtbl = m_BLL.getValue(FileName, PageNo);

        if (l_dtbl.Rows.Count == 1)
        {
            return Convert.ToInt32(l_dtbl.Rows[0]["Value"]);
        }
        return 0;
    }

我知道我们可以直接传递参数而不分配给局部变量...我的问题更多是关于方法定义及其处理方式。

What is the best / good way to implement method calls.

For eg: From the below which is generally considered as best practice. If both are bad, then what is considered as best practice.

Option 1 :

   private void BtnPostUpdate_Click(object sender, EventArgs e)
    {
        getValue();
    }

    private void getValue()
    {
        String FileName = TbxFileName.Text;
        int PageNo = Convert.ToInt32(TbxPageNo.Text);

        // get value from Business Layer
        DataTable l_dtbl = m_BLL.getValue(FileName, PageNo);

        if (l_dtbl.Rows.Count == 1)
        {
            TbxValue.Text = Convert.ToInt32(l_dtbl.Rows[0]["Value"]);
        }
        else
        {
            TbxValue.Text = 0;
        }
    }

Option 2 :

    private void BtnPostUpdate_Click(object sender, EventArgs e)
    {
        String FileName = TbxFileName.Text;
        int PageNo = Convert.ToInt32(TbxPageNo.Text);

        int Value = getValue(FileName, PageNo);

        TbxValue.Text = Value.ToString();

    }

    private int getValue(string FileName, int PageNo)
    {
        // get value from Business Layer
        DataTable l_dtbl = m_BLL.getValue(FileName, PageNo);

        if (l_dtbl.Rows.Count == 1)
        {
            return Convert.ToInt32(l_dtbl.Rows[0]["Value"]);
        }
        return 0;
    }

I understand we can pass parameters directly without assigning to a local variable... My question is more about the method definition and the way it is handled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合久必婚 2024-09-26 02:06:21

如果您自动订阅事件,我认为拥有一个带有事件处理程序签名的方法并不是特别糟糕,该方法仅委托给具有您需要的“真实”签名的方法(在本例中,没有参数) 。

如果您手动订阅,则可以改用 lambda 表达式:

postUpdateButton.Click += (sender, args) => PostUpdate();

然后在 PostUpdate 中完成工作。是否将 PostUpdate 拆分为两种方法,一种处理 UI 交互,一种处理 BLL 交互,这取决于您。在这种情况下,我认为这并不重要。

不过,如何构建 UI 逻辑以使其可测试是完全不同的事情。我最近成为 MVVM 模式的粉丝,但我不知道它对您的特定场景有多大的适用性(它实际上是围绕 Silverlight 和 WPF 设计的)。

不过还有其他一些评论:

  • 按照惯例,参数应该采用驼峰命名法,而不是帕斯卡命名法。
  • 您真的相信您可以从在局部变量中添加 l_ 前缀中获益吗?这不是很明显他们是本地人吗?就我个人而言,我并不热衷于此处显示的大多数变量名称 - 考虑根据变量的含义而不是其类型来命名。
  • 使用DataTable 返回信息是一种容易出错的方法。为什么 BLL 不能返回一个 int? 来指示该值(或缺少值)?

If you're subscribing to the event automatically, I don't think it's particularly bad to have a method with the event handler signature which just delegates to a method which has the "real" signature you need (in this case, no parameters).

If you're subscribing manually, you can use a lambda expression instead:

postUpdateButton.Click += (sender, args) => PostUpdate();

and then do the work in PostUpdate. Whether you then split up the PostUpdate into two methods, one to deal with the UI interaction and one to deal with the BLL interaction is up to you. In this case I don't think it matters too much.

How you structure UI logic to make it testable is a whole different matter though. I've recently become a fan of the MVVM pattern, but I don't know how applicable that would be to your particular scenario (it's really designed around Silverlight and WPF).

A couple of other comments though:

  • Conventionally, parameters should be camelCased, not PascalCased
  • Do you genuinely believe you're getting benefit from prefixing local variables with l_? Isn't it obvious that they're local? Personally I'm not keen on most of the variable names shown here - consider naming variables after their meaning rather than their type.
  • Using a DataTable to return information is a somewhat error-prone way of doing things. Why can the BLL not return an int? to indicate the value (or a lack of value)?
带上头具痛哭 2024-09-26 02:06:21

如果我不实现 mvc,这就是我喜欢的。我假设这里有网络。

我首先执行选项 2,但不是让按钮代码设置文本 id,而是创建一个属性来设置文本框值。

我这样做是因为如果其他东西设置了文本框值,那么您将重复代码。如果更改名称或控件类型,情况会很糟糕。

here is what i like to to if i don't implement mvc. and i'm assuming web here.

I'd do option 2 first but instead of having the buttons code set the text id create a property to set the text boxs value.

I do this because if something else sets the textbox value then you are going to duplicate code. bad if you change a name or control type.

零度℉ 2024-09-26 02:06:21

根据您的示例,选项 2 是最佳选择。选项 1 了解您的表单以及如何在表单上显示数据,这违反了 SRP

According to your example, option 2 is the way to go. Option 1 knows about your form and how to display data on it, which violates the SRP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文