code review(自审)

代码的自我评审能够帮助我们找出代码的错误,优化维护代码,这里我选取了以前安卓做的一个小demo进行评审。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mrwu.myapplication.MainActivity"
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录界面"
        android:textSize="30dp"
        android:background="#DBDBDB"
        android:layout_gravity="center"></TextView>

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>

    <EditText
        android:id="@+id/et2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:hint="请输入密码"/>
    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:layout_gravity="center"
        />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="头像"
        android:layout_gravity="center"/>
    <ImageView
        android:id="@+id/Iv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"/>
        <EditText
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的性别是:"/>
    <RadioButton
        android:layout_marginLeft="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"/>
    <RadioButton
        android:layout_marginLeft="200dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"/>

    </RelativeLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我喜欢的专业:"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="JAVA"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="英语"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="高数"/>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ip:"
        android:textSize="25dp"/>
      <EditText
        android:id="@+id/et3"
        android:layout_marginLeft="25dp"
        android:layout_width="350dp"
        android:layout_height="wrap_content" />
</RelativeLayout>
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
package com.example.mrwu.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
       EditText et1;
       EditText et2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         et1=(EditText) findViewById(R.id.et1);

         et2=(EditText) findViewById(R.id.et2);

        Button bt1 =(Button)findViewById(R.id.bt1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1=et1.getText().toString();
                String  str2=et2.getText().toString();
                if(TextUtils.isEmpty(str1)&&TextUtils.isEmpty(str2)){
                        Toast.makeText(MainActivity.this,"请输入用户名和密码",Toast.LENGTH_SHORT).show();

                }else if(TextUtils.isEmpty(str1)&&str2!=null){
                    Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();


                }else if(TextUtils.isEmpty(str2)&&str1!=null){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();

                }else{
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    Intent myIntent = new Intent(MainActivity.this,SecondActivty.class);
                    startActivity(myIntent);
                }


                }

        });
    }
}
public class SecondActivty extends AppCompatActivity {
    ImageView Iv1;
    EditText et3;
    TextView tv2;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        Iv1 = (ImageView) findViewById(R.id.Iv1);
        et3 = (EditText)findViewById(R.id.et3);
        tv2 = (TextView)findViewById(R.id.tv2);
        Iv1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP){
                    float x= event.getX();
                    float y=event.getY();
                    String info = "X=" + x + "Y=" + y;
                    Toast.makeText(SecondActivty.this, info, Toast.LENGTH_LONG).show();
                }
                return true;
            }

    });
        et3.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (event.getAction()){
                    case KeyEvent.ACTION_UP:
                        String ip = et3.getText().toString();
                        tv2.setText(Subs(ip));
                    case KeyEvent.ACTION_DOWN:
                        break;
                }

                return false;
            }
            String  Subs(String total){
                String news = "";
                for (int i =0;i<=total.length()/3;i++ ){
                    if(i*3+3<total.length()){
                 news = news+total.substring(i*3,Math.min(i*3+3,total.length()))+"-";
                    }else{
                 news = news+total.substring(i*3,Math.min(i*3+3,total.length()));
                    }
                }
               return news;
            }



        });
        }


    }

1.代码没有块缩进:4个空格

2.控件ID的命名是随便命名的,没有意义

3.必要的地方没有注释

4.类名没有以UpperCamelCase风格编写

5.参数名是随意定义的,没有以LowerCamelCase风格编写

6.UI控件成员变量没有统一加上控件缩写作为后缀,这样容易与普通成员变量混淆

posted @ 2017-03-24 20:27  三仰化二铁  阅读(233)  评论(0编辑  收藏  举报