2

I have heard several times that creating unnecessary instance variables is bad practice, but can't figure out why it is a bad practice?

What effects does it have if we use unnecessary instance variables in our application?

Any help would be appreciated, thanks in advance.

wurde
  • 2,487
  • 2
  • 20
  • 39
  • Do it have any adverse effects on our application loading time etc?? – kartik upadhyay May 23 '15 at 07:59
  • 2
    In theory the loading time might be a bit slower, because all instance variables will be copied from the controller to the view - even if you do not use them in the view. That is a waste of memory and and just takes some extra CPU cycles. – spickermann May 23 '15 at 08:06
  • Means it will slow down our response time. makes sense, Thanks @spickermann – kartik upadhyay May 25 '15 at 06:04

3 Answers3

3

Because they are unnecessary but they will still float around in your views.

The catch is, you know that they are unnecessary so you won't care about them (even if they contain wrong data) but next developers won't know that they are unnecessary. He would think that there is some important reason that is why kartik defined them. He might go ahead and use them and may end up using wrong data.

Poor guy will have to struggle a lot to understand the reason of their existence. And when he will know then he may curse you :)

Vijay Meena
  • 683
  • 1
  • 7
  • 12
0

Using instance variable everywhere is not a wise thing to do. You can think of global variables (on a smaller scale) as an analogy. There are often methods that have data very specific to them. Sharing them across with all the methods of the object does not make sense, when you can directly pass them to specific methods as parameters. Also this may potentially lead to un-debuggable spaghetti code.

shivam
  • 16,048
  • 3
  • 56
  • 71
0

If I were you I would like to think that instance variables hold the state of my objects. Therefore I would only have to use just enough instance variables for the states I need to know or use to interact with other objects.

Bala
  • 11,068
  • 19
  • 67
  • 120