Asp.net 一个或多个方法,但可读性强

Asp.net 一个或多个方法,但可读性强,asp.net,Asp.net,我重新考虑了如下所示的其中一种方法,我个人认为它可读性很强,但我的一位同事不太喜欢它,认为它有太多的方法。我错了吗?如果不是,我怎么能说服他? 原始方法: Private Sub SendTextMessage(ByVal sender As Object, ByVal eventArgs As EventArgs) If String.IsNullOrEmpty(SMSUserControl.TxtPhoneProperty) Then

我重新考虑了如下所示的其中一种方法,我个人认为它可读性很强,但我的一位同事不太喜欢它,认为它有太多的方法。我错了吗?如果不是,我怎么能说服他? 原始方法:

   Private Sub SendTextMessage(ByVal sender As Object, ByVal eventArgs As EventArgs)
                    If String.IsNullOrEmpty(SMSUserControl.TxtPhoneProperty) Then
                        lblError.Text = Globalization.GetTranslation("PhoneNotSet", "A mobile phone number must be specified")
                        Exit Sub
                    End If

                    Try
                        If Not String.Equals(UserHelper.CurrentUser.Mobile, SMSUserControl.TxtPhoneProperty) Then
                            UserHelper.CurrentUser.Mobile = SMSUserControl.TxtPhoneProperty
                            UserHelper.CurrentUser.Properties.Save()
                        End If
                        If IPhoneProfilePresenter Is Nothing Then
                            IPhoneProfilePresenter = New IPhoneProfilePresenter(Me, CabFileNameManagerFactory.Create(), SMSSenderFactory.Create())
                        End If
                        IPhoneProfilePresenter.SendTextMessage(New TextMessageInfo(SMSUserControl.TxtPhoneProperty, GetOCSInstallerLink(), DownloadLink.Text))

                    Catch ex As Exception
                        lblError.Text = _
                            CWebLog.LogAndDisplayError(CurrentSession.IsFullLogging, _
                                                        Globalization.GetTranslation("MobilePhoneSave", _
                                                                                      "Failed to save the mobile phone number"), _
                                                        ex)
                    End Try
                End Sub
    Private Sub SendTextMessage(ByVal sender As Object, ByVal eventArgs As EventArgs)
            If PhoneNumberIsNotSet() Then
                lblError.Text = Globalization.GetTranslation("PhoneNotSet", "A mobile phone number must be specified")
                Exit Sub
            End If

            Try
                If User_Input_Mobile_Is_Not_The_Same_As_The_One_Stored_In_The_Database() Then
                    UpdateMobile()

                End If
                GetIPhoneProfilePresenter().SendTextMessage(New TextMessageInfo(SMSUserControl.Mobile, GetOCSInstallerLink(), DownloadLink.Text))




            Catch ex As Exception
                lblError.Text = _
                    CWebLog.LogAndDisplayError(CurrentSession.IsFullLogging, _
                                                Globalization.GetTranslation("MobilePhoneSave", _
                                                                              "Failed to save the mobile phone number"), _
                                                ex)
            End Try
        End Sub

        Private Sub UpdateMobile()

            UserHelper.CurrentUser.Mobile = SMSUserControl.Mobile
            UserHelper.CurrentUser.Properties.Save()
        End Sub

        Private Function User_Input_Mobile_Is_Not_The_Same_As_The_One_Stored_In_The_Database() As Boolean

            Return Not String.Equals(UserHelper.CurrentUser.Mobile, SMSUserControl.Mobile)
        End Function

        Private Function PhoneNumberIsNotSet() As Boolean

            Return String.IsNullOrEmpty(SMSUserControl.Mobile)
        End Function

        Private Function GetIPhoneProfilePresenter() As IPhoneProfilePresenter

            If IPhoneProfilePresenter Is Nothing Then
                IPhoneProfilePresenter = New IPhoneProfilePresenter(Me, CabFileNameManagerFactory.Create(), SMSSenderFactory.Create())
            End If
            Return IPhoneProfilePresenter
        End Function
