The top of the bad taste is the duplicate code. Why it is so notorious. You can reply to B1 to see the "eliminate the Bad Taste" series of the opening article "Refactoring, enterprise application of the Bible", this article will not repeat the explanation, citing only the point of view:
Tip: For anything, the code should appear only once, and only once.
We'll show you how to eliminate several typical duplicate codes and how to develop a habit of eliminating duplicate code. repeating in the same class
The first is the existence of duplicate code in the same class, which is easiest to identify and easiest to solve. Look at the following example:
try {
executorservice.runtasks (...)
Timer.scheduleatfixedrate (
() =>saveprogress (Getprogress ()),
period, period
)
} finally {
executorservice.shutdown ()
timer.cancel ()
saveprogress (getprogress ())
}
Saveprogress (Getprogress ()) appears in two places, which can be solved by means of an extraction method.
try {
executorservice.runtasks (...)
Timer.scheduleatfixedrate (
() =>persistprogress (),
period, period
)
} finally {
executorservice.shutdown ()
timer.cancel ()
persistprogress ()
}
Def persistprogress () {
saveprogress (getprogress ())
}
If a class is super long and thousands of rows, it is difficult to recognize duplicates even in a class. This involves another bad taste "super-long Class", "eliminate the bad taste" followed by a special article to solve it. repeat under the same class tree
The second is repeated in different subclasses under the same class tree, which is more difficult to identify than the first one.
Class Child1 extends Parent {
def run () {
init ()
process ()
}
...
}
Class Child2 extends Parent {
def run () {
process ()
}
...
}
You can move the public part up to a common parent class by moving the method and the template method up.
Class Parent {
def run () {//Template Method
init ()
process ()
}
def init () {}
def process ()//no function body, Equivalent to a virtual function
}
Class Child1 extends Parent {
override def init () {...}
Override Def process () {...}
}
Class Child2 extends Parent {
override def process () {...}
}
repeat in an unrelated class
The third is in two completely unrelated classes, if not specifically found hard to find.
Class App1 {
val last3 = Scores.sort (). Take (3)
}
Class App2 {
val last10 = Scores.sort (). Take (10)
}
You can eliminate duplicates by first extracting the method and then moving the method to the newly created class.
Class App1 {
val last3 = SEQS.LASTN (scores, 3)}
Class App2 {
val last10 = SEQS.LASTN (scores, 10)
}
Object Seqs {//New tool class
def Lastn[e] (Seq:seq[e], n:int)
: seq[e] = {//return value type
seq.sort (). Take (n)
}
}
The above example is a two class that is not connected to the business.
It is possible that there is a business connection, or it is directly similar, at this time the public base class should be extracted and then reconstructed according to the second repetition.
Careful crossing will find that three types of repetition have a progressive relationship, away from the more and more distant, more and more difficult to identify, to solve is also. Repeating in unrelated classes can be resolved by refactoring to the first two, and then continuing refactoring. to develop good coding habits
It is not difficult to solve duplication, but it is difficult to find repetition. It is not difficult to find repetition, the difficulty is to cultivate the habit of discovering repetition. A yard before writing code to complete the function, will be accustomed to look around, whether someone has done the relevant functions, and I want to do can share code, it is easy to find duplicates.
Tip: If you want to move a point, then first understand the circle around her.
Not only to solve the repetition, and after a period of time, I found that the overall understanding of the system, unconsciously improved a lot.
In fact, and life is the same, the famous financial coach Bodoshefel in the "Puppy Money Daddy-teach you to achieve financial freedom" also stressed:
It is your responsibility for the future to constantly turn the private sector into a dominant area.
The private sphere is related to me in life, and the dominant field is directly or indirectly influenced by me. Encounter things can not be self-sweeping in front of the snow, need to actively participate, change passive as the initiative, so that there will be more opportunities, let us more and more good. Summary
I hope you crossing not only learned to eliminate duplication of skills, but also understand the text of the two important tips.
For anything, the code should appear only once, and only once.
If you want to move a point, first understand the circle around her. related articles
Reply B1 See the Bible for refactoring, enterprise applications Description of the sample code
A yard think to say how to write good code, you have to take the code to say things. But the display of the code on the phone screen is a big problem. In order to make sure that the sample code can be read clearly, one yard makes a few choices:
Use fragments instead of fully operational code, highlighting the focus
Language with Scala because she's simple and easy to read
Do not feel that you do not understand Scala, there is pressure, a yard will use her most approachable part, rest assured.
Tip: Scala is a language on the JVM platform with simple and extensible features, and now the very popular Big data processing framework Spark is developed in Scala, and it's not a loss to learn Scala.