Click here to Skip to main content
15,897,704 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Not null check Pin
Eddy Vluggen7-Jan-11 8:34
professionalEddy Vluggen7-Jan-11 8:34 
GeneralRe: Not null check Pin
Keith Barrow7-Jan-11 4:24
professionalKeith Barrow7-Jan-11 4:24 
GeneralRe: Not null check Pin
fjdiewornncalwe5-Jan-11 3:44
professionalfjdiewornncalwe5-Jan-11 3:44 
GeneralRe: Not null check Pin
Ciumac Sergiu5-Jan-11 5:03
Ciumac Sergiu5-Jan-11 5:03 
GeneralRe: Not null check Pin
GibbleCH5-Jan-11 8:50
GibbleCH5-Jan-11 8:50 
GeneralIsNullOrEmpty Pin
AspDotNetDev30-Dec-10 8:23
protectorAspDotNetDev30-Dec-10 8:23 
GeneralRe: IsNullOrEmpty [modified] Pin
OriginalGriff30-Dec-10 10:23
mveOriginalGriff30-Dec-10 10:23 
GeneralRe: IsNullOrEmpty Pin
AspDotNetDev30-Dec-10 11:29
protectorAspDotNetDev30-Dec-10 11:29 
For shame! String is a class and all class instances get their references passed around (so there is no large copy when using ByVal). When you do ByRef, you actually pass a reference to that reference, allowing the source variable (used by the caller) to be assigned something different. Take this for example:
VB.NET
Public Class Form1

    Private small As String = "a"
    Private large As String = "This was a really long string I shortened so I could paste it here easily..."
    Private Const MAX As Integer = 9999999
    Private startTime As DateTime
    Private endTime As DateTime
    Private smallDuration As Integer
    Private largeDuration As Integer
    Dim sb As System.Text.StringBuilder

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        sb = New System.Text.StringBuilder()
        GC.Collect()
        GC.WaitForPendingFinalizers()
        startTime = DateTime.Now
        For i As Integer = 0 To MAX
            sb.Append(FirstChar(large))
        Next
        endTime = DateTime.Now
        largeDuration = CInt(endTime.Subtract(startTime).TotalMilliseconds)


        sb = New System.Text.StringBuilder()
        GC.Collect()
        GC.WaitForPendingFinalizers()
        startTime = DateTime.Now
        For i As Integer = 0 To MAX
            sb.Append(FirstChar(small))
        Next
        endTime = DateTime.Now
        smallDuration = CInt(endTime.Subtract(startTime).TotalMilliseconds)


        MessageBox.Show(String.Format("Small: {0}, Large: {1}", smallDuration.ToString(), largeDuration.ToString()))

    End Sub

    ' TODO: Try ByRef and ByVal, and compare them.
    Private Function FirstChar(ByRef str As String) As String
        Return str.Substring(0, 1)
    End Function

End Class


The timings in that example will always be very similar, whether you use a large string, a small string, ByRef, or ByVal. Using ByRef when you don't actually intend to treat that variable as an output from the function would be a coding horror!

But don't stop there. There are plenty more horrors in the IsNullOrEmpty function. Smile | :)

GeneralRe: IsNullOrEmpty Pin
OriginalGriff30-Dec-10 21:01
mveOriginalGriff30-Dec-10 21:01 
JokeRe: IsNullOrEmpty Pin
fjdiewornncalwe2-Jan-11 4:23
professionalfjdiewornncalwe2-Jan-11 4:23 
GeneralRe: IsNullOrEmpty Pin
PIEBALDconsult6-Jan-11 2:29
mvePIEBALDconsult6-Jan-11 2:29 
GeneralRe: IsNullOrEmpty Pin
fjdiewornncalwe6-Jan-11 3:58
professionalfjdiewornncalwe6-Jan-11 3:58 
GeneralRe: IsNullOrEmpty Pin
_Erik_3-Jan-11 3:43
_Erik_3-Jan-11 3:43 
GeneralRe: IsNullOrEmpty Pin
fjdiewornncalwe3-Jan-11 4:58
professionalfjdiewornncalwe3-Jan-11 4:58 
GeneralRe: IsNullOrEmpty Pin
_Erik_3-Jan-11 5:16
_Erik_3-Jan-11 5:16 
GeneralRe: IsNullOrEmpty Pin
fjdiewornncalwe3-Jan-11 5:34
professionalfjdiewornncalwe3-Jan-11 5:34 
GeneralRe: IsNullOrEmpty Pin
AspDotNetDev3-Jan-11 6:09
protectorAspDotNetDev3-Jan-11 6:09 
GeneralRe: IsNullOrEmpty Pin
fjdiewornncalwe3-Jan-11 15:03
professionalfjdiewornncalwe3-Jan-11 15:03 
GeneralRe: IsNullOrEmpty Pin
PIEBALDconsult6-Jan-11 2:34
mvePIEBALDconsult6-Jan-11 2:34 
GeneralRe: IsNullOrEmpty Pin
_Erik_6-Jan-11 4:31
_Erik_6-Jan-11 4:31 
GeneralRe: IsNullOrEmpty Pin
PIEBALDconsult6-Jan-11 15:35
mvePIEBALDconsult6-Jan-11 15:35 
GeneralSubmission for the Guiness book of world records Pin
Michael Agroskin29-Dec-10 5:38
Michael Agroskin29-Dec-10 5:38 
QuestionRe: Submission for the Guiness book of world records Pin
AspDotNetDev29-Dec-10 7:08
protectorAspDotNetDev29-Dec-10 7:08 
AnswerRe: Submission for the Guiness book of world records [modified] Pin
Michael Agroskin29-Dec-10 10:22
Michael Agroskin29-Dec-10 10:22 
GeneralRe: Submission for the Guiness book of world records Pin
TorstenH.3-Jan-11 4:35
TorstenH.3-Jan-11 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.