按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
Dim ticket As New Ticket( _
New DateTime( _
Integer。Parse(dateSplit(0)); _
Integer。Parse(dateSplit(1)); _
Integer。Parse(dateSplit(2))); _
New Integer() { _
Integer。Parse(splitUpText(1)); _
Integer。Parse(splitUpText(2)); _
Integer。Parse(splitUpText(3)); _
Integer。Parse(splitUpText(4)); _
Integer。Parse(splitUpText(5)); _
Integer。Parse(splitUpText(6))}; _
Integer。Parse(splitUpText(7)))
_tickets。Add(ticket)
Loop
Return 〃〃
End Function
End Class
Let’s examine how the implementation works。
Borrowing Code to Solve Another Problem
The borrowed code is the implementation of the Process() method; shown here in abbrevi
ated form:
Public Function Process(ByVal input As String) _
As String Implements IProcessor。Process
Dim reader As TextReader = New StringReader(input)
Do While reader。Peek …1
Dim splitUpText As String() = reader。ReadLine。Split(New Char() {〃 〃c})
Dim dateSplit As String() = splitUpText(0)。Split(New Char() {〃。〃c})
Dim ticket As New Ticket( _
'。 。 。
Loop
Return 〃〃
End Function
…………………………………………………………Page 424……………………………………………………………
402 CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q
Other than the new _tickets。Add(ticket) line; the code is identical to the Text2Binary。
Process() implementation。 The bolded code adds the lottery draw to the list of drawn numbers。
Once the tickets have been instantiated; they can be added to the list of tickets that will be
queried and searched。
CODE REUSE
The borrowed code demonstrates code reuse through copy and paste。 Realize that code that was used to
process binary objects is now being used in a pletely new context。
The copied and pasted code reuses classes and functionality from another problem context。 You copied
and pasted the functionality to parse and instantiate the Ticket type; but reused the Ticket type itself。
When we worked on the code for the Ticket type; you probably had no idea that we would reuse the
same code。 In fact; even I had no idea that we would reuse the code。 I find this happens a lot in my projects。
So how does this work? I write code to fulfill two criteria: minimal to solve the task and general enough
to not restrict further usage。 So I don’t actually design for code reuse; and I don’t have code reuse on my mind。
What I have on my mind is to design code in such a way that it could potentially be reused。
Let’s look at that code that could have solved the frequency problem previously。
Dim splitUpText() as String = lineOfText。Split(New Char() { 〃 〃c })
frequency(Integer。Parse(splitUpText(0))) += 1
frequency(Integer。Parse(splitUpText(1))) += 1
。 。 。
Is this code efficient? No; even though the lines of code are minimal; the code itself is not efficient。 If I
wanted to use the same code to perform another frequency analysis; which could happen; I would need to
copy and paste yet again; and thus the code is not efficient。
Is this code general enough to be used in another context? Absolutely not; because to reuse the code;
you would need to copy and paste it; and do some slight alterations。
This is an excellent example of code that can be written very quickly and is very effectively used by copying
and pasting it everywhere。 You are productive and can solve a problem quickly; but it cannot be easily extended or
maintained。 Imagine finding a bug and having copied and pasted the code ten times。 That would mean you
would need to find the ten different locations and see if the bug exists in those different locations。
Using LINQ
To find the frequency of a specific number; you don’t need to use LINQ。 In fact; LINQ can
always be avoided by using Visual Basic code。 So then why use LINQ? The reason is that LINQ
makes it easier for you to write plicated search queries that are agnostic of the source。 An
example is two versions of the code used to solve the frequency problem: one that is not reus
able and one that is reusable。 The code that is not reusable is the query without LINQ; and the
reusable code is the query with LINQ。
So let’s look at the frequency code that is not reusable。
…………………………………………………………Page 425……………………………………………………………
CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q 403
Function FrequencyOfANumberNotReusable(ByVal numberToSearch As Integer) _
As Integer
Dim runningTotal As Integer = 0
For Each aTicket as Ticket in _tickets
If aTicket。Numbers(0) = numberToSearch OrElse _
aTicket。Numbers(1) = numberToSearch OrElse _
aTicket。Numbers(2) = numberToSearch OrElse _
aTicket。Numbers(3) = numberToSearch OrElse _
aTicket