4

There it goes a design issue I've been thinking about for, without finding convincing info about it.

Supposing I've got some instance variables into my classes, now imagine I want to write some private functionality for my class, using that value. It's not a problem to write something like that:

public class Example{

    private String attribute1;

    public void setAttribute1(String att){
        this.attribute1 = att;
    }

    private void processAttribute(){
        //do something with attribute1
    }

    public void executeExample(){
        processAttribute();
    }

}

Where processAttribute() uses the attribute1 value internally. However, many doc says we should try to limit the use of global variables. Would it be a more reusable and well-designed way to write something like this?

public class Example{

    private String attribute1;

    public void setAttribute1(String att){
        this.attribute1 = att;
    }

    private void processAttribute(String att){
        //do something with attribute1
    }

    public void executeExample(){
        processAttribute(this.attribute1);
    }

}

Pool your ideas.

Aritz
  • 30,971
  • 16
  • 136
  • 217
  • This is not a case of global variables and the first style is the correct style. To be global, you need to make the variable `public static` or something like that. – Karthik T Jun 26 '13 at 07:27

3 Answers3

2

Many of the arguments against global state apply here too:

  • it's harder to reason about the program correctness if the attribute is used in other places besides the processAttribute method
  • it's harder to parallelize code that uses global state: what should happen if the attribute is modified while it's being processed?
  • more: http://c2.com/cgi/wiki?GlobalVariablesAreBad

On the other hand, it's a private method and you are free to implement it however you like as long as you fulfill the contract for the class.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • That's the point I want to reach to. So, in your opinion, should I use parameters here? – Aritz Jun 26 '13 at 07:37
  • Personally I like to keep everything as local as possible, but sometimes it just makes more sense to use instance or static attributes. – Joni Jun 26 '13 at 07:43
1

First thing is that attribute1 is not a global attribute , it is only a class variable. Class variables will be available in all the class methods, using this operation and hence you need not to pass them as method parameters.

As there is no need of passing the method parameter here so implementing it does not seems logical. Btw this is my personal opinion, others may have different ideas.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 1
    It's obvious that he doesn't NEED to pass it. The question is what best design is. – Goatcat Jun 26 '13 at 07:27
  • It is an attribute of the the class Example. If it is declared something like public static, then you may consider it to be a global variable. – Juned Ahsan Jun 26 '13 at 07:28
  • Don't agree totally with you, at least by [Wikipedia def](http://en.wikipedia.org/wiki/Class_variable). However, as @Goatcat says, it's a design term, whatever name you want to give to it.. – Aritz Jun 26 '13 at 07:28
  • @XtremeBiker I agree to your point, but the general way of declaring the global variable is as what i specified in previous comment. Here is another SO link for your reference: http://stackoverflow.com/questions/4646577/global-variables-in-java – Juned Ahsan Jun 26 '13 at 07:31
  • wrong.. class variables are not those what you are thinking... static variables are called class variable – stinepike Jun 26 '13 at 07:32
  • @StinePike I understand that both static and non-static are class attributes. Static attributes are shared among objects,while non-static ones belong to each object. I have no confusion in teh understanding. Terms used may be confusing, apologies for that. – Juned Ahsan Jun 26 '13 at 07:34
  • please check the doc .. http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html – stinepike Jun 26 '13 at 07:35
1

However, many doc says we should try to limit the use of global variables.

I think you have misunderstood the concept. Usually global variables are those which are declared as public static so that it can be accessed directly from any other part of the application.

So in your both example the variable attribute1 is not a global variable. It is only a member variable of the class.

Hence I don't see much difference between the two different codes.

If the design is fixed then I think it is better to use the first one to make it more readable. And if in future there is other chances to send other variables as parameter rather than the member variable then you can use the second implementation. (Although I think it totally depends on coder's personal choice)

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • I agree with you it's a coder's personal choice and there not so much difference between the two ones. Now, imagine a much more complex class, where different threads can be running at the same time. – Aritz Jun 26 '13 at 07:36
  • I don't see any performance difference between that two implementation – stinepike Jun 26 '13 at 07:37