Hell in a bit-basket



  • <FONT size=1>I Ran across this absolute gem today.  This class was being to package up values stored in asp controls (Checkbox lists, radio buttons, etc) and store them in a single byte that was then stored in sql server. enjoy.</FONT>

    '*************************************************************
    '*************************************************************
    '***This Class Handles bit level flags. allowing you to    ***
    '***Access individual bits within a variable               ***
    '*** You can check the status and modify the bit pattern   ***
    '***                                                       ***
    '*** Use with care and remember that modifying a single    ***
    '*** bit WILL drastically alter the value of any variable  ***
    '*************************************************************
    '*************************************************************

    ''' -----------------------------------------------------------------------------
    ''' Project  : internet_live
    ''' Class  : BitProcessor
    '''
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Provides a way to work with individual bits as flags
    ''' </summary>
    ''' <remarks>
    ''' This Class Handles bit level flags. allowing you to
    ''' Access individual bits within a variable You can check
    ''' the status and modify the bit itself. Use with care and
    ''' remember that modifying a single bit WILL drastically alter
    ''' the value of any variable
    ''' </remarks>
    ''' <history>
    '''
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class BitProcessor

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '' Fields - Implementation variables
        ''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Integer count for error checking
        ''' </summary>
        ''' <remarks>
        ''' Because the bitproccessor can only handle 32 bits it is important to make sure
        ''' that we do not run over.
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private _index As Integer = 0 'mind the underscore at the beginning

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' The value whose bits to check/modify
        ''' </summary>
        ''' <remarks>
        ''' This variable holds the value associated with the MyByte property
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private _MyByte32Bit As Integer = 0 'This is our control byte 32 bits of pseudo unsigned goodness woot!

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '' Properties                                         ''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''


        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' an Indexed boolean property that allows access to the individual bits
        ''' </summary>
        ''' <param name="index"></param>
        ''' <value></value>
        ''' <remarks>
        ''' This property applies a mask to the bits of our value
        ''' to either set, unset or read the bit value.
        ''' The bit mask used is 1<<index where index is the bit number to check/modify
        '''
        ''' The Bit Mask is used in conjunction with the following logical algorithms.
        '''
        ''' Check bit: MyByte AND mask
        ''' SET Bit:  MyByte OR mask
        ''' UnSET Bit:  MyByte AND  (Not mask)
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Default Public Property Bits(ByVal index As Integer) As Boolean
            Get
                If (index <= 31 And (index >= 0)) Then
                    'lets check our bit
                    Dim mask As Integer = (1 << index) 'set our mask
                    If (_MyByte32Bit And mask) = False Then
                        Return False
                    Else
                        Return True
                    End If
                Else
                    'our bit index is out of range so Crash and Burn!!!
                    Throw New HttpException(9002, "Bit Proccessor encountered an error: Bits Index out of range", 9002)
                    'Err.Raise(9002, "BitProccessor", "Bit Index out of range")
                End If
            End Get
            Set(ByVal Value As Boolean)
                Dim mask As Integer = (1 << index)  'set our mask
                If Value Then 'we set the bit
                    _MyByte32Bit = _MyByte32Bit Or mask
                Else 'lets unset the bit
                    _MyByte32Bit = _MyByte32Bit And (Not mask)
                End If
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Provides access to the to a 32 Byte Integer to work with
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' The above sums it up
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Property MyByte() As Integer
            Get
                Return _MyByte32Bit
            End Get
            Set(ByVal Value As Integer)
                _MyByte32Bit = Value
            End Set
        End Property

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Creates a bitpattern from a checkbox/checkboxlist,radiobutton,radiobuttonlist control.
        ''' </summary>
        ''' <param name="myControl">A webControl to make a bit pattern from</param>
        ''' <returns>a string containing the type name of the control.</returns>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function addControlValues(ByRef myControl As WebControl) As String
            Dim strControlType As String
            strControlType = myControl.GetType.Name().ToLower

            Select Case strControlType
                Case "checkboxlist"
                    'run checkboxlist function
                    addCheckboxList(myControl)
                Case "checkbox"
                    'run checkbox function
                    addCheckbox(myControl)
                Case "radiobuttonlist"
                    'run checkboxlist function
                    addRadioButtonList(myControl)
                Case "radiobutton"
                    'run checkbox function
                    addRadioButton(myControl)
            End Select
            Return strControlType
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Adds a checkbox to the bit pattern
        ''' </summary>
        ''' <param name="myControl">a checkbox control whose value to add</param>
        ''' <remarks>
        ''' nope
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub addCheckbox(ByRef myControl As CheckBox)
            If _index <> 32 Then
                If myControl.Checked Then
                    Bits(_index) = True
                Else
                    Bits(_index) = False
                End If
                _index += 1
            Else
                Throw New HttpException("Bit Processor Index out of range")
            End If
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Adds a checkboxlist to the bit pattern
        ''' </summary>
        ''' <param name="myControl">a checkboxlist control whose value to add</param>
        ''' <remarks>
        ''' nope
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub addCheckboxList(ByRef myControl As CheckBoxList)
            Dim myItem As ListItem

            If (_index + myControl.Items.Count) < 32 Then
                For Each myItem In myControl.Items
                    'set me?
                    If myItem.Selected = True Then
                        Bits(_index) = True 'Victory is MINE!
                    Else
                        'nahh another time perhaps
                        Bits(_index) = False
                    End If
                    _index += 1
                Next
            End If
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Adds a radiobutton to the bit pattern
        ''' </summary>
        ''' <param name="myControl">a radiobutton control whose value to add</param>
        ''' <remarks>
        ''' nope
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub addRadioButton(ByRef myControl As RadioButton)
            If _index <> 32 Then
                If myControl.Checked Then
                    Bits(_index) = True
                Else
                    Bits(_index) = False
                End If
                _index += 1
            Else
                Throw New HttpException("Bit Processor Index out of range")
            End If
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Adds a radiobuttonlist to the bit pattern
        ''' </summary>
        ''' <param name="myControl">a radiobuttonlist control whose value to add</param>
        ''' <remarks>
        ''' nope
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub addRadioButtonList(ByRef myControl As RadioButtonList)
            Dim myItem As ListItem

            If (_index + myControl.Items.Count) < 32 Then
                For Each myItem In myControl.Items
                    'set me?
                    If myItem.Selected = True Then
                        Bits(_index) = True 'Victory is MINE!
                    Else
                        'nahh another time perhaps
                        Bits(_index) = False
                    End If
                    _index += 1 'increment by 1
                Next
            End If
        End Sub
    End Class



  • Waitwaitwait, let me get this... So this guy makes a class that stores a 32 bit set of flags - and then adds another 32 bit variable (_index) to store which of the 32 bits he is addressing? THAT's optimisation at it's best!



  • 'our bit index is out of range so Crash and Burn!!!

     probably the best part :)

     



  • <rant>
    @galgorah said:

    <-- snip -->

    <-- my favorite comment -->
        Private _index As Integer = 0 'mind the underscore at the beginning

    <-- snip -->

    <-- learn to use regions -->
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        '' Properties                                         ''
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    <-- snip -->

    <-- learn the difference between ByVal and ByRef.  (Hint: VB.Net <> VB6) -->

        Public Function addControlValues(ByRef myControl As WebControl) As String
            Dim strControlType As String
            strControlType = myControl.GetType.Name().ToLower

            Select Case strControlType
                Case "checkboxlist"
                    'run checkboxlist function
                    addCheckboxList(myControl)
                Case "checkbox"
                    'run checkbox function
                    addCheckbox(myControl)
                Case "radiobuttonlist"
                    'run checkboxlist function
                    addRadioButtonList(myControl)
                Case "radiobutton"
                    'run checkbox function
                    addRadioButton(myControl)
    <-- add a default condition... throw an error on an unsupported type please! -->
            End Select
            Return strControlType
        End Function

    <-- snip -->

    <-- didn't we see this recently? -->
                If myControl.Checked Then
                    Bits(_index) = True
                Else
                    Bits(_index) = False
                End If

    <-- snip -->


    </rant>



  • @Mal1024 said:

    'our bit index is out of range so Crash and Burn!!!

     probably the best part :)

    Better than Private _index As Integer = 0 'mind the underscore at the beginning?  You just KNOW this means they aren't using Option Explicit.



  • As usual, the real WTF is that the languages used is Visual Basic.  The double WTF is that it's VB.NET used as though it was VB6.  And, of course, the third WTF is the ridiculous over-commenting of everything.



  • The REAL WTF is that .NET already has this class named BitArray which does exactly that, use an integer and access the individual bits in it...



  • @Mal1024 said:

    'our bit index is out of range so Crash and Burn!!!
    probably the best part :)

    Especially as it throws a HttpException instead of an OutOfRange exception (or whatever it's called).



  • @ObiWayneKenobi said:

    And, of course, the third WTF is the ridiculous over-commenting of everything.

    Usually, I'd rather see over- than under-commenting.

    But then I saw this...multiple times..

        ''' <remarks>
        ''' nope
        ''' </remarks>

     



  • I would like to add that this was apparently created for the following purpose.  A checkbox is used to enable or disable a textbox.  this composite is used to specify which type of treatment is expected and provide additional details.  The values from the Checkbox are used to determine wether or not to populate certain varchar fields in the table.  unchecked = put in the database.  These checkbox are then compiled through this bitprocessor class and stored as an int in the database.  So this means if you want to add a treatment option to the webform you have to entirely rework the app and the database table data becomes garbage and goes to hell in a bit basket.



  • Every instance of a bitprocessor in our code also is prefaced by this comment:

    <FONT color=#008000 size=2> 

    'we need to check bit status here and print our text accordingly
    </FONT><FONT color=#008000 size=2>'<JAMESBROWN_VOICE>Im'a Bit machine! HOUGH-HAWGH!</JAMESBROWN_VOICE>

    </FONT>


  • @galgorah said:

    Every instance of a bitprocessor in our code also is prefaced by this comment:

    <font color="#008000" size="2"> </font>

    <font color="#008000" size="2">'we need to check bit status here and print our text accordingly
    </font><font color="#008000" size="2">'<JAMESBROWN_VOICE>Im'a Bit machine! HOUGH-HAWGH!</JAMESBROWN_VOICE></font>

     

    Well, that sheds a bit of light on the situation.  My guess is that the author was dealing with some imposed internal documentation standard, and the whole POS is an editorial comment on that standard.



  • Just scrolling down thru this mess, I noticed two three things:

     

    Private _MyByte32Bit As Integer = 0 'This is our control byte 32 bits of pseudo unsigned goodness woot!

    Public Property MyByte() As Integer
            Get
                Return _MyByte32Bit
            End Get<snip>

    a "control byte" that is 32 bits long? er...this would be the first byte I've ever encountered that's not 8 bits.

    and

     

    What the hell does a bit processor class have to do with controls and radio buttons? Processing a bitmask is independent of UI. Why is all this in the same class?

     

    and

    Default Public Property Bits(ByVal index As Integer) As Boolean
            Get
                If (index <= 31 And (index >= 0)) Then
                   <snip/>
                Else
                    'our bit index is out of range so Crash and Burn!!!
                    Throw New HttpException(9002, "Bit Proccessor encountered an error: Bits Index out of range", 9002)
                    'Err.Raise(9002, "BitProccessor", "Bit Index out of range")
                End If
            End Get

    Why the hell would you throw an HTTPException for an error processing a bitmask? Even assuming this class can only be used in a web app (cuz, after all, that IS the only place you'd ever want to check bits), and HTTPException is supposed to be in reference to HTTP processing errors.

     

    from MSDN:

    HttpException Class

    			Describes an exception that occurred during the processing of HTTP requests.</p><p>&nbsp;</p><p>I guess technically, if the bit processor is used at anypoint in a webpage, it's "during an HTTP request"? WTF? What if I want to process bits in a windows service? an HTTPException would be a bit out of place there.</p><p>&nbsp;</p><p>&nbsp;</p><p><br><br>&nbsp;</p><p>&nbsp;<br>&nbsp;</p><p>&nbsp;</p>


  • @bstorer said:

    @Mal1024 said:

    'our bit index is out of range so Crash and Burn!!!

     probably the best part :)

    Better than Private _index As Integer = 0 'mind the underscore at the beginning?  You just KNOW this means they aren't using Option Explicit.

     

    Why does that imply they aren't using Option Explicit? Option Explicit merely requires that all variables be declared. He's declaring it. So, maybe he is, maybe he isn't.

    OE on means you have to

    OE off means you don't have to, but still CAN.

    therefore you can't determine the OE status from this statement.

     



  • @bstorer said:

    @Mal1024 said:

    'our bit index is out of range so Crash and Burn!!!

     probably the best part :)

    Better than Private _index As Integer = 0 'mind the underscore at the beginning?  You just KNOW this means they aren't using Option Explicit.

     

    OR, that they have a property named index, which in grand WTF tradition, probably doesn't reference the private _index member. Marvelous. 


Log in to reply