UnholySalmon
UnholySalmon

Reputation: 77

Android Development: Is using objects bad practice?

I'm currently developing my first app for a project and was wondering should I use objects and have less code or have more code but no objects. Here is the code which will go in to 4 separate methods (i per activity) or one class to reference in each activity.

        TextView text1 = (TextView)this.cal.findViewById(R.id.txt1);
        TextView text2 = (TextView)this.cal.findViewById(R.id.txt2);
        TextView text3 = (TextView)this.cal.findViewById(R.id.txt3);
        TextView text4 = (TextView)this.cal.findViewById(R.id.txt4);
        TextView text5 = (TextView)this.cal.findViewById(R.id.txt5);
        TextView text6 = (TextView)this.cal.findViewById(R.id.txt6);
        TextView text7 = (TextView)this.cal.findViewById(R.id.txt7);
        TextView text8 = (TextView)this.cal.findViewById(R.id.txt8);
        TextView text9 = (TextView)this.cal.findViewById(R.id.txt9);
        TextView text10 = (TextView)this.cal.findViewById(R.id.txt10);
        TextView text11 = (TextView)this.cal.findViewById(R.id.txt11);
        if(x == 0)
        {
            text1.setText (text1.getText() + "sciemce and enginnering");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");

        }
        else if(x ==1)
        {
            text1.setText (text1.getText() + "arts");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");
        }

        else if(x == 2)
        {
            text1.setText (text1.getText() + "1");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");

        }

        else if(x ==3)
        {
            text1.setText (text1.getText() + "1");
            text2.setText (text2.getText() + "add a bit");
            text3.setText (text3.getText() + "add a bit");
            text4.setText (text4.getText() + "add a bit");
            text5.setText (text5.getText() + "add a bit");
            text6.setText (text6.getText() + "add a bit");
            text7.setText (text7.getText() + "add a bit");
            text8.setText (text8.getText() + "add a bit");
            text9.setText (text9.getText() + "add a bit");
            text10.setText (text10.getText() + "add a bit");
            text11.setText(text11.getText() + "add a bit");
        }





}

This code above which is very long (goes on for 4 if statements) which will appear in 4 different activities as I am loading grading schemes which change depending on a faculty. I have it in a separate class at the moment and I am creating an object of the class to call the method to load the table which is the code above, or should I put this code in my activities separatly because I heard using ojects was bad practice. Thanks in adavance and sorry if it is very ambigous but its too much code to post =).

Upvotes: 0

Views: 106

Answers (3)

jerrytao
jerrytao

Reputation: 156

  1. A table? If there is not too much work base on id, try use List instead of a lot of objects,like this.

    list.add(this.cal.findViewById(...))
    
  2. And change the if...else.. to switch;

  3. Use loop is totally fine.

    for(item in list){
       if(x==1 && index==0) {item.setText("...");}
       else{item.setText("another...");}
    }
    

Upvotes: 0

Rich
Rich

Reputation: 36856

There are a number of things you can do here. The first thing that jumps out at me is: what does the value of x mean? When you check if x == 0, what does that condition represent? I'd rename "x" to something more descriptive and maybe make named constants where you have the hard coded integers. So, your code would be something like

if (selectedDegree == SCIENCE_AND_ENGINEERING)

instead of

if (x == 0)

and anyone reading your code would understand it more. I'd also create a helper method to append the text each time.

text1.setText (text1.getText() + "sciemce and enginnering");
text2.setText (text2.getText() + "add a bit");
text3.setText (text3.getText() + "add a bit");
text4.setText (text4.getText() + "add a bit");

would become

appendText(text1, "science and engineering");
appendText(text2, "and a bit");
appendText(text3, "and a bit");
appendText(text4, "and a bit");

private void appendText(TextView textView, String addedText) {
    if (textView != null) {
        textView.setText(textView.getText() + addedText);
    }
}

Upvotes: 0

asdf
asdf

Reputation: 667

You can simplify it with a loop, for example:

else if(x ==1)
    {
        text1.setText (text1.getText() + "arts");
        text2.setText (text2.getText() + "add a bit");
        text3.setText (text3.getText() + "add a bit");
        text4.setText (text4.getText() + "add a bit");
        text5.setText (text5.getText() + "add a bit");
        text6.setText (text6.getText() + "add a bit");
        text7.setText (text7.getText() + "add a bit");
        text8.setText (text8.getText() + "add a bit");
        text9.setText (text9.getText() + "add a bit");
        text10.setText (text10.getText() + "add a bit");
        text11.setText(text11.getText() + "add a bit");
    }

to

for (int i = 1; i < 12; i++){
    text[i].setText(....)
}

Upvotes: 3

Related Questions