I have been following a tutorial on how to write a basic tile map editor in C# + XNA and I have a question about the coding practice used. The main Game class contains many public static variables which are then accessed and modified from other instantiated classes without the use of getters or setters. Is this bad coding practice?
Link to the tutorial: http://www.youtube.com/watch?v=Tbs5EA-9Zfg
I have been following a tutorial on how to write a basic tile map
Share
I’m not going to watch a bunch of videos just to see the code in question, but if these are mutable types then I’d definitely say it’s poor practice in general coding. Static variables end up making unit testing much harder, and your code is harder to reason about because of all the global state. UI code often violates best principles in my experience – I’m sure it doesn’t have to though, and MVVM helps on this front for WPF/Silverlight. I’d try to write the code using best practices you’re used to from other environments, but bear in mind any performance impact which could be relevant in some XNA applications.
If the public static variables are effectively constants (readonly variables for immutable types) then I don’t have much of a problem with it – although if the type initializer ends up getting complicated, you can get type initializer cycles which can be a nightmare to debug. I’ve just finished writing a blog post about that very topic. You’d potentially get the same problem even if the variables were private and accessed via properties, although in that case you can change the timing more easily.