Is it bad practice to change the value of a static variable?
Asked Answered
P

8

10

I have a static string variable which i need to change possibly depending on the HTTP protocol.

Is it bad practice to change the static string variable>

static string QuoteWebServiceUrl = CommonFunctions.ReadAppSetting("QuoteWebServiceUrl");

if(url == "https")
{
  QuoteWebServiceUrl = CommonFunctions.ReadAppSetting("QuoteWebServiceUrlSecure");
}
else
{
  QuoteWebServiceUrl = CommonFunctions.ReadAppSetting("QuoteWebServiceUrl");
}

Thanks

Pad answered 15/2, 2011 at 15:49 Comment(2)
I think you've misunderstood the concept of static and/or are confusing it with const. Also the else in you code is pointless as QuoteWebServiceUrl will already equal the value you are setting.Leyden
i thought it might be a problem for threading issues. The if/else was just a random mock up of what i am doing thanks for your help thoughPad
G
11

No. Of course you can change the value of a static string variable. Why do you think it's a bad pratice?

Goodill answered 15/2, 2011 at 15:52 Comment(5)
Indeed not bad practice. But the OP's reason, such as it is, I'm sure has to do with the fact that mutable static fields are prone to threading issues. (Just like any fields, of course, but static fields are more likely to be shared across threads than instance fields.)Yonatan
I'm surprised this got so many upvotes given likely disastrous consequences in a multi-threaded environment.Oosperm
@CaptainTom: I didn't see anywhere OP says he is trying to load settings concurrently. We won't assume it's a multi-thread program by default, right? Otherwise a lot of answers on SO are incorrect.Goodill
Always take multi-threading into consideration when dealing with statics that are changing at runtime.Oosperm
@CaptainTom: I disagree. Consider lots of built-in classes, they are documented as NOT THREAD SAFE.Goodill
E
2

I mean, modifying a static variable is a non-issue. It's a variable. It can vary. So why would varying (i.e., modifying it) be a bad practice? Yes, there are situations where you shouldn't or have to be careful if you do, but in general, it's not.

The big issue here is reading application settings deep in the guts of your application. It kills maintainability and testability. It is a horrifically bad practice and I encourage you to stop immediately.

Erigeron answered 15/2, 2011 at 15:54 Comment(1)
I can't change the app settings thing since this is a company application I'm just working on modifying modules to. Thanks again thoughPad
W
2

In this case it looks like it's just a one-time setup, but you must be mindful of race conditions in a multithreaded environment, including ASP.NET.

Warehouse answered 15/2, 2011 at 15:59 Comment(0)
E
0

Programmatically speaking, from the language POV it's ok to change it.

Otherwise depends on the logic of the variable, the meaning it has in the overall business logic.

Exacerbate answered 15/2, 2011 at 15:56 Comment(0)
O
0

On its own, changing a static variable is not a bad thing.

However, I see no precautions taken regarding multiple instances of this class modifying the same static. This is a potential concurrency issue.

In addition, you should probably set the two settings in statics and choose the one that's appropriate based on your condition, but leave the statics alone once they are initialized.

Oosperm answered 15/2, 2011 at 16:25 Comment(0)
S
0

For multi-thread editing static variable is not good not scalable and can run in some random cases inappropriately. but......

There are some business logic where there is only one thread at a time to process or restricting to one thread. If that's the case its completely OK to use.

In those cases it is good to have and we may sometimes have no other options rather changing (one of the case is with when using WEBAPI with Async task and most importantly only one thread at time and user wants dynamic status update for each api hit)

Satterfield answered 22/10, 2020 at 8:18 Comment(0)
J
-1

You can use static property instead of static variable.


private static string QuoteWebServiceUrl 
{ 
    get 
    { 
        if(url == "https") 
        {   
            return CommonFunctions.ReadAppSetting("QuoteWebServiceUrlSecure"); 
        } 
        else 
        {   
            return CommonFunctions.ReadAppSetting("QuoteWebServiceUrl"); 
        } 
    } 
}

Static Property

Johnettajohnette answered 13/1, 2015 at 21:59 Comment(1)
This does nothing to address the question being asked.Epicardium
H
-2

No, it is not a bad practice in general. In your particular case, it is a terrible idea.

Herbherbaceous answered 15/2, 2011 at 16:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.