A summary of refactoring a function block

Source: Internet
Author: User

Because leader doesn't recommend taking the time to do this, it's basically overtime work.

In the process of doing this, IntelliJ idea is more familiar with, review the use of Spy,mock,verify,when in the Mocikitto, this refactoring is basically a small step fast walk, layer separation

Intelliij idea:
Refactoring technique: If you want to move a method to another class, you can add a class type parameter by means of the method, and then place the cursor on any character of the methods name "If the method name is selected, then F6 will fail", press F6, select the expected class in the popup dialog box. Because the parameters of this method are other types of objects "

When you change the number of parameters for a method, you can change the usage, move from top to bottom, or press CTRL+F6 to operate on the GUI interface of the call. IntelliJ idea's automatic operation often leaves some tails, for example, deleting a parameter of a constructor, assigning a parameter to a field and constructing an assignment statement that is not automatically deleted.

If the method parameter has multiple, and the parameter type is not very different, IntelliJ idea Alt+enter provides the modification may be wrong, for example, if you delete the 5th parameter, idea may delete the 4th parameter, then change the actual function of the parameter name, causing the parameter dislocation, will compile the error, This scenario is recommended for use with the Ctrl+f6 GUI interface.

When you move a method through F6 to another class, the move operation succeeds, and if you use the field from the original class, the method will compile. Do not worry about this situation, the general step is to move first, and then resolve the compilation error.

When moving a method, the class that the method adds does not typically choose an abstract method, which moves the method into an abstract method, and the correct approach is that the added type parameter is the subclass of the expected abstract class, so that it can be

When building a factory class or interface, you can write class Name first, and then use the shortcut key of idea to create a Class,interface,method,field

The common method of removing if else is that each branch is an object, then implements the same interface, and puts the judgment logic of the If Else branch into the factory class so that the code is clean.
Scalability and maintainability are increased, extensibility: if you increase or decrease a scene, modify only the factory class, or add or remove new classes, this unrelated scenario does not affect the implementation of other scenarios, and you do not have to re-test those scenarios that are not involved.
And the business logic and each scenario can write test cases independently. If there is duplication of code, not only can see, but also to get away. Duplicate code is extracted into the parent class. You can also define an interface, an abstract class to implement, and a business class to inherit an abstract class. Duplicate fields or methods are placed in an abstract class.

Shortcut keys in idea:
CTRL+ALT+F12: Open a package;
CTRL+F12: Open File Structure
CTRL+ALT+B: An implementation class for an interface, a subclass of an abstract class
Alt+f7: Find using Fields, methods
Ctrl+e: Recently used files, useful in F6 move operation
Ctrl+alt+leftarrow key: The last Action code location. Unfortunately, Remote Desktop is not available, you can only use the mouse to click on the toolbar button
Ctrl+shift: Moving a method or field or statement
When you press Alt+enter on the left or right object of the Equals method, Flip appears to swap
Ctrl+b go To Definition, then press CTRL+B, return
Alt+insert: constructors, Get,set,overrider
Ctrl+alt+t:surround with Try Catch
Press Ctrl+shift+t on a method or class to create a test case
Ctrl+p: Viewing method parameters
Ctrl+q: View API
SHIFT+F6: Renaming
Ctrl+alt+n: inline, that is, the independent method is integrated into the call, the inline method removes the
Press CTRL + Z in the class code of the refactoring operation to undo the action you just made
Compare with clipboard: First copy a piece of text, then select a text in idea, right-click on the selected text, select "Compare with Clipboard" To compare, identical: same, consistent
Ctrl+shift+f9:compile, to do in the module or project, the cursor in a class file, will only compile this file, if the reference to other non-apiclass, then compile but
Idea has a running test case that compiles a module or project to discover which classes are not compiled. Be sure to compile or run the test case first before committing the code

Git:
Git fetch//get updates on origin
Git merge//and Local code merge, if you have clifict, resolve to commit
git Add.//Add all local files
Git status-s//View the local file status, if there is no output, the instructions have been processed.
git commit-m "comment"//submit to local repository
Git push Origin master//commits to the master branch of the GIT server
git branch//view
git log--oneline-5

Review of mock use in test cases:
Spy, if you do not want to execute the spy method, use Doreturn, otherwise, the spy method will be executed
Just stick to an example,

Import Com.download.util.subnet;import Com.file.utils.stmimmanageimp;import Com.file.utils.fileinfoutil;import Com.api.util.debugprn;import Org.junit.before;import Org.junit.test;import Org.junit.runner.runwith;import Org.mockito.mock;import Org.mockito.runners.mockitojunitrunner;import Java.io.file;import java.io.IOException; Import static Org.hamcrest.core.is.is;import static org.junit.assert.assertthat;import static org.mockito.mockito.*; @RunWith (Mockitojunitrunner.class)//This must have, otherwise @mock will fail the public class Stmfileexportertest extends Abstractfileexporttest {//ut also need to remove duplicate private static final debugprn LOGGER = new Debugprn (StmFileExporterTest.class.getNa    Me ());    Private String TargetPath;    @Mock//Use Mock annotations to make the code clearer private Fileinfoutil Filestyle;    @Mock private Stmimmanageimp Immanageimp; @Before//Readiness public void init () throws IOException {TargetPath = StmFileExporterTest.class.getResource ("/com/f        ile/fileexporter/"). GetPath (); if (!new File (TargetPath). mkdirs()) {Logger.info (String.Format ("Path (%s) mkdirs failed!", TargetPath)); }} @Test public void should_dodataclearwork_deletetemppath_when_getbydatatype_is_true_and_savefile_not_exists () {//The method name embodies the scene try {when (Filestyle.gettaskname ()). Thenreturn ("Task1"); return value of//mock off method String Local            Temppah = TargetPath + filestyle.gettaskname () + File.separator + "" + file.separator;            Subnet spysubnet = Spy (Getgv3subnet ())//spy an object because the file in this object localtempfle = new file (Localtemppah);            if (!localtempfle.mkdirs ()) {Logger.info (String.Format ("Path (%s) mkdirs failed!", localtempfle));            } stmfileexporter Stmfileexporter = new Stmfileexporter (TargetPath, Filestyle);            Stmfileexporter spy = Spy (Stmfileexporter); Spy.setimmanageimp (IMMANAGEIMP);
             To match a parameter in a mock method, you use the EQ ();d Oreturn, and do not perform a mock method
Doreturn (True). When (Spy). Getdatabydatatype (eq (spysubnet), anystring (), anystring ());
            Spy.sync (spysubnet);            Verify (Immanageimp). Dodata ();            Verify (Immanageimp). Clearwork ();//equivalent to verify (Immanageimp,times (1)). Clearwork ()            
           The Assertthat method of Mockito can print mismatched expect and actual data, the expected data needs to use is (), and is () function is also very powerful
Assertthat (Localtempfle.exists (), is (false));
        } catch (Exception e) {            throw new IllegalArgumentException ("UT Fail", e);        }    }    @Test public    void Should_dodataclearwork_deletetemppath_when_type_not_equals_gv3 () {        try {            Subnet Spysubnet = Spy (Getnotgv3subnet ());            Stmfileexporter stmfileexporter = new Stmfileexporter (TargetPath, filestyle);            Stmfileexporter spy = Spy (Stmfileexporter);            Spy.sync (spysubnet);            Verify (Spy, times (0)). Dostmsync (spysubnet);        } catch (Exception e) {            throw new IllegalArgumentException ("UT Fail", e);}}}    

Tired and full of it.
That's all I remember.

A summary of refactoring a function block

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.