Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
N

notmasteryet

@notmasteryet
About
Posts
11
Topics
2
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • 5 in a 1 to 10 range
    N notmasteryet

    leppie wrote:

    Yeah, now fixed, my brain needs rest now!

    ...too much VB code for that one

    The Weird and The Wonderful

  • How to be popular among your colleagues
    N notmasteryet

    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) where size 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 as

    Enum NumberSize
    Bit_8
    Bit_16
    Bit_32
    Bit_64
    Bit_128
    End Enum

    you may perform range (in)validation InputNumberSize < NumberSize.Bit_8 OrElse InputNumberSize > NumberSize.Bit_16, it also allows compliler to optimize Select statement code execution. Do you think change NumberSize to an integer type would introduce more problems?

    The Weird and The Wonderful csharp help tutorial

  • Percent-decoding a URL
    N notmasteryet

    If the code was written on C++ strNew might be a CString, if it was written on C# it might be StringBuilder.

    The Weird and The Wonderful question

  • Percent-decoding a URL
    N notmasteryet

    At first I thought he was smart to use Excel to generate code using ="strNew.Replace(""%" & DEC2HEX(A1) & """, """ & CHAR(A1) & """);" to save typing of boring code that anybody can type. :) But then I noticed case sensitive replacements that as possible to generate using following VB.NET code:

    For i As Integer = 32 To 126
    Dim ch As Char = Chr(i)
    If Not Char.IsLetterOrDigit(ch) Then
    Console.WriteLine("strNew.Replace(""%{0:X2}"", ""{1}"");", i, ch)
    If i Mod 16 > 9 Then
    Console.WriteLine("strNew.Replace(""%{0:x2}"", ""{1}"");", i, ch)
    End If
    End If
    Next i

    It's still less typing than traditional way to do stuff. Genius! :cool:

    The Weird and The Wonderful question

  • NULL Checking and Defensive Programming
    N notmasteryet

    You have good start with your superior¡ He is right about "Checking pointers for NULL throughout the code eats too many CPU cycles and seriously degrades performance". And, of course, pointer can be NULL. Most people agree: to check pointers for NULL on every parameter in public or protected member of the class, and private and internal members shall not waste CPU to check for something that by logic will never be null. If you not sure about something you wrote, insert Debug.Assert (C#), assert (Java), or ASSERT (MS C/C++). They will be excluded from production code and it will "... just to crash and provide a stack dump" during debugging/testing. Every code piece has to be executed during unit tests. And if you have something like if(obj == null) return false; and obj is never null according code coverage, then something wrong: either create test to cover that case (which will be impossible when it’s hidden inside private members), or just remove to not spend one CPU cycle :-). Check also http://channel9.msdn.com/shows/Going+Deep/Expert-to-Expert-Contract-Oriented-Programming-and-Spec/[^].

    The Weird and The Wonderful data-structures performance question

  • Avoid return statement in the middle - horror or not?
    N notmasteryet

    Be aware of Junioria Developerus animal. Those species may decide that PromptUser() && flagA && flagB && flagC looks prettier. :) Cyclomatic complexity for && and if is the same. Some people believe that cyclomatic complexity number is directly related to code maintenance. And in this thread we did not really lower the number. Is the original code fine as is?

    The Weird and The Wonderful question

  • Threading horror
    N notmasteryet

    Look at bright side: if you have two processors (or dual-core processor), the second one will be busy with something. :)

    The Weird and The Wonderful

  • Iterating an Enum?
    N notmasteryet

    You are right. Building of lookup table improves performance.

        static Dictionary<string, StopBits> parsedStopBits = new Dictionary<string, StopBits>();
    
        static ClassName()
        {
            foreach (StopBits sb in Enum.GetValues(typeof(StopBits)))
            {
                parsedStopBits.Add(sb.ToString(), sb);
            }
        }
    
        static StopBits stopBitsFromString3(string stopBitsAsString) 
        { 
            StopBits result; 
            if (!parsedStopBits.TryGetValue(stopBitsAsString, out result)) 
                result = StopBits.None;
            return result;
        }
    

    Difference between performance of this code and Enum.Parse one is 10 times faster. Difference between perfromance of this code and horror one is 100 times faster. It is not multiple return you should worry about. It's try-catch. If non-enum value name will be passed in the stopBitsFromString2 function, it's performance will be the same as stopBitsFromString's. Knowing when not to use try-catch is priceless. :)

    modified on Monday, December 15, 2008 12:32 PM

    The Weird and The Wonderful question

  • How to be popular among your colleagues
    N notmasteryet

    1. Always repeat obvious things twice.

    Enum NumberSize
    Bit_8 = 8
    Bit_16 = 16
    Bit_32 = 32
    Bit_64 = 64
    Bit_128 = 128
    End Enum

    2. 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 String

    3. 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

    The Weird and The Wonderful csharp help tutorial

  • Do not trust a computer...
    N notmasteryet

    Found in legacy code...

    Dim startbit As Integer = ((6 * data.i) + (3 * data.j) + 16)
    startbit = Math.Ceiling(startbit / 16) * 16

    If startbit Mod 16 <> 0 Then Throw New Exception("you don't know how to round")

    :) Always check what computer is doing

    The Weird and The Wonderful tutorial

  • I wonder!!!!
    N notmasteryet

    Try to compile following without first or second using:

    using System; // 1

    namespace MyCode.Console
    {
    using System; // 2

    class Program
    {
        static void Main(string\[\] args)
        {
            Console.WriteLine("Test");
        }
    }
    

    }

    The Weird and The Wonderful question csharp dotnet
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups