DEV Community

Ayden002
Ayden002

Posted on

Refactoring old code

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;
}

Enter fullscreen mode Exit fullscreen mode

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

After modification:

return flag == 1;
Enter fullscreen mode Exit fullscreen mode

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) 
                  )
             )
           );
  }

Enter fullscreen mode Exit fullscreen mode

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);
  }

Enter fullscreen mode Exit fullscreen mode

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

Top comments (2)

Collapse
 
huni profile image
瑞可 朱

Hi It's Huni here. Your post was really helpful for me understanding the refactoring of code but would you pls explain a bit more detaild on how you decide do it in this way?That would be nice if you do so.

Collapse
 
ayden002 profile image
Ayden002

Hi Huni, GOT ITTT! i will try to imorove that next time and thanks for your reading !****