Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 913
telsokari Avatar asked Feb 15 '11 15:02

telsokari


2 Answers

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

like image 166
Cheng Chen Avatar answered Oct 06 '22 00:10

Cheng Chen


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.

like image 20
jason Avatar answered Oct 05 '22 23:10

jason