Gerard McCann
Gerard McCann

Reputation: 41

Use of instance variables vs passing data as a parameter

What are the disadvantages when an instance variable is used instead of passing the data as a parameter.

Using instance variables seems more readable to me, but are there disadvantages of making them available to the whole class. I guess this is coming from global vs local variable perspective.

Using instance variables

public class ChoicesFragment extends Fragment implements View.OnClickListener {
    private CharSequence[] mButtonTextData;
    private String mTitleTextData;  
    
    private View mUserChoiceView;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mUserChoiceView = inflater.inflate(R.layout.available_choices, container, false);

        getBundleData();

        setTitle();
        setButtons();        

        return  mUserChoiceView;
    }
    
    private void getBundleData() {
        //populate button text
        Bundle bundleData = getArguments();
        mTitleTextData = bundleData.getString(CardFragment.TITLE_TEXT_KEY);
        mButtonTextData = bundleData.getCharSequenceArray(CardFragment.BUTTON_TEXT_KEY);        
    }
    
    private void setTitle() {
        TextView title = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView1);
        String genericTitle = getResources().getString(R.string.title_selection);
        title.setText(genericTitle + mTitleTextData);
    }

    //sets up button data
    public void setButtons() {
        TextView[] buttonsTextView = new TextView[4];
        buttonsTextView[0] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView2);
        buttonsTextView[1] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView3);
        buttonsTextView[2] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView4);
        buttonsTextView[3] = (TextView) mUserChoiceView.findViewById(R.id.choicesTextView5);

        for(int i = 0; i < buttons.length; i++) {
            buttonsTextView[i].setText(mButtonTextData[i]);
        }
    }   
}

Second example pass the data as parameters

public class ChoicesFragment extends Fragment implements View.OnClickListener { 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {                            
        View userChoiceView;
                             
        String titleTextData;
        CharSequence[] buttonTextData;
        
        mUserChoiceView = inflater.inflate(R.layout.available_choices, container, false);

        Bundle bundleData = getArguments();
        buttonTextData = bundleData.getString(CardFragment.TITLE_TEXT_KEY);
        titleTextData = bundleData.getCharSequenceArray(CardFragment.BUTTON_TEXT_KEY);

        setTitle(userChoiceView, titleTextData);
        setButtons(userChoiceView, buttonTextData);        

        return  mUserChoiceView;
    }
    
    private void setTitle(View userChoiceView, String titleText) {
        TextView title = (TextView) userChoiceView.findViewById(R.id.choicesTextView1);
        String genericTitle = getResources().getString(R.string.title_selection);
        title.setText(genericTitle + titleText);
    }

    //sets up button data
    public void setButtons(View userChoiceView, CharSequence[] buttonText) {
        TextView[] buttonsTextView = new TextView[4];
        buttonsTextView[0] = (TextView) userChoiceView.findViewById(R.id.choicesTextView2);
        buttonsTextView[1] = (TextView) userChoiceView.findViewById(R.id.choicesTextView3);
        buttonsTextView[2] = (TextView) userChoiceView.findViewById(R.id.choicesTextView4);
        buttonsTextView[3] = (TextView) userChoiceView.findViewById(R.id.choicesTextView5);

        for(int i = 0; i < buttons.length; i++) {
            buttonsTextView[i].setText(buttonText[i]);
        }
    }   
}

Upvotes: 1

Views: 600

Answers (1)

Robert Harvey
Robert Harvey

Reputation: 180908

Using parameters allows you to make the method self-contained. You never have to wonder what value the instance variable has, because you passed the data as a parameter.

Apart from the very useful benefit of encapsulation, parameterized methods are far easier to unit test than methods that depend on the value of instance variables.

Upvotes: 3

Related Questions