java常用重构技巧
点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
作者 | binn_chern
来源 | urlify.cn/Iv22Qf
基本原则
public class A{
private static B b = new B();
}
class MAF {
public void setSize (int size) {
_size = size;
}
private int _size;
}
更正
class DAF_fixed {
final public void setSize (int size) {
_size = size;
}
private int _size;
}
String str = "hello";
String str = new String("hello");
public HashMap(int initialCapacity, float loadFactor);
for(int i=0;i<list.size();i++)
for(int i=0,len=list.size();i<len;i++)
A a = new A();
if(i==1){list.add(a);}
应该改为
if(i==1){
A a = new A();
list.add(a);
}
Public void test(){
Object obj = new Object();
……
Obj=null;
}
Public void test(){
Object obj = new Object();
……
Obj=null;
//执行耗时,耗内存操作;或调用耗时,耗内存的方法
……
}
Public void test(){
Object obj = new Object();
……
Obj=null;
}
上面这个就没必要了,随着方法test()的执行完成,程序中obj引用变量的作用域就结束了。但是如果是改成下面:
Public void test(){
Object obj = new Object();
……
Obj=null;
//执行耗时,耗内存操作;或调用耗时,耗内存的方法
……
}
int index = indexOf(obj);vector.remove(index);
public static Credit getNewCredit()
{
return new Credit();
}
改进后的代码使用clone()方法:
private static Credit BaseCredit = new Credit();
public static Credit getNewCredit()
{
return (Credit)BaseCredit.clone();
}
Map<String, String[]> paraMap = new HashMap<String, String[]>();
for( Entry<String, String[]> entry : paraMap.entrySet() )
{
String appFieldDefId = entry.getKey();
String[] values = entry.getValue();
}
import java.util.Vector;
class CEL {
void method (Vector vector) {
for (int i = 0; i < vector.size (); i++) // Violation
; // ...
}
}
更正:
class CEL_fixed {
void method (Vector vector) {
int size = vector.size ()
for (int i = 0; i < size; i++)
; // ...
}
}
import java.util.Vector;
public class DIC {
public void addObjects (Object[] o) {
// if length > 10, Vector needs to expand
for (int i = 0; i< o.length;i++) {
v.add(o); // capacity before it can add more elements.
}
}
public Vector v = new Vector(); // no initialCapacity.
}
public Vector v = new Vector(20);
public Hashtable hash = new Hashtable(10);
public class USC {
String method () {
StringBuffer s = new StringBuffer ("Hello");
String t = s + "World!";
return t;
}
}
public class STR {
public void method(String s) {
String string = s + "d" // violation.
string = "abc" + "d" // violation.
}
}
public class STR {
public void method(String s) {
String string = s + 'd'
string = "abc" + 'd'
}
}
类和方法优化
class BadExample {
public void someMethod1(){
//code
System.out.println("重复代码");/* 重复代码块 */
//code
}
public void someMethod2(){
//code
System.out.println("重复代码");/* 重复代码块 */
//code
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
public void someMethod1(){
//code
someMethod3();
//code
}
public void someMethod2(){
//code
someMethod3();
//code
}
public void someMethod3(){
System.out.println("重复代码");/* 重复代码块 */
}
}
class BadExample {
public void someMethod(){
//function[1]
//function[2]
//function[3]
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
public void someMethod(){
function1();
function2();
function3();
}
private void function1(){
//function[1]
}
private void function2(){
//function[2]
}
private void function3(){
//function[3]
}
}
class BadExample {
public void someMethod(Object A,Object B){
if (A != null) {
if (B != null) {
//code[1]
}else {
//code[3]
}
}else {
//code[2]
}
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
public void someMethod(Object A,Object B){
if (A == null) {
//code[2]
return;
}
if (B == null) {
//code[3]
return;
}
//code[1]
}
}
class BadExample {
public void someMethod(Object A,Object B){
if (A != null) {
if (B != null) {
//code
}
}
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
public void someMethod(Object A,Object B){
if (A != null && B != null) {
//code
}
}
}
class BadExample {
private int i;
public int someMethod(){
int temp = getVariable();
return temp * 100;
}
public int getVariable(){
return i;
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
private int i;
public int someMethod(){
return getVariable() * 100;
}
public int getVariable(){
return i;
}
}
class BadExample {
public void someMethod(int i,int j,int k,int l,int m,int n){
//code
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
public void someMethod(Data data){
//code
}
}
class Data{
private int i;
private int j;
private int k;
private int l;
private int m;
private int n;
//getter&&setter
}
class BadExample {
public void someMethod1(){
send("您的操作已成功!");
}
public void someMethod2(){
send("您的操作已成功!");
}
public void someMethod3(){
send("您的操作已成功!");
}
private void send(String message){
//code
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
protected static final String SUCCESS_MESSAGE = "您的操作已成功!";
public void someMethod1(){
send(SUCCESS_MESSAGE);
}
public void someMethod2(){
send(SUCCESS_MESSAGE);
}
public void someMethod3(){
send(SUCCESS_MESSAGE);
}
private void send(String message){
//code
}
}
class BadExample {
public int someMethod(Data data){
int i = data.getI();
int j = data.getJ();
int k = data.getK();
return i * j * k;
}
public static class Data{
private int i;
private int j;
private int k;
public Data(int i, int j, int k) {
super();
this.i = i;
this.j = j;
this.k = k;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public int getK() {
return k;
}
}
}
/* ---------------------分割线---------------------- */
class GoodExample {
public int someMethod(Data data){
return data.getResult();
}
public static class Data{
private int i;
private int j;
private int k;
public Data(int i, int j, int k) {
super();
this.i = i;
this.j = j;
this.k = k;
}
public int getI() {
return i;
}
public int getJ() {
return j;
}
public int getK() {
return k;
}
public int getResult(){
return i * j * k;
}
}
}
结束语
锋哥最新SpringCloud分布式电商秒杀课程发布
👇👇👇
👆长按上方微信二维码 2 秒
感谢点赞支持下哈
评论