What's Wrong With This Code? (#15)

Mortimer is taking up TDD, and starts a new project by writing the following test.

<TestMethod()> _
Public Sub CanReverseString()

    Dim input As String = "OdeToCode"
    Dim result As String = Utility.ReverseString(input)
    Assert.AreEqual("edoCoTedO", result)

End Sub

Yes, it's another one of those big projects that reverses strings all the time.

With a test in place, Mortimer writes the following stub.

Public Class Utility
    Public Shared Function ReverseString( _
        ByVal input As String) As String
 
        Return Nothing

    End Function
End Class

Good news - the test fails! Now it's time to provide a real implementation...

Public Class Utility
    Public Shared Function ReverseString( _
        ByVal input As String) As String

        Dim chars As Char()
        chars = New Char(input.Length) {}

        For i As Integer = 0 To input.Length - 1
            chars(i) = input(input.Length - i - 1)
        Next

        Return New String(chars)

    End Function
End Class

Mortimer thinks he has everything straight - but the test still fails! What could be wrong with Mortimer's ReverseString?

posted on Monday, April 30, 2007 9:12 PM by scott

Comments

Monday, April 30, 2007 7:40 PM by Christopher Steen

# Link Listing - April 30, 2007

Using enterprise architecture framework to map services and set their granularity [Via: nattYGUR ] Silverlight...
Monday, April 30, 2007 8:26 PM by JohnB

# re: What's Wrong With This Code? (#15)

uhhh, it works on my computer? Please let me know why it shouldn't.
Monday, April 30, 2007 9:46 PM by scott

# re: What's Wrong With This Code? (#15)

John:

You should notice an extra character in the reverse string.

An unprintable character...
Tuesday, May 01, 2007 5:00 AM by Jason

# re: What's Wrong With This Code? (#15)

in VB, chars should be initialized like this:
chars = New Char(input.Length-1) {}

Notice the "-1". This is different than C#.
Tuesday, May 01, 2007 7:31 AM by JohnB

# re: What's Wrong With This Code? (#15)

Ok, I agree that the init of the array includes an extra element but I wired this up with NUnit as well and the test passes. I do not get any unprintable characters.
Tuesday, May 01, 2007 12:10 PM by scott

# re: What's Wrong With This Code? (#15)

John:

That's very odd. Let's exchange project files. My address is scott at OdeToCode.com
Wednesday, May 02, 2007 10:32 AM by James Curran

# re: What's Wrong With This Code? (#15)

>> I do not get any unprintable characters.

If that random character happens to be a zero (which in theory should happen no less than 1 in 256 trials, but in practice probably occurs much more often), you will accidentally get a correct string.
Wednesday, May 02, 2007 6:19 PM by BusinessRx Reading List

# What Was Wrong With #15?

Not many takers on WWWTC #15 , but Jason finally nailed it. The bug in the code revolves around how VB
Monday, May 14, 2007 8:28 PM by John Bonano

# Array Initialization in VB and C#

I must admit that I was not aware there was a difference and I&amp;#39;m sure it&amp;#39;s elementary to some
Wednesday, May 16, 2007 10:35 PM by John Bonano

# Array Initialization in VB and C#

I must admit that I was not aware there was a difference and I&amp;#39;m sure it&amp;#39;s elementary to some
Friday, March 28, 2008 11:07 PM by John Bonano

# Array Initialization in VB and C#

Array Initialization in VB and C#
Friday, March 28, 2008 11:10 PM by John Bonano

# Array Initialization in VB and C#

Array Initialization in VB and C#
Saturday, March 29, 2008 11:10 AM by John Bonano

# Array Initialization in VB and C#

Array Initialization in VB and C#
Saturday, March 29, 2008 11:50 AM by John Bonano

# Array Initialization in VB and C#

Array Initialization in VB and C#
Saturday, March 29, 2008 11:52 AM by John Bonano

# Array Initialization in VB and C#

Array Initialization in VB and C#