Test Driven Development

Best Coding Practices Blog

In which I learn that I've always done Test Driven Development


Date : 2012-05-15
Test Driven Development is defined as a software development process that repeats a very short development cycle. The basic steps of this development cycle are creating a "failing automated test case" that defines the new feature or functionality. Next the code is written to accomplish this task that is then tested by the first step. The final step is to refactor the code to match standards and incorporate the new process into an existing code base or class structure.

When I first read that description it sounded like something I would need to learn so I did some reading. The more I read the more I think, “that's what I'm already doing”. I used to think that I needed to progress to being able to write more and more code between tests as this should be more productive. After seeing the general acceptance for the Test Driven Development process I've redirected some of my goals. The shorter development cycle is very fitting for the modern development environments. I could illustrate the steps with 3 code blocks:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strTestCompanyName as String = “This Company Name is Too Long”
Response.write(“String Shortened: “ & TrimWithEllipses(strTestCompanyName, 10))
End Sub

That page will not work is saved because the TrimCompanyName function is not yet written. This is the failing test case. Then add the function.
Code:
Function TrimWithEllipses(ByVal strName As String, ByVal MaxLen As Integer) As String
        If strName.Length > MaxLen Then
            Return Trim(Left(strName, MaxLen - 3)) & "…" ' Added the extra Trim call in case the string was trimmed to a blank space
        Else
            Return strName
        End If
    End Function

After adding the above code the previous Page_Load function will now work. The final step would be to take the TrimWithEllipses function and add it to a String Extension Class or some other existing code base.
Code:
Imports System.Runtime.CompilerServices

Module StringExtension
    <Extension()> _
    Function TrimWithEllipses(ByVal aString As String, ByVal MaxLen As Integer) As String
        If aString.Length > MaxLen Then
            Return Left(aString, MaxLen - 3) & "..."
        Else
            Return  aString
        End If
    End Function
End Module

And that is how I have been programming for years.

If you compare the modern development process to old multipass compiler development processes you realize that modern integrated development environments actually do a partial test compile after ever line is completed. The Syntax checking that is constantly watching for errors is running the first step of the old compile process repeatedly. With as much PC power as we have these days it makes sense to compile and test often instead of waiting for the end of the day as was often the case in the past. Even a very large code base will usually only take a matter of seconds to compile and test allowing for many more iterations of any development process.

Comments :

No comments yet
  • Search For Articles