One of my refactoring practices

Source: Internet
Author: User

Reconstruction, in our Program The design is really very important. Its role, or even magic, can make us Code Vibrant and elegant. Because refactoring cannot be the syntax of the program language, there are few introductions to refactoring on the market, especially the authoritative book that truly makes a splash, we can only keep practicing and thinking in every practice. Taking my own refactoring experience as an example. This time, I plan to output the transmitted string to the console in a certain way, so what I want to do is parse the string. This kind of work can be done by using regular expressions, but it is too confusing to say it is because of regular expressions. Basically, when I want to use it, it's always bad (in fact, it's a big bang !). In addition, this time with the support of the org. JSON library, this work can be said to be relatively convenient. Let's just look at how I feel the beauty of refactoring when I am doing this thing. Because I am a newbie, it is really confusing to write code at the beginning. Therefore, every time I write the code, I will read it again and change it again, even if it can pass functional testing. String: String STR =" [{\ "Title \": \ "Java book \", \ "author \": \ "JSON \", \ "rating \": 3 }, {\ "title \": \ "Java book \", \ "author \": [\ "JSON \", \ "Fuck \"], \ "rating \": 3}] "; This string expresses two books, each with a title, author, and rating. What I want to do is to parse the information from the book. I believe everyone has noticed that the author of the book can be a string array or not. Then we start parsing. I am using Java and the compiler is eclipse. I wrote this method at the beginning: Book = New Book (); Book. parse (STR ); Why is it book, because I want to write code for a test case that passes a separate book, and then I want to write more than one. Book is a class defined by myself, used to store book information. The following is the internal details of the book:

  Public   Class  Book {  Private  String title; Private   Int  Rating;  Private  String [] author;  Public  String gettitle (){  Return  Title ;}  Public   Int  Getrating (){  Return  Rating ;}  Public String [] getauthor (){  Return  Author ;}
}

 

The next step is to implement the parse method. The implementation of this method is not difficult, for example:
    Private   Static Book parse (string Str) Throws  Jsoexception {book = New Book (); jsonobject JSON = New  Jsonobject (STR); book = Parsesinglebook (book, JSON );  Return  Book ;}  Private   Static Book parsesinglebook (book, jsonobject JSON) Throws  Jsoexception {book. Title = JSON. getstring ("title" ); Book. Rating = JSON. getint ("rating"); Object authorobj = JSON. Get ("author" ); Book. Author = Parseauthorobjasauthor (book, authorobj );  Return  Book ;}  Private   Static String [] parseauthorobjasauthor (book, object authorobj) Throws  Jsoexception {  If (Authorobj Instanceof  Jsonarray) {jsonarray jsonauthor = (Jsonarray) authorobj;  Int Length = Jsonauthor. Length (); book. Author = New  String [length];  For ( Int I = 0; I <length; I ++ ) {Book. Author [I] = (String) jsonauthor. Get (I );}}  Else  {Book. Author =New String [1 ]; String singleauthor = (String) authorobj; book. Author [ 0] = Singleauthor ;}  Return  Author ;} 

 

This is the code for solving this issue in a single book. I have already reconstructed the code and it was originally written in a method, then put the code in a method according to the functions they want to implement. This method is the "extraction method". This makes our code more readable, which part of the implementation can be clear at a glance, and earnestly abides by the design principle of single responsibility of the Code. However, there is still a problem here. I believe everyone has noticed that the book parameter is passed in every method. This code is always confusing, that is, why should I transmit the book? So there is a question, is it necessary? In fact, there is really no need, because you can see through the code, the reason why a book is uploaded is because I need an initialized book class. Then I can initialize it in the method! The following code is as follows:
  Private   Static Book parsesinglebook (jsonobject JSON) Throws  Jsoexception {book =New  Book (); book. Title = JSON. getstring ("title" ); Book. Rating = JSON. getint ("rating" ); Object authorobj = JSON. Get ("author" ); Book. Author = Parseauthorobjasauthor (authorobj );  Return  Book ;}  Private   Static String [] parseauthorobjasauthor (Object authorobj) Throws Jsoexception {string [] author;  If (Authorobj Instanceof  Jsonarray) {jsonarray jsonauthor = (Jsonarray) authorobj;  Int Length = Jsonauthor. Length (); author = New  String [length];  For ( Int I = 0; I <length; I ++) {Author [I] = (String) jsonauthor. Get (I );}}  Else  {Author = New String [1 ]; String singleauthor = (String) authorobj; author [ 0] = Singleauthor ;}  Return  Author ;} 

 

Note that in the second method, I changed book. Author to author. That's because I noticed a problem, that is, what exactly does this method contain? It is nothing more than a string array! So of course, I can initialize a string array in it. Well, there is another worth noting in this method, that is, the judgment of the condition (authorobj instanceof jsonarray), which is actually to determine whether it is an array or not. Such a judgment is not identified by any mark, because there is no connection between the two! So we can do this, no matter what type it is, it is first set to an object, because no matter the array or not, it is essentially an object, and then use instanceof to determine whether it is an array instance. For more than one case, the above methods are basically called for implementation, because they are all the same and only need to use loops. This practice makes me deeply aware of the importance of refactoring. To be honest, at first I had a lot of methods. The two situations correspond to their own processing methods, but later I found that they are actually the same in the refactoring process. Algorithm So why can't we extract them as a method and call them? In this way, the logic of my code will be clearer. The parameter list is displayed. Basically, when you see more than one parameter list, you will start to think about whether there are too many parameters? Remember what the input and output of your method are. Just like the last method above, what I input is actually a string array, and what I output is a string array. So can we define an array in the method instead of passing in the parameter? This idea also applies to merge methods. Basically, as long as the input and output methods are the same, they may be merged. This practice also gives me a better understanding of object encapsulation. The encapsulation of objects. I believe that anyone who learns Java is familiar with it (well, I admit, I am not quite clear about the true meaning of the word ). The reason for encapsulation is to hide implementation details, but why? Generally, you can think of hiding users so that they can safely use classes or methods without worrying about specific implementations. This is indeed a packaging requirement, but it is based on the user perspective to think about the meaning of encapsulation, and what about the program designer? Is their encapsulation just like this? Our encapsulation encapsulates the implementation details of the code into a method or class, and then calls other methods or classes. This idea is better than passing the relevant variables to parameters, what are the benefits? The main reason is that if we upload an object, it will bring in other things of this object, but if it is a method call, it is just to call methods related to this class, and will not bring anything redundant. This topic has been mentioned for the moment. I think I need to think about it again. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.