重新计算的方法:

   Private Sub SendTextMessage(ByVal sender As Object, ByVal eventArgs As EventArgs)
                    If String.IsNullOrEmpty(SMSUserControl.TxtPhoneProperty) Then
                        lblError.Text = Globalization.GetTranslation("PhoneNotSet", "A mobile phone number must be specified")
                        Exit Sub
                    End If

                    Try
                        If Not String.Equals(UserHelper.CurrentUser.Mobile, SMSUserControl.TxtPhoneProperty) Then
                            UserHelper.CurrentUser.Mobile = SMSUserControl.TxtPhoneProperty
                            UserHelper.CurrentUser.Properties.Save()
                        End If
                        If IPhoneProfilePresenter Is Nothing Then
                            IPhoneProfilePresenter = New IPhoneProfilePresenter(Me, CabFileNameManagerFactory.Create(), SMSSenderFactory.Create())
                        End If
                        IPhoneProfilePresenter.SendTextMessage(New TextMessageInfo(SMSUserControl.TxtPhoneProperty, GetOCSInstallerLink(), DownloadLink.Text))

                    Catch ex As Exception
                        lblError.Text = _
                            CWebLog.LogAndDisplayError(CurrentSession.IsFullLogging, _
                                                        Globalization.GetTranslation("MobilePhoneSave", _
                                                                                      "Failed to save the mobile phone number"), _
                                                        ex)
                    End Try
                End Sub
    Private Sub SendTextMessage(ByVal sender As Object, ByVal eventArgs As EventArgs)
            If PhoneNumberIsNotSet() Then
                lblError.Text = Globalization.GetTranslation("PhoneNotSet", "A mobile phone number must be specified")
                Exit Sub
            End If

            Try
                If User_Input_Mobile_Is_Not_The_Same_As_The_One_Stored_In_The_Database() Then
                    UpdateMobile()

                End If
                GetIPhoneProfilePresenter().SendTextMessage(New TextMessageInfo(SMSUserControl.Mobile, GetOCSInstallerLink(), DownloadLink.Text))




            Catch ex As Exception
                lblError.Text = _
                    CWebLog.LogAndDisplayError(CurrentSession.IsFullLogging, _
                                                Globalization.GetTranslation("MobilePhoneSave", _
                                                                              "Failed to save the mobile phone number"), _
                                                ex)
            End Try
        End Sub

        Private Sub UpdateMobile()

            UserHelper.CurrentUser.Mobile = SMSUserControl.Mobile
            UserHelper.CurrentUser.Properties.Save()
        End Sub

        Private Function User_Input_Mobile_Is_Not_The_Same_As_The_One_Stored_In_The_Database() As Boolean

            Return Not String.Equals(UserHelper.CurrentUser.Mobile, SMSUserControl.Mobile)
        End Function

        Private Function PhoneNumberIsNotSet() As Boolean

            Return String.IsNullOrEmpty(SMSUserControl.Mobile)
        End Function

        Private Function GetIPhoneProfilePresenter() As IPhoneProfilePresenter

            If IPhoneProfilePresenter Is Nothing Then
                IPhoneProfilePresenter = New IPhoneProfilePresenter(Me, CabFileNameManagerFactory.Create(), SMSSenderFactory.Create())
            End If
            Return IPhoneProfilePresenter
        End Function
根据反馈重新考虑因素

Private Sub ValidateAndSaveMobilePhoneAndSendTextMessage(ByVal sender As Object, ByVal eventArgs As EventArgs)
            If ValidateMobilePhoneNumber() Then
                SaveMobilePhoneNumber()
                SendTextMessage()
            End If

        End Sub

        Private Sub SendTextMessage()

            If iPhoneProfilePresenter Is Nothing Then
                iPhoneProfilePresenter = New iPhoneProfilePresenter(Me, CabFileNameManagerFactory.Create(), SMSSenderFactory.Create())
            End If
            iPhoneProfilePresenter.SendTextMessage(New TextMessageInfo(SMSUserControl.TxtPhoneProperty, GetOCSInstallerLink(), DownloadLink.Text))
        End Sub

        Private Sub SaveMobilePhoneNumber()

            Try
                If Not String.Equals(Globalization.CurrentUser.Mobile, SMSUserControl.TxtPhoneProperty) Then
                    Globalization.CurrentUser.Mobile = SMSUserControl.TxtPhoneProperty
                    Globalization.CurrentUser.Properties.Save()
                End If
            Catch ex As Exception
                lblError.Text = _
                    CWebLog.LogAndDisplayError(CurrentSession.IsFullLogging, _
                                               ContentHelper.GetTranslation("MobilePhoneSave", _
                                                                            "Failed to save the mobile phone number"), _
                                               ex)
            End Try
        End Sub

        Private Function ValidateMobilePhoneNumber() As Boolean
            Dim isValid As Boolean = True
            If String.IsNullOrEmpty(SMSUserControl.TxtPhoneProperty) Then
                lblError.Text = ContentHelper.GetTranslation("PhoneNotSet", "A mobile phone number must be specified")
                isValid = False
            End If
            Return isValid
        End Function

