Why are static variables bad?

Started by
56 comments, last by Sacaldur 10 years, 7 months ago

So how would someone resolve such Sleep trouble ?

By making it part of the system that it is designed for.

Generally you want to make data as object local as you can, for instance if I had an enum I created that defined ordinal directions in a 2d game like north, east, south, etc. Quite a few systems might use that, in that case I might place it into a header file under a namespace that can be included by any parties interested in using that enum. For something like a sleep variable that isn't a constant you would probably want to make it belong to the thing that uses it the most, i.e. your window or engine class or whatever. Then just allow it to be accessed from other parts of code that need to determine what it is or modify it.

Using another example you may create an enum of substates for a state machine, well if only that state machine uses it and any other objects would -have- to include the state machine to ever be interested in using the enum then you could place it in the header file. It's a balance of trying to keep information local to where it is used while not mashing everything together in globals or giant include files. It's actually a bad idea to have a header file like "constants.hpp" or something as well due to the fact that everytime that file changes it will force recompilation in the other files.
Advertisement

For something like a sleep variable that isn't a constant you would probably want to make it belong to the thing that uses it the most, i.e. your window or engine class or whatever.

This one seem to be reasonable advice i think. Indeed probably removing it from the globals module (I put it there because I was tending to forgot where it can be so it is easy to find) and defining it into window module - and reference from all the others. This is some improvement.- But I am still not sure if this removes some troubles I eventually see still related

(such problems are related maybe to fact that 1-1 'linear' dependencies in code are clear, but when you have such 'accesible spots' you can have uncontrolled many-many

dependencies and it brings some trouble

[but I am not sure if this is just about that, this is I think related not only to system of referencing but to some code flow architecture]

- I "will be must" (Wiill had to ? future neccesity do not know how to write this in english ) rethink it still more, to find, if there is no way to repairing it more, by some other elaborate design or something)

(blank, sorry mistaken click)

I'd like to thanks for the replies, I kept thinking of how could I change my code design and I think I got something a little better than before.

I didn't really like having static variables in my engine since the game class (exposed to other programmers) could access it as well and potentially break something.

I added a "resourceLoader" so all objects that wants to use the engine resource module must go through, and then it can manages itself and I don't need to have it global static for the code:

void Game::SomeFunction()
{
    Image* myImage = new Image();
    myImage->Load("assetName"); //I accessed the static resourceModule here
}

Now it is:

void Game::SomeFunction()
{
   //Since I'm already inside game, and game has access to engine
   Image* myImage = engineResources.Load<Image>("assetName");
   //If "assetName" hasn't been loaded yet, it'll call "Image->Load()" so I don't have all load functions inside my resource module
   //I want to make objects manage themselves so it's easier to handle
}

I still left the original constructor open so Game can build their own objects if needed (such as simple Quad, or loading something from outside the resource Module, though they'd need to manage it themselves).

So that's something less that I need to worry about outside access that could mess things up, thanks for the help!

Static variables can be used. But they must not be used with a lack of knowledge of what static is capable of doing to the results you expect not to have but in fact have gottern.

Suppose you made private static int life for all instances of the Monster class, what would happen to the monster when your ship shot a laser at it? Assuming the laser can kill it with one hit and also assuming there are more than one instance of the Monster class in the game winodw, both monsters will in fact be affected with the hit even though you only aim at that one monster.

Static variables has its benefits. Suppose when you want to access a method of a class without having creating the object of the class This acts as a convenience.

If you find yourself misusing static, this is a clear indication to re-evaluate your code design in terms of object-oriented design.

A good example of a Student class using non-static and static variables would be

public class Student

{

private static int unitsToGraduate = 120;

private int name;

private int age;

}

Static variables has its benefits. Suppose when you want to access a method of a class without having creating the object of the class This acts as a convenience.

You want to do so when you have Code unrelated to any class or you're not able to use a non-static way (e. g. because your function does some mathematics and you're not able to extend the basic datatypes). If you're working object oriented you want to use methods and in most cases it's possible to do so. It requires a bit more time to get to a better solution and sometimes you're just using the first solution coming to you're mind.

A good example of a Student class using non-static and static variables would be

public class Student {
    
    private static int unitsToGraduate = 120;
    private int name;
    private int age;
    
    // [...]
    
}

... assuming there is only a single university or wathever the students are used by in your system. As soon as you have multiple universities with different unitsToGraduate values, you have to move the static variable into the university (regular member) and you should recognize: this value is a universities property.
That's why also this example is at last also not a good example for the usage of static variables.

In general speaking: You won't have to use static variables in most cases. And if you believe there is no other way without a static variable, there is probably still an other way. ;)

In general speaking: You won't have to use static variables in most cases. And if you believe there is no other way without a static variable, there is probably still an other way. ;)

Well you would actually have to use a static. Specifically, you would need to use a static method instead of the static variable to present encapsulation in your code.


As soon as you have multiple universities with different unitsToGraduate values, you have to move the static variable into the university (regular member) and you should recognize: this value is a universities property.

Would a programmer really do something like that? Given your scenario, I would actually make the unitsToGraduate an instance variable for each instance of University class.

The example I gave was demonstrated by a Stanford University professor.

I do agree, it depends on the situation.

As soon as you have multiple universities with different unitsToGraduate values, you have to move the static variable into the university (regular member) and you should recognize: this value is a universities property.


Would a programmer really do something like that? Given your scenario, I would actually make the unitsToGraduate an instance variable for each instance of University class.

Aren't you saying the same thing I did?
public class University {
    private int unitsToGraduate;
    private List<Student> students;
    // no static
    
    [...]
}

public class Student {
    private String name;
    private int age;
    // no static
    
    [...]
}

The example I gave was demonstrated by a Stanford University professor.

I'm uncertain about the situation in other countries, but as far as I heard about you should not rely on everything IT professors are telling.

This topic is closed to new replies.

Advertisement