0

独学のコーダーとして、コーディングのベスト プラクティスについて質問があります。私は同じプログラムを 2 つの異なる方法で作成しましたが、どちらの方法が望ましいかについて誰かに教えてもらいたいですか? これは、CPU のオーバーヘッドや RAM の使用量、または単にコーディングのベスト プラクティスの観点からのものである可能性があります。この質問には多くの答えや理論があるかもしれませんが、それがスタック オーバーフローの目的ではないことはわかっていますが、私のコーディング スタイルが問題を引き起こしているため、これはスタック オーバーフローの質問です。私も固執するか、採用する必要がある方法を知っています。

どちらの例にも、2 つのボタンと 2 つの更新可能なテキストビュー (ここには含まれていません) を持つ単純な XML レイアウト ファイルがあります。

例 1. (私が現在コーディングしている方法)

public class MainActivity extends Activity {

    Button button1Add, button2Add;
    TextView text1Display, text2Display;
    int count1, count2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initilizeButtons();
        setupListeners();
    }

    public void initilizeButtons() {
        button1Add = (Button) findViewById(R.id.button1);
        button2Add = (Button) findViewById(R.id.button2);
        text1Display = (TextView) findViewById(R.id.textView1);
        text2Display = (TextView) findViewById(R.id.textView2); 
        count1 = Integer.parseInt(text1Display.getText().toString());
        count2 = Integer.parseInt(text2Display.getText().toString());
    }

    public void setupListeners() {
        button1Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                count1++;
                text1Display.setText(String.valueOf(count1));
            }
        });

        button2Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                count2++;
                text2Display.setText(String.valueOf(count2));
            }
        });
    }

}

例 2. (私がコーディングすべきだと思う方法は?)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initilizeButtons();

    }

    public void initilizeButtons() {
        Button button1Add = (Button) findViewById(R.id.button1);
        Button button2Add = (Button) findViewById(R.id.button2);

        button1Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                TextView text1Display = (TextView) findViewById(R.id.textView1);
                int headCount = Integer.parseInt(text1Display.getText().toString());
                headCount++;
                text1Display.setText(String.valueOf(headCount));
            }

        });

        button2Add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                TextView text2Display = (TextView) findViewById(R.id.textView2);
                int bodyCount = Integer.parseInt(text2Display.getText().toString());
                bodyCount++;
                text2Display.setText(String.valueOf(bodyCount));
            }

        });
    }

}

例 1 では、変数を宣言し、プログラム全体でそれらに追加するだけなので、これはより高速になる (つまり、CPU が少なくなる) と想定しています。ここでRAMの使用量が心配です。

例 2 では、変数を使用するたびにすべての変数を再宣言する必要があるため、CPU 使用率が高くなると思いますが、使用する RAM が少ないのでしょうか?

これらは単なる例であり、それら自体が CPU や RAM のオーバーヘッドに大きな影響を与えることはないと確信しています。あなたが提供する回答からの情報を、私の一般的なコーディングの実践に適用します。

4

4 に答える 4

1

i think example 1 should be the preferred way.

The Ram usage does not increase much, as the Widgets are existing if you use findViewById or not. You only get a reference to the objects shown.

Personally i use AndroidAnnotations this allows you to drop the initilizeButtons() as you can add some Annotations to the fields and the library injects them for you. Look at thier code comparison on thier website... its awesome. And it also supports many other nice features. And the best part is, that it uses Code generation and not use runtime reflection(what costs cpu and performance) like other similar librarys...

于 2013-03-13T13:16:01.570 に答える
0

役に立たないかもしれませんが、私は実際には両方のオプションを個人的に嫌っています。複雑なレイアウトのアクティビティでは、onclickメソッドのコードを何度も繰り返す必要があります。

このことを考慮; (タイプミスがある場合は申し訳ありませんが、メモ帳で行いました)。スペルが間違っているメソッド「initilizeButtons」に名前を付けるなど、コードの残りの部分には焦点を当てませんでした。また、ボタンだけでなく、テキストビューや整数も初期化するため、誤解を招く恐れがあります。

public class MainActivity extends Activity implements OnClickListener {

    Button button1Add, button2Add;
    TextView text1Display, text2Display;
    int count1, count2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initilizeButtons();
        setupListeners();
    }

    public void initilizeButtons() {
        button1Add = (Button) findViewById(R.id.button1);
        button2Add = (Button) findViewById(R.id.button2);
        text1Display = (TextView) findViewById(R.id.textView1);
        text2Display = (TextView) findViewById(R.id.textView2); 
        count1 = Integer.parseInt(text1Display.getText().toString());
        count2 = Integer.parseInt(text2Display.getText().toString());
    }

    public void setupListeners() {
        button1Add.setOnClickListener(this);
        button2Add.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
       switch(view.getId()){     
         case R.id.button1:
            // Do your button1 things
         break;
         case R.id.button2:
            // do your button2 things
         break; 
        }
    }

}
于 2013-03-13T13:21:49.233 に答える
0

それはあなたの必要性に依存します、

たとえば、アクティビティがそのビューで何度も使用する場合は、オプション2の方が適しています

于 2013-03-13T13:12:09.940 に答える
0

メモリとパフォーマンスに関しては、明らかな違いは見られません。一般に、宣言された変数のスコープをできるだけ小さく保つことをお勧めします。したがって、残りのアクティビティでボタンが不要になった場合は、クラスでフィールドとして宣言しないでください。

私の個人的な経験では、多くの匿名内部クラスを使用するとコードが読みにくくなるため、インターフェイスを実装するアクティビティを使用するか、囲んでいるアクティビティで小さな名前付き内部クラスを作成します。

于 2013-03-13T13:37:27.427 に答える