How to be popular among your colleagues
-
1. Always repeat obvious things twice.
Enum NumberSize
Bit_8 = 8
Bit_16 = 16
Bit_32 = 32
Bit_64 = 64
Bit_128 = 128
End Enum2. Hide all comments behind visible portion of screen - your co-workers do not need them.
Public Function CBin_Number(ByVal InputNumber As Double, ByVal InputNumberSize As NumberSize) As String 'converts large numbers into strings 8, 16,32, 64, or 128 characters long
Dim FinalBinNumber As String3. Show that 128 zeros and ones can be easily fit in 48 bits mantissa.
Dim TempNumber As Double = InputNumber Dim PlaceCounter As Integer
4. Always remind that computer cannot be trusted even with simple operation of subtraction.
Select Case InputNumberSize Case NumberSize.Bit\_8 PlaceCounter = 7 Case NumberSize.Bit\_16 PlaceCounter = 15 Case NumberSize.Bit\_32 PlaceCounter = 31 Case NumberSize.Bit\_64 PlaceCounter = 63 Case NumberSize.Bit\_128 PlaceCounter = 127 End Select
5. Do not listen anybody (even yourself) and choose most interesting way to handle the task.
If (TempNumber - (2 ^ PlaceCounter)) > 0 Then PlaceCounter = 127 End If
6. Be simple – the String class is the best thing since sliced bread.
Do Until (PlaceCounter < 0) If (TempNumber - (2 ^ PlaceCounter)) >= 0 Then FinalBinNumber = FinalBinNumber & "1" TempNumber = TempNumber - (2 ^ PlaceCounter) ElseIf (TempNumber - (2 ^ PlaceCounter)) < 0 Then FinalBinNumber = FinalBinNumber & "0" End If PlaceCounter = PlaceCounter - 1 Loop
7. Always add checks for weird cases
If FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" Then FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 or Error: Number Too Large" End If Return FinalBinNumber
End Function
Public Shared Function ReaderRev(ByVal BinString As String) As String 'converts strings into ASCII character string
8. Show that you are bilingual, e.g. by using QBASIC and VB.NET constructs
Dim tempstring As String = Replace(BinString, " ", "") Dim tempbinstr As String Dim TempChar As Char
9. Do not re
-
1. Always repeat obvious things twice.
Enum NumberSize
Bit_8 = 8
Bit_16 = 16
Bit_32 = 32
Bit_64 = 64
Bit_128 = 128
End Enum2. Hide all comments behind visible portion of screen - your co-workers do not need them.
Public Function CBin_Number(ByVal InputNumber As Double, ByVal InputNumberSize As NumberSize) As String 'converts large numbers into strings 8, 16,32, 64, or 128 characters long
Dim FinalBinNumber As String3. Show that 128 zeros and ones can be easily fit in 48 bits mantissa.
Dim TempNumber As Double = InputNumber Dim PlaceCounter As Integer
4. Always remind that computer cannot be trusted even with simple operation of subtraction.
Select Case InputNumberSize Case NumberSize.Bit\_8 PlaceCounter = 7 Case NumberSize.Bit\_16 PlaceCounter = 15 Case NumberSize.Bit\_32 PlaceCounter = 31 Case NumberSize.Bit\_64 PlaceCounter = 63 Case NumberSize.Bit\_128 PlaceCounter = 127 End Select
5. Do not listen anybody (even yourself) and choose most interesting way to handle the task.
If (TempNumber - (2 ^ PlaceCounter)) > 0 Then PlaceCounter = 127 End If
6. Be simple – the String class is the best thing since sliced bread.
Do Until (PlaceCounter < 0) If (TempNumber - (2 ^ PlaceCounter)) >= 0 Then FinalBinNumber = FinalBinNumber & "1" TempNumber = TempNumber - (2 ^ PlaceCounter) ElseIf (TempNumber - (2 ^ PlaceCounter)) < 0 Then FinalBinNumber = FinalBinNumber & "0" End If PlaceCounter = PlaceCounter - 1 Loop
7. Always add checks for weird cases
If FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" Then FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 or Error: Number Too Large" End If Return FinalBinNumber
End Function
Public Shared Function ReaderRev(ByVal BinString As String) As String 'converts strings into ASCII character string
8. Show that you are bilingual, e.g. by using QBASIC and VB.NET constructs
Dim tempstring As String = Replace(BinString, " ", "") Dim tempbinstr As String Dim TempChar As Char
9. Do not re
Hi, horrible indeed... :doh: But why would someone be 'popular among his colleagues' when programming like this? I'd rather think Hey, this is a guy who is not really good in his profession when I read things like that in someone else's code. And this means: Much to do for colleagues (refactoring, hard to read/understand) and much potential trouble (likelihood of errors is high, maintainability is hard) - so he makes his colleagues lifes harder... :confused: Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software. -
Hi, horrible indeed... :doh: But why would someone be 'popular among his colleagues' when programming like this? I'd rather think Hey, this is a guy who is not really good in his profession when I read things like that in someone else's code. And this means: Much to do for colleagues (refactoring, hard to read/understand) and much potential trouble (likelihood of errors is high, maintainability is hard) - so he makes his colleagues lifes harder... :confused: Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software. -
1. Always repeat obvious things twice.
Enum NumberSize
Bit_8 = 8
Bit_16 = 16
Bit_32 = 32
Bit_64 = 64
Bit_128 = 128
End Enum2. Hide all comments behind visible portion of screen - your co-workers do not need them.
Public Function CBin_Number(ByVal InputNumber As Double, ByVal InputNumberSize As NumberSize) As String 'converts large numbers into strings 8, 16,32, 64, or 128 characters long
Dim FinalBinNumber As String3. Show that 128 zeros and ones can be easily fit in 48 bits mantissa.
Dim TempNumber As Double = InputNumber Dim PlaceCounter As Integer
4. Always remind that computer cannot be trusted even with simple operation of subtraction.
Select Case InputNumberSize Case NumberSize.Bit\_8 PlaceCounter = 7 Case NumberSize.Bit\_16 PlaceCounter = 15 Case NumberSize.Bit\_32 PlaceCounter = 31 Case NumberSize.Bit\_64 PlaceCounter = 63 Case NumberSize.Bit\_128 PlaceCounter = 127 End Select
5. Do not listen anybody (even yourself) and choose most interesting way to handle the task.
If (TempNumber - (2 ^ PlaceCounter)) > 0 Then PlaceCounter = 127 End If
6. Be simple – the String class is the best thing since sliced bread.
Do Until (PlaceCounter < 0) If (TempNumber - (2 ^ PlaceCounter)) >= 0 Then FinalBinNumber = FinalBinNumber & "1" TempNumber = TempNumber - (2 ^ PlaceCounter) ElseIf (TempNumber - (2 ^ PlaceCounter)) < 0 Then FinalBinNumber = FinalBinNumber & "0" End If PlaceCounter = PlaceCounter - 1 Loop
7. Always add checks for weird cases
If FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" Then FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 or Error: Number Too Large" End If Return FinalBinNumber
End Function
Public Shared Function ReaderRev(ByVal BinString As String) As String 'converts strings into ASCII character string
8. Show that you are bilingual, e.g. by using QBASIC and VB.NET constructs
Dim tempstring As String = Replace(BinString, " ", "") Dim tempbinstr As String Dim TempChar As Char
9. Do not re
-
1. Always repeat obvious things twice.
Enum NumberSize
Bit_8 = 8
Bit_16 = 16
Bit_32 = 32
Bit_64 = 64
Bit_128 = 128
End Enum2. Hide all comments behind visible portion of screen - your co-workers do not need them.
Public Function CBin_Number(ByVal InputNumber As Double, ByVal InputNumberSize As NumberSize) As String 'converts large numbers into strings 8, 16,32, 64, or 128 characters long
Dim FinalBinNumber As String3. Show that 128 zeros and ones can be easily fit in 48 bits mantissa.
Dim TempNumber As Double = InputNumber Dim PlaceCounter As Integer
4. Always remind that computer cannot be trusted even with simple operation of subtraction.
Select Case InputNumberSize Case NumberSize.Bit\_8 PlaceCounter = 7 Case NumberSize.Bit\_16 PlaceCounter = 15 Case NumberSize.Bit\_32 PlaceCounter = 31 Case NumberSize.Bit\_64 PlaceCounter = 63 Case NumberSize.Bit\_128 PlaceCounter = 127 End Select
5. Do not listen anybody (even yourself) and choose most interesting way to handle the task.
If (TempNumber - (2 ^ PlaceCounter)) > 0 Then PlaceCounter = 127 End If
6. Be simple – the String class is the best thing since sliced bread.
Do Until (PlaceCounter < 0) If (TempNumber - (2 ^ PlaceCounter)) >= 0 Then FinalBinNumber = FinalBinNumber & "1" TempNumber = TempNumber - (2 ^ PlaceCounter) ElseIf (TempNumber - (2 ^ PlaceCounter)) < 0 Then FinalBinNumber = FinalBinNumber & "0" End If PlaceCounter = PlaceCounter - 1 Loop
7. Always add checks for weird cases
If FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" Then FinalBinNumber = "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 or Error: Number Too Large" End If Return FinalBinNumber
End Function
Public Shared Function ReaderRev(ByVal BinString As String) As String 'converts strings into ASCII character string
8. Show that you are bilingual, e.g. by using QBASIC and VB.NET constructs
Dim tempstring As String = Replace(BinString, " ", "") Dim tempbinstr As String Dim TempChar As Char
9. Do not re
-
What wrong with this
notmasteryet wrote:
Enum NumberSize Bit_8 = 8 Bit_16 = 16 Bit_32 = 32 Bit_64 = 64 Bit_128 = 128 End Enum
I think it is good practice. Do you have any better practice ?
Enum NumberSize
EightBits = 8
SixteenBits = 16
ThirtyTwoBits = 32
SixtyFourBits = 64
HundredTwentyEightBits = 128
End Enum:) IMHO in this particular case, there is no effective use of the enumeration (see #4). There is no checks on invalid enumeration values either; most common call of the CBin_Number function was performed with argument
CType(size, NumberSize)
wheresize
is an integer. It is hard to find best practices for enumerations since it just grouped constants. Usually all enumeration replaced by the Strategy Pattern during refactoring (see http://sourcemaking.com/refactoring/replace-type-code-with-state-strategy[^] or http://www.jeremyjarrell.com/archive/2007/10/28/64.aspx[^]). Type casting to enumeration datatype does not do validation, so a callie have to validate enumaration value by itself. However, by decalaring enumeration asEnum NumberSize
Bit_8
Bit_16
Bit_32
Bit_64
Bit_128
End Enumyou may perform range (in)validation
InputNumberSize < NumberSize.Bit_8 OrElse InputNumberSize > NumberSize.Bit_16
, it also allows compliler to optimizeSelect
statement code execution. Do you think change NumberSize to an integer type would introduce more problems?