• Post Reply Bookmark Topic Watch Topic
  • New Topic

instance variable vs local variable  RSS feed

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I check my code with a static grammar analyze tool. One of the problem is FieldCanBeLocal. When an instance variable in a class only accessed by one method inside, it could be defined as a local variable.

I think instance variable should represent an object's state. It should be kept in a minimum quantity. Local variables should be used as more as possible for encapsulation. I find some variables are defined as instance variables only because more than two methods inside use them. Is there a way to make them local variables? Or they should be treated as inside states compare with other instance variables which can be detected by other classes either by getXXX or setXXX?

Thanks.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object state and its visibility with set() and get() are separate
questions. Instance variables for internal use only are fine.
Jim ... ...
 
ravi thatikonda
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Louis Wang wrote:I check my code with a static grammar analyze tool. One of the problem is FieldCanBeLocal. When an instance variable in a class only accessed by one method inside, it could be defined as a local variable.

I think instance variable should represent an object's state. It should be kept in a minimum quantity. Local variables should be used as more as possible for encapsulation. I find some variables are defined as instance variables only because more than two methods inside use them. Is there a way to make them local variables? Or they should be treated as inside states compare with other instance variables which can be detected by other classes either by getXXX or setXXX?

Thanks.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you got any more to say than simply copying an old post?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without knowing more about the code in question it's difficult to formulate any meaningful opinion--if you reduce the code to a minimal example that still produces the warning it might be easier to understand why it might be saying that.
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers. Below is a sample code.

temp and tempB are not states of a Person. Indeed, temp should be defined as a local variable. But tempB is used in two methods, although i don't think it's a Person's state, I can't find a way to make it locale.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then pass it between methods.

Static code analysis is a *best effort*--just because it says something doesn't mean it's necessarily true.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beyond the state of a Person, however, instance and class variables can show object state.

Jim... ...
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!