© Adam L. Davis 2020
A. L. DavisModern Programming Made Easyhttps://doi.org/10.1007/978-1-4842-5569-8_11

11. Refactoring

Adam L. Davis1 
(1)
Oviedo, FL, USA
 

Refactoring 1 means changing code in a way that has no effect on functionality. It is only meant to make the code easier to understand or to prepare for some future addition of functionality. For example, sometimes you refactor code to make it easier to test. Many IDEs provide menus and key shortcuts for performing common refactorings.

There are two categories of refactoring we will cover, object-oriented and functional, corresponding to the two different programming styles.

Object-Oriented Refactoring

The following actions are common refactorings in OOP:
  • Changing a method or class name (renaming)

  • Moving a method from one class to another (delegation)

  • Moving a field from one class to another

  • Adding or removing parameters from a method

  • Creating a new class using a set of methods and fields from a class

  • Changing a local variable to a class field

  • Replacing a bunch of literals (strings or numbers) with a constant (static final)

  • Replacing some constants or literals with an enum

  • Moving a class from an anonymous class to a top-level class

  • Renaming a field

Functional Refactoring

The following actions are common refactorings in FP:
  • Renaming a function

  • Wrapping a function in another function and calling it

  • Inline a function wherever it is called

  • Extract common code into a function (the opposite of the previous)

  • Renaming a function parameter

  • Adding or removing a parameter

You might notice some similarities between both lists. The principles of refactoring are universal.

Refactoring Examples

NetBeans, like many IDEs, supports refactoring. You can try it out by selecting some code, right-clicking, and selecting the Refactor menu. Here are some examples of refactoring code.

Renaming a Method

Before:
1   public static void main(String...args) {
2       animateDead();
3   }
4   public  static void  animateDead() {}
After:
1   public static void main(String...args) {
2       doCoolThing();
3   }
4   public  static void  doCoolThing() {}

Moving a Method from One Class to Another (Delegation)

Before :
1   public static void main(String...args) {
2       animateDead();
3   }
4   public  static void  animateDead() {}
After:
1   public class Animator() {
2       public void animateDead() {}
3   }
4   public static void main(String...args) {
5       new Animator().animateDead();
6   }

Replacing a Bunch of Literals (Strings or Numbers) with a Constant (Static Final)

Before :
1   public static void main(String...args) {
2       animateDead(123);
3       System.out.println(123);
4   }
5   public static void animateDead(int n) {}
After:
1   public static final int NUM = 123;
2   public static void main(String...args) {
3       animateDead(NUM);
4       System.out.println(NUM);
5   }
6   public static void animateDead(int n) {}

Renaming a Function

Before:
1   function castaNastySpell() { /* cast a spell here */ }
After:
1   function castSpell() { /* cast a  spell here  */ }

Wrapping a Function in Another Function and Calling It

Before:
1   castSpell('my cool spell');
After:
1   (function(spell) { castSpell(spell) })('my cool spell');

Inline a Function Wherever It Is Called

This might be done during refactoring when a function is too simple or is only used in one place.

Before:
1   function castSpell(spell) { alert('You cast ' + spell); }
2   castSpell('crucio');
3   castSpell('expelliarmus');
After:
1   alert('You cast ' + 'crucio');
2   alert('You cast ' + 'expelliarmus');

Extract Common Code into a Function (the Opposite of the Previous)

Before :
1   alert('You cast crucio');
2   alert('You cast expelliarmus');
After:
1   function castSpell(spell) { alert('You cast ' + spell); }
2   castSpell('crucio');
3   castSpell('expelliarmus');
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset