Are globals still bad?

I came across some code like this today; having a flow control flag that is visible to the entire class. My gut says this is the wrong way to handle this need for flow control (it almost looks like the old-time global flag in C).

(An example is provided in VB.Net, but similar things can be done in C #.)

Public Class myClass

    Private bMyFlag As Boolean ''// <------

    private sub New
        bMyFlag = false
    end sub

    private sub mySub1
        bMyFlag = true
        mySub3()
        mySub4()
        mySubN()
    end sub

    private sub mySub2
        bMyFlag = false
        mySub3()
        mySub4()
        mySubN()
    end sub

    private sub mySub3
        if bMyFlag then
            DoSomething()
        else
            DoSomethingElse()
        end if
    end sub

    private sub mySub4
        if not bMyFlag then
            DoSomethingDifferent()
        else
            DoSomethingReallyDifferent()
        end if
    end sub

    private sub mySubN
        if not bMyFlag then
            DoNotPassGo()
        else
            DoNotCollect200Dollars()
        end if
    end sub
end class

It is obvious to me that he was vaccinated after this fact. I plan to redesign the code so that bMyFlag is the parameter passed to mySub3, mySub4 and mySuvN. However, before I do this:

Is there a good reason for having a flow control flag that is global to the class?

Or, if not, why is it considered bad practice?

+3
8

bMyFlag, , , ( ..). , . , , , . mySub1 mySub2, ?

+7

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

, , , . - , , .

+12

, "". . ( - , ).

+9

, , . , . / . . , , .

- , , : . Editable . , readonly . ( readonly , - ).

, , , . , if/else , - , .

:

, , . LoggableObject. LoggableObject : ScreenLoggable FileLoggable. ILoggable , . ScreenLoggable, , , . FileLoggable, . , , . factory, ILoggable, . , .

+4

, , . , - +, bMyFlag - (). bMyFlag, mySub3, mySub4, mySubN do, . , bMyFlag , .

+2

, ( ), , . , .

+1

, , . Mysub3, mysub4 .. , . , .

+1

It is not global, but still random. If it is used only a couple of times, then maybe it’s good to leave it for simplicity, but something more than 2 or 3 times, and you should look to extract various functions in other classes.

If you want all the code to be local to this class, you can nest classes in it. Something like the following (sorry if the vb syntax is wrong, it has been a while)

Public Class myClass

Private bMyobj As iThing ' <------

Private Sub New()
    bMyobj = null
End Sub

Private Sub mySub1()
    bMyobj = New Thing2()
    mySub3()
    mySub4()
    mySubN()
End Sub

Private Sub mySub2()
    bMyobj = New Thing1()
    mySub3()
    mySub4()
    mySubN()
End Sub

Private Sub mySub3()
    bMyObj.DoSomething()
End Sub

Private Sub mySub4()
    bMyObj.DoSomethingDifferent()
End Sub

Private Sub mySubN()
    bMyObj.PassGo()
End Sub


 Public Interface iThing
     Sub DoSomething()
     Sub DoSomethingDifferent()
     Sub PassGo()
 End Interface

 Public Class Thing1
     Implements iThing

     Public Sub DoSomething()
         ' Implementation 1
     End Sub
     Public Sub DoSomethingDifferent()
         ' Implementation 1
     End Sub
     Public Sub PassGo()
         ' Don't do it
     End Sub
 End Class
 Public Class Thing2
     Implements iThing
     Public Sub DoSomething()
         ' Implementation 2
     End Sub
     Public Sub DoSomethingDifferent()
         ' Implementation 2
     End Sub
     Public Sub PassGo()
         ' Don't collect 200 dollars
     End Sub
 End Class

End Class
0
source

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


All Articles