At the Rails Edge this morning, PragDave spoke out against class variables, and in favor of class instance variables. I totally agree. Look at this code:
Can you guess what this program will do? It demonstrates three of the ways that class variables cause trouble:
- The behave differently from instance variables if they are missing.
- They are shared across an inheritance hierarchy in a counterintuitive way.
- They require a new syntax, when the instance variable syntax is perfect for the task.
But maybe I am wrong. Ola Bini, who says many wise things, appears to be arguing exactly the opposite. Does anybody have a use case where class variables are the best solution?