Reconstruction of (a)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Writing in the last final, this is learned in the face of the last things, origin of this five article, is within three days of learning, is because the ThoughtWords Homework. This part of the contents of the can be learned in November this blog, in accordance with the order is almost like that, in accordance with the idea of agile software development step by step over, but meta programming in general Ruby Ruby is wonderful, wonderful. And this also is in a phone interview, asked a few books which are of the order of Homework. In general, you can learn a lot of things are really good, although not much chance.

So, then the code part, a part of the said, overall reconstruction is very interesting. If the reconstruction let me for what I did for a definition of the words, that is the following two contents:

The basic part

Repalce Subclass with Fields (by the field instead of the subclass)


The original structure is like that shown in the above, please forgive me I can't use the code to describe, but on the whole is about the same, the structure theory of the original or good, the problem is the only difference only in the "return quantity data".
The original is to take into account the late, when different books have different attributes, or the need of different operations, later, found in another part of the system optimization, it is beneficial to the system expansion, it is that we can add a new book in, instead of adding a new class, add a new class, will only make the system more and more complex. (reprinted retained Phodal's Blog Phodal's zenthink)

Originally considered using the adapter pattern to solve this problem, the problem is that looks like will only complicate matters.

Decompose Conditional (decomposition conditions)

This thing with so much, only the original condition is complex, first posted the refactored code.
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me

  1. def is_weekend(sum)
  2. sum>5 && sum<=7
  3. end
  4. def is_weekday(sum)
  5. sum<=5 && sum >0
  6. end

It is an expression used to determine the conditions, but do not know the system overhead here will much more than the original, the problem is that you can make the system easier to maintain, with code
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me

  1. if is_weekend(sum)
  2. day.weekend+=1
  3. elsif is_weekday(sum)
  4. day.weekday+=1

However, let me feel a little make an unnecessary move feeling, but readability is much better than the original, for the complex system is quite favourable.
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me

  1. if sum>5 && sum<=7
  2. day.weekend+=1
  3. elsif sum<=5 && sum>0
  4. day.weekday+=1

However, it is also suitable for another principle mentioned in the reconstruction of a Book.

Replace Magic Numer with Symbolic Constant (substituted magic number to a literal constant)

On the original code, sometimes we may not be able to understand the 0, 5, 7 is doing, but if put in
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me

  1. is_weekend()
  2. is_weekday()

Function, is simple.

Extrace Method (refined function)

This is inside with more bad, for example, is a function of it, in other words, is to let the function has real significance .
Originally, the price is compared with function;
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me

  1. compare()

The problem is when we compare, we still need to make a few different value value, and several different; also need to compare, so the function has a minimum value independent several out, such as
[ruby] view plaincopyprint?To see the code in CODEA piece of code to me

  1. get_costsmin()
  2. list_costsmin()
  3. select_costsmin()

In other place, is a major function of the original into small function.
The other can see "to improve the design of existing code refactoring


A began to organize the code, let a person feel not very happy, and very easy to not start, several simple methods,
In general, the reconstruction is fun~~~
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Tobias at December 14, 2013 - 6:06 AM