按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
develop code; you have multiple possible ways to solve a problem。 If you start messing around
with the original source code; by the time you reach the third or fourth solution; the code might
be a plete mess。 Your fixes might make things worse; and trying to backtrack in your source
code bees very difficult。
■Note To manage your source code; you should use version control。 However; even with version control;
when you delete past attempts; ideas get lost。 Thus; while your source code will be clean; you might forget
something you did three or four hours ago。 Trust me; this happens; because developing source code is an
intensive thought process。
The solution is to create a shim that calls the TranslateHello() method。 The shim is used
to fix the bug。 The following shim code is a temporary solution:
Public Function TrimmingWhitespace(ByVal buffer As String) As String
Return Translator。TranslateHello(buffer。Trim())
End Function
TrimmingWhitespace() is a method that trims whitespace from the string to be translated。
buffer。Trim is new functionality that preprocesses the buffer。 Finally; we call the original
method; TranslateHello(); to perform the translation。
Of course; we need to test the new method to see if it trims the string to be translated。 The
following is the corresponding test code。
Dim verifyValue As String = TrimmingWhitespace(〃 allo〃)
If verifyValue。pareTo(〃hallo〃) 0 Then
Console。WriteLine(〃Extra white spaces allo to hallo test failed〃)
End If
The test calls the work…in…progress method; TrimmingWhitespace(); to see if everything
works。 The verification code does not change。
So that you get predictable results; remember to call the shim and not the original method。
If you run the test code; you’ll see that the shim works; and thus we have a solution。
Finding a Substring
Another solution to the whitespace problem is to find a specific substring in a buffer。 Think of
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