去掉“用户输入…”功能;将代码内联。否则,这是可以接受的


像这样的函数名太荒谬了,IMHO,因为你可能想在以后做更多的事情,然后你需要更改名称(以免产生误导)。另外,读起来也很烦人,IMHO。

您将原来的方法减少了1个if语句,并在exchange中增加了4个方法。我想说这使得代码更加复杂

原来的方法实际上是一个3语句方法。这足够短,不需要重构。其中一个重构方法的巨大名称暗示了它不是一个好的重构语句


我只想在原稿中添加更多的注释,尤其是整个函数的标题注释。

问他为什么会担心“太多”方法。一般来说,现代语言中的函数调用不会增加显著的开销。对于试图阅读同事代码的程序员来说,节省时间通常比为处理器节省几个时钟周期要经济得多

就我个人而言,我认为阅读一个函数可以提供它所做工作的高级概述(因为它包括对其他方法的调用,这些方法都是以信息的形式命名的),而不是我自己去理解它的意图,这是很好的

这就是说,正如其他人所指出的,
用户输入移动与存储在数据库中的方法不同()
并不是我见过的最干净的方法名称。

方法应该“只做一件事”(更多细节请参见此)。这也被称为

方法应简短、易懂且无副作用。您应该能够以方法的名称描述方法的功能。例如,如果您的方法是
VerifyAddress和AddToDatabase
,则需要将其拆分为两个方法-
VerifyAddress
AddToDatabase


在过去,调用许多方法(或当时调用的过程和函数)被认为是一个坏主意,因为其中涉及到开销。但对于现代编译器和更快的处理器,这不是一个问题。

您关心的可能是,代码可以采用的不同路径的数量。这可以作为一个度量来标识代码片段

每方法圈复杂度高表明方法变得太复杂

不仅是你自己在做这件事,其他任何一个和它一起工作的人都会经历一段艰难的时光。 这变得很难理解

  • 试验
  • 维护和
  • 重构

这段代码稍后会出现。更好的做法是将圈复杂度保持在2以下或等于2;i、 e在做出决定时,您的方法不应超过两条路径。

注意:这不是对您问题的直接回答,但我无法将其纳入评论中

类名/结构是什么?我倾向于根据经验法则,方法名应该尽可能放在类名的上下文中


如果你的类是
MobileContact
,那么
MobileContact.SaveMobilePhoneNumber()
可以变成
MobileContact.Save()
MobileContact.User\u Input\u Mobile\u与存储在数据库中的不一样()
可以变成
MobileContact.IsDirty()
,谢谢,为什么我需要把这个函数内联呢?Ybbest:正如我所说的,把它抽象出来没有多大意义。我怀疑你的代码中还有其他地方在调用它(事实上,理想情况下,它应该在其他地方完成,但无论如何),就目前情况而言,不得不在其他地方寻找这种逻辑是很奇怪的。我已经在重新分解的基础上重新分解了代码,请再次检查。我想我正在使用一个非常大的遗留代码,并且没有任何单元测试。这可能是迄今为止我所能做的最好的工作。感谢您的所有评论。感谢他为推荐这样一个好名字所做的努力。就可读性而言,最好的可能不是决策路径,而是方法名称,这使得上述方法显得很长。我已经根据反馈重新考虑了chrisf:FWIW,我认为问题不在于接受方法应该“做一件事”(我认为这是显而易见的),问题是帮助人们认识到“一件事”应该是什么:)(即紧凑但完整)。在一个大的方法中添加更多的注释会造成维护噩梦。他将代码大小增加了一倍——这怎么更具可读性?原文有3个if语句-这是一个大方法吗?我同意。像UpdateMobile()这样的私人助手方法——只为副作用而调用,不接受任何输入——使第二个示例更难理解。方法名称应该是描述性的,但清晰明了的代码胜过描述性的方法名称。信任我()。顺便说一句,我不同意“只需添加更多评论”的想法。在第一个代码中添加注释并不会使它变得更好。改进变量名和分解类依赖关系将使代码变得更好。我并不认为这不是世界上最好的重新分解工作,但如果你需要添加大量注释以使其更易于理解,那么应该首先对其进行重构。代码应该是自文档化的。+1用于提及我以前从未遇到过的有用指标。我希望你不介意我在你的答案中编辑一些链接,以节省其他读者的谷歌搜索……阿斯迪:嗯,这里不是圈复杂度。他没有添加更多的路径,他只是试图抽象出不需要的东西。谢谢Jim和silky,非常感谢,我应该照顾好自己