[Java] Refactoring_Memo_20200819

3 minute read

Refactoring

1. Program improvement

Program development is a very complicated act, and we proceed with trial and error. There are various methods, open a review and decide what should be improved, whether you can proceed with development as it is, etc. I will check with the customer and proceed.

Also, because of the deadline and the deadline is approaching There may be times when you have to choose. *If it is not possible to make maximum efforts to keep the deadline

Therefore, ** If left unconscious, the program under development will become more and more complicated. ** From a later point of view, I myself wonder, “Why did you write such an incomprehensible code?” It is not uncommon for even a skilled engineer. An example is when you increase the number of similar classes by copy and paste and it becomes difficult to fix them later.

However, the more complicated the code is, the more difficult it is to fix. The more complicated a program is, the lighter it initially is, but the more it becomes like a snowman over time. ▽ The complexity will increase and you will not be able to touch it at the end. → It would be difficult for companies to hire talented engineers just for that reason.

――Therefore, an activity called refactoring is recommended in which the code is reviewed and improved from time to time.

Complex programs…

・I don't understand why the code is messy ・Difficult to understand when viewed by people other than creators and designers ・It may be different because it is separated from the processing design ・It is easy to see the code because only the ones that remain are left. ・It is easy for the contents to come in even if viewed by people other than the team or creators and designers ・Completion according to design **2. What is refactoring** Strictly speaking, refactoring means "**improving the internal structure without changing the behavior and specifications seen from the outside**". Yes. For example, when refactoring the test class, another class that calls the test class will be greatly affected. We will not modify (change the operation contents of the method to be completely different). ――The term refactoring refers to activities rather than special writing. Before the concept of refactoring was born, the programs that were created and operated normally were The idea that it is better not to touch has permeated. ――Because, when you make a fix and a bug comes out, the fix work spreads to the whole project and becomes uncollectible Because there is a possibility. However, it is a daily occurrence that specification changes and requirements change in program development, For that reason, refactoring activities gradually expanded. **3. Typical refactoring activities** The following are typical refactorings. (1) ** Combine overlapping parts in one place ** If similar code parts appear in multiple places due to copy and paste, etc., I will make it possible to call it all in one method. ② **Split methods and classes** Cut out a part of the method that is too large and complicated as another method. Usually, it will often be cut out as a private method. ③ ** Use external library and new Java version syntax ** By using an external library and a new grammar, the processing that was written using a large number of lines It may be simple. Of course, use only the libraries that you can use. **4. Refactoring notes/disadvantages** Basically, it's a good thing to do more and more refactoring. However, refactoring also takes time (= cost). Therefore, if you are unlikely to get a benefit that exceeds the cost, In some cases, "even if the code has low maintainability, it should not be refactored." **Note ① It is difficult to obtain the benefits of refactoring just before the delivery date** Even if you refactor with the deadline approaching immediately, the product will immediately Once completed, you will not be able to take advantage of the advantage that “it will be easy to modify”. This doesn't seem so good for the author who wants to release it after making it perfect. → It might be better to do... In short, it is often the case that it is better to properly perform verification work such as testing if the delivery date is at the limit. ―― **Note ② Do not do too much, do not overdo ** Like a test, if you want to do refactoring activities, you can endlessly do it. Also, if you look at the design that was thought out to be perfect at the time of refactoring in a few months It may not be optimal. Time is not infinite, so refactoring should be done appropriately to avoid wasting cost and effort. It is important to do it.

Tags:

Updated: