Refactoring old code
Tags: webdev
Published: 2023-02-18 14:30:50

Hey what is up guys this is Ayden from  HZ University of Apply science and toady im gonna try refactor some code myself.
First of all, reconstruct an application of the classical design ideas, design principles, design patterns and programming specifications we have learned. I often write some code about logic, but these are far less successful than modifying a piece of messy code or even code that I can't understand.
**Why use refactoring?**
For projects, refactoring can keep the code quality under control, including removing known bugs, improving the running efficiency of programs, and adding new functions.

**What tools do I use to find and modify code?**
I found an open source code in Github and opened it in IDEA.
No one should not know Github. May be?
GitHub is a hosting platform for open source and private software projects. Github, as an open source code library and version control system, is the first choice for managing software development. Github can host git libraries and provide a web interface at the same time.
IDEA is a Java integrated development environment tool software, which is recognized as the best Java development tool in the industry, especially in intelligent code assistant, automatic code prompt, refactoring, Java EE support, and various version tools. I like his refactoring function very much. He always gives me the best solution.

**> Here is a piece of code I found.**
Before modification:

```
if ( flag == 1 ){
    return true;
}
else{
    return false;
}

```
It looks too uncomplicated. IDEA tools help me automatically change it into a more concise form.

After modification:

```
return flag == 1;
```

Before modification:

```
public boolean isStartAfter(Date date) {
    Calendar calendar = BusinessCalendar.getCalendar();
    calendar.setTime(date);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    
    return ( (hour<fromHour)
             || ( (hour==fromHour)
                  && (minute<=fromMinute) 
                ) 
           );
  }
 
 
  public boolean includes(Date date) {
    Calendar calendar = BusinessCalendar.getCalendar();
    calendar.setTime(date);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    
    return ( ( (fromHour<hour)
               || ( (fromHour==hour)
                   && (fromMinute<=minute) 
                 )
             ) &&
             ( (hour<toHour)
               || ( (hour==toHour)
                    && (minute<=toMinute) 
                  )
             )
           );
  }

```
Through observation, I found that this old code contains repeated parts, so I used methods to replace these repeated expressions.


After modification:

```
 private boolean tailGreatHead(int headHour, int headMinute, int tailHour,
int tailMinute, boolean includeEqual) {
    boolean tailGreatHeadHour = (headHour < tailHour);
    boolean tailEqualHeadHour = (headHour == tailHour);
    boolean tailGreatHeadMinute = (headMinute < tailMinute);
    boolean tailEqualHeadMinute = (headMinute == tailMinute);
 
    boolean tailGreatEqualHeadMinute = tailGreatHeadMinute || includeEqual
&& tailEqualHeadMinute;
 
    return (tailGreatHeadHour || (tailEqualHeadHour && tailGreatEqualHeadMinute));
  }
 
  private boolean tailGreatHead(int headHour, int headMinute, int tailHour,
int tailMinute) {
    return tailGreatHead(headHour, headMinute, tailHour, tailMinute, false);
  }
 
  private boolean tailGreatEqualHead(int headHour, int headMinute,
int tailHour, int tailMinute) {
    return tailGreatHead(headHour, headMinute, tailHour, tailMinute, true);
  }
 
  public boolean isStartAfter(Date date) {
    Calendar calendar = BusinessCalendar.getCalendar();
    calendar.setTime(date);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    
    return this.tailGreatEqualHead(hour, minute, fromHour, fromMinute);
  }
 
 
  public boolean includes(Date date) {
    Calendar calendar = BusinessCalendar.getCalendar();
    calendar.setTime(date);
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    
    return this.tailGreatEqualHead(fromHour, fromMinute, hour, minute) &&
             this.tailGreatEqualHead(hour, minute, toHour, toMinute);
  }

```
Final conclusion
Refactoring involves many methods, some of which are confusion in definition, some contain duplicate code, and some even contain bugs. Through refactoring, we can modify the imperfect code without changing the interface to make its logic more precise and clear. In short, refactoring is a very important design pattern.

THANKS!
    THE END