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
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 其他
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 其他
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 其他
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 其他
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 其他
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 其他
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 文学名著
- 最新章:第230章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 其他
- 最新章:第27章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 其他
- 最新章:第28章