I am talking about normal PC applications, memory should be sufficient.
They declare global or member variables.
In each function/method, they use the same global/member variables.
At the beginning of each method, the variables are re-initialized so the old value is never used.
Why do they keep the variables but no chance to reuse the old value?
class A
{
List<Car> carList;
public List<Car> M()
{
// always re-init variable
this.carList = new List<Car>();
// ...
// fetch from DB
return this.carList;
}
public List<Car> N(string id)
{
// always re-init variable
this.carList = new List<Car>();
// ...
// fetch from DB
return this.carList;
}
}
5
It’s impossible to know the motivations behind this code given a single point in time. It could be the result of wear-and-tear on the class.
Imagine, for instance, that once the car list really did function as a member variable, with some methods modifying it and some retrieving it. Then, some bugs were discovered that were caused by different methods modifying the value at the wrong time – perhaps the introduction of multithreaded execution. Now the programmer went and changed all car-list-generating methods to return the list, rather than store it in the member variable. But people often change the minimum amount required to get things working, especially if rushed and working on a critical bug, and rather than creating a local varible, he just left the existing reference to the member variable. Voila!
1
They think that they are escaping declaring variable every time in each function. But this is not good programming habit to keep global variable if you every time initialize value.
Life cycle of global variable may lead to bad memory management in this case
In your example, it might likely be bad to keep the old value. If you have constructed one list of cars in N and returned that list to caller A, and then caller B makes a request using M, and M cleared the same list and populated a different set of cars, wouldn’t caller A perhaps be confused when suddenly the list mutated? It is more likely that Greg Hewgill’s comment to your question is entirely valid, it never should have been member state in the first place.
What you see there is just a symptom of what often accompanies this, and that is a large class with members at the the wrong layers of abstraction, a class that is probably doing too much for too many people and can often sprawl to the point of being indecipherable. If, on the other hand, reinitialized member state is your only problem with the class, then maybe you’re better off than those of us who have seen and worked with far worse.
1
It could be that you’ve got something like this:
class A
{
List<Car> carList;
public List<Car> M()
{
// always re-init variable
this.carList = new List<Car>();
// the following functions modify carList
sortTheCarList();
filterTheCarList();
frobnicateTheCarList();
return this.carList;
}
}
It possible that it at least makes sense. Although I still think its bad style to do it this way.