Why do some programmers keep values in global variables or member variables but not reused?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Why do some programmers keep values in global variables or member variables but not reused?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật