梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
it as a search solution�察�where a method iterates over a string and attempts to match elements
of a buffer to some text that is being searched。 The work´in´progress code is shown in Figure 3´8。
The test code is not shown here�察�because it is similar to that for the previous solution�察�with
the difference being the method being tested。
´´´´´´´´´´´´´´´´´´´´´´Page 84´´´´´´´´´´´´´´´´´´´´´´´
62 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
IndexOf�┌� attempts to match the
text ^allo ̄ in the variable buffer。
Work´in´progress
If the text is found�察�an offset
method
that is not ´1 is returned。
Public Function FindSubstring��ByVal buffer As String��_
As String
If buffer。IndexOf�─�allo;�� ´1 Then
Return Translator。TranslateHello�─�allo;��
End If
Return ;;
End Function IndexOf�┌� returns the index of the
first matched character�察�which is 2。
Visual Basic will always start
a l l o counting with the value 0。
Index 0 1 2 3 4 5
Figure 3´8。 Finding a substring to solve the whitespace problem
Which Is the Best Solution�拭�
Take a moment to think about which is the best solution�此�trimming the whitespace or finding a
substring。 The answer is neither is perfect�察�each solution has its problems。 This is a very mon
occurrence when you are developing software。 You think you have all the corners covered�察�and
then another scenario causes your software to fail。
Again�察�I want to stress that you need to write more tests to figure out which scenarios might
cause your software to fail。 For the solution of trimming whitespace�察�writing another test causes
it to fail�察�as shown in the following code。 It cannot be tweaked to make the test succeed。
Dim verifyValue As String = TrimmingWhitespace�─�a allo;��
If verifyValue。pareTo�─�hallo;�� 0 Then
Console。WriteLine�─�Test failed�此�cannot parse multiple words;��
End If
In the test�察�the leading ^a ̄ is considered the first character and is not trimmed。 The verification
will fail because pareTo�┌� cannot verify the misaligned buffer caused by the leading ^a。 ̄
If the new test were executed against the substring solution�察�it would succeed and find the
word ^allo。 ̄ Because the new test caused the first solution to fail�察�it would seem that solution is
a no´go。 Our confidence has been increased in the second solution because the old test and the
new test did not fail。 But don¨t be too hasty to consider that as the best path。 The substring
solution fails with the following test。
Dim verifyValue As String = FindSubstring�─�allodium;��
If verifyValue。pareTo�─�hallo;�� 0 Then
Console。WriteLine�─�allodium to hallo test failed;��
End If
´´´´´´´´´´´´´´´´´´´´´´Page 85´´´´´´´´´´´´´´´´´´´´´´´
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 63
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