个人项目优化小细节总结

作为开发人员,总是会遇到接手别人的二手项目,一般接手之后基本都会修改或者优化。这边也是,接手了一个二手项目,一些优化小细节总结下,也为自己以后优化多张张经验。积少成多嘛。

1.
优化前:

 boolean push = SPUtil.getBoolean(getApplicationContext(), ConstantsUtil.PUSH, true);
        if (push) {
            cbPush.setChecked(true);
        } else {
            cbPush.setChecked(false);
        }

也不知道想表达什么,反正当时还看了半天想着这业务逻辑,最后也是突然大悟

优化后:

boolean push = SPUtil.getBoolean(getApplicationContext(), ConstantsUtil.PUSH, true);

cbPush.setChecked(push);

2.优化前:

   new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(openid) ) {

                    startActivity(new Intent(SplashActivity.this, ChooseLoginActivity.class));
                } else {

                    if (2 == loginType) {
                        startActivity(new Intent(SplashActivity.this, DseignerMainActivity.class));

                    } else if (4 == loginType) {
                        startActivity(new Intent(SplashActivity.this, ClerkMainActivity.class));

                    }  else {
                        startActivity(new Intent(SplashActivity.this, ChooseLoginActivity.class));
                    }


                }


                finish();
            }
        }, 500);

看到这么多if else 肯定很头疼把,来看看我优化后的

优化后:

       new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(openid) ) {
                    startChooseLoginActivity();
                } else {
                    isLoginType(loginType);
                }
                finish();
            }
        }, 500);
    }

    private void isLoginType(int loginType){
        switch (loginType){
            case 2:
                startActivity(new Intent(SplashActivity.this, DseignerMainActivity.class));
                break;
            case 4:
                startActivity(new Intent(SplashActivity.this, ClerkMainActivity.class));
                break;
            default:
                startChooseLoginActivity();
                break;
        }
    }

    private void startChooseLoginActivity(){
        startActivity(new Intent(SplashActivity.this, ChooseLoginActivity.class));
    }

虽然代码量相差不多,但是逻辑比较清晰,也容易阅读

你可能感兴趣的:(Android)