I had a talk with someone on the #ruby-lang channel the other day about @@class_variables
. It all started when a user asked what's the best way to keep track of connected users to his server (I slightly simplified it, but that's the gist of it).
So, I suggested:
class User
@@list = {} #assuming he wants to look up users by some type of ID
def initialize(user_id, ...)
@@list[user_id] = self
#...
end
end
However, somone said that the use of global state here is considered bad practice.
I understand why a global state is bad for something that relies on multiple back-ends, because the global part of global state stops being so global, and becomes localised to that one back-end. Or that it interferes with dependency injection.
I really can't think of any other reason why this is bad, though. And, if concurrency ever becomes an issue (there's a need for multiple back-ends), then we can update the code to use Redis (or something similar).
Also, I found this question on programmers.sxc, but it doesn't help me understand why the above code is considered so bad? Also, what would be the alternative?