场景1:调用方法还是定义局部变量
风格1
Object param = getSomeParameter();
useParamFirstTime(param);
...
useParamSecondTime(param);
风格2
useParamFirstTime(getSomeParameter());
...
useParamSecondTime(getSomeParameter());
场景2:传一个对象还是一个对象+一个属性
风格1
methodOne(Object obj) {
int type = obj.getType();
if (type == 1) {
methodTwo(obj);
}
}
methodTwo(Object obj) {
int type = obj.getType();
if (type == 1) {
...
}
}
风格2
methodOne(Object obj) {
int type = obj.getType();
if (type == 1) {
methodTwo(obj, type);
}
}
methodTwo(Object obj, int type) {
if (type == 1) {
...
}
}
场景3:用实例字段还是局部变量
风格1
Class A {
B b;
methodA() {
B localB = this.b;
localB.methodB();
}
}
风格2
Class A {
B b;
methodA() {
this.b.methodB();
}
}