One long method or many methods, but very readable

I reanalyzed one of the methods, as shown below, I personally think that it is very readable, but one of my colleagues does not really like that he claims that there are too many methods. I am wrong, if not, how can I convince him? Source Method:

   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

The refactored method:

    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

Recalculated based on feedback

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
+3
source share
6 answers

Methods should "Do only one thing" (for more information, see the Coding Horror Blog Post ). This is also known as the principle of shared responsibility .

(), . , . , VerifyAddressAndAddToDatabase, - VerifyAddress AddToDatabase.

( , ), . .

+3

, , Cyclomatic complex, , . , .

, .

, , -, , .

. , Cyclomatic 2; .. .

+2

"User_Input..."; . .

, , , , , , , ( ). , , .

+1

1 if 4 . , .

- 3- . , . - , .

, .

+1

, " " . , . , , .

, , ( , ), , .

, , User_Input_Mobile_Is_Not_The_Same_As_The_One_Stored_In_The_Database() , - .

+1

. , ...

/ ? , , , , .

MobileContact, MobileContact.SaveMobilePhoneNumber() MobileContact.Save() MobileContact.User_Input_Mobile_Is_Not_The_Same_As_The_One_Stored_In_The_Database() MobileContact.IsDirty().

0

Source: https://habr.com/ru/post/1727986/


All Articles