4

Is storing-of-data-in-classes-as-static-variables-to-access-them-later-on-from-one-page-to-another a good practice? It always seemed fishy to me...

Comments
  • 3
    If it is shared data that all users have the same and if it can be recreated if necessary its just another form of caching.

    I use it in c# for some things that do not serialize well and that need to be machine local.

    If it contains any user data your walking a very slack barbwire line that will draw blood.
  • 0
    @Voxera haha. knew it. forgot to mention it is user data. Thanks!
  • 1
    In general, using a static field is not bad "per se". It depends on what you do with it. If the purpose is to create shortcuts and pass data between different classes (pages in your case) then I would look to different approaches (messages, DI ..)
  • 2
    @jetttlag depending on platform, statics are shared by all visitors which can make for an interesting mix of information.

    I have seen it happen where page settings was stored in statics and several users tried to change them.

    The result was very random.

    Luckily it was no secret information that leaked.
  • 1
    senior android dev here! (but still, I may be wrong, take this with a grain of salt)

    I think you are right, generally it's not a good practice for a couple of reasons:

    - doing so tight-couples the class that has the static variable with any classes that use the variable
    - doing so makes it's hard to reason about the outcome of the code that use the variable since the variable could be any value

    I suggest going with event bus kind of thing if you want to synchronize state between all component like a bunch of fragments with a bunch of activities
Add Comment