按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
The word tested; “allodium;” contains the characters allo。 The verification will succeed;
and this is an example of a false positive。
■Note It is important to have many tests that verify a multitude of different scenarios。 You should have tests
that are supposed to succeed and those that are supposed to fail。
The conclusion is that neither of the solutions works properly。 With extended testing; each
solution created new problems。 We need to find another solution。
DEVELOPMENT FRUSTRATIONS
It would seem that creating solutions and then creating tests that nullify the solution is a lesson in frustration。
You create code that does not solve the problems。 What you must realize and take to heart is that this is part
of the software development process。 Yes; some people write code and don’t worry about the tests; but those
developers give software development a bad name。
You want to be a trustworthy developer who tests your code。 I have been; and my wife is; a software
development manager。 In her words; “I can deal with slow developers; but I cannot deal with developers who
I cannot trust to write stable and robust code。 The problem with untrustworthy developers is that I cannot let
them release code into production and always have to have somebody looking over their shoulders。”
Writing the Tests Before Writing the Code
The reason the previous solutions failed is because each solution was a knee…jerk reaction。
Knee…jerk reactions are those where when you encounter a bug; you fix the bug—no more and
no less。 Instead; you should figure out what the bug is trying to tell you。 The original bug with
the leading whitespace was not a whitespace bug; but a bug that was saying; “Hey; what if my
text is not aligned; or part of a sentence; and so on?”
To solve the bug; you don’t write code; but you think of all of the tests that your code needs
to pass。 You need to assign responsibility and define contexts that succeed and fail。 In the
translation example; the appropriate implementation approach would be to write the tests
before writing the code。 Table 3…1 shows the contexts that fail and succeed。
Table 3…1。 Appropriate Tests for the Translation Program
Test Verification Result
allo Success
〃 allo 〃 Success
word allo Fail: you can’t translate a single word without translating the other words
word allo word Fail: same reason as with word allo
prefixallo Fail: different word
…………………………………………………………Page 86……………………………………………………………
64 CH AP T E R 3 ■ L E A R N IN G AB OU T ST R I N G M A N I P U L AT IO N S
Table 3…1。 Appropriate Tests for the Translation Program (Continued)
Test Verification Result
alloappend Fail: different word
prefixalloappend Fail: different word
As you can see in the table; most test cases are failures because the translation ponent
is meant to translate single words only。 The test cases seem plete; but in fact; we are missing
one more set of cases; which are outlined in Table 3…2。
Table 3…2。 The Missing Test Cases for the Translation Program
Test Verification Result
Allo Success
〃 allO 〃 Success
Text can contain mixed cases; and from a human perspective; mixed case is still the same
word。 However; the puter considers mixed case as a pletely different buffer; and so we
must be able to cope with this situation。
Figure 3…9 shows the working solution。
Chaining ToLower() and
Trim() together first obtains ToLower() returns a
the result of ToLower() and lowercase copy of
then applies Trim() to it the original string
Public Class Translator
Public Shared Function TranslateHello(ByVal input _
As String) As String
Dim temp As String = input。ToLower()。Trim()
If temp。pareTo(〃hello〃) = 0 Then
Return 〃hallo〃
ElseIf temp。pareTo(〃allo〃) = 0 Then
Return 〃hallo〃
End If Trim() removes whitespace
Return 〃〃
End Function
End Class
Figure 3…9。 The final translation solution
…………………………………………………………Page 87……………………………………………………………
CH AP T E R 3 ■ L E AR N IN G AB O U T ST R I N G M A N I PU L A TI O N S 65
Looking at the solution; you see that there are elements from the first solution; which was
discounted because it did not work properly。 This is an example of how quickly solving a bug;
seeing that code fail; and discounting the solution can be incorrect。 You need to think through
why something fails; and not just fix the bug to get rid of it。
■Note All of the solutions involved use String type methods。 The String type is very sophisticated and
supports many operations that are monly performed on text。
At this point; we are finished with the translation of the greeting。 Next; I’ll point out a couple of
additional items that you need to keep in mind when dealing with strings。
Quoting Strings
Along with strings; Visual Basic has the Char type。