In the last three years that I have worked as developer, I have seen a lot of examples where people use a switch statement to set the path (both in back-end and front-end) for a URL. Below is an example of this:
Back-end example (C#):
public static string getHost(EnvironmentEnum environment){
var path = String.Empty;
switch (environment)
{
case EnvironmentEnum.dev:
path = "http://localhost:55793/";
break;
case EnvironmentEnum.uat:
path = "http://dev.yourpath.com/";
break;
case EnvironmentEnum.production:
path = "http://yourpath.com/";
break;
}
return path;
}
Front-end example (JavaScript):
(function () {
if (window.location.host.indexOf("localhost") !== -1) {
window.serviceUrl = "http://localhost:57939/";
}
else if (window.location.host.indexOf("qa") !== -1) {
window.serviceUrl = "http://dev.yourpath.com/";
}
else {
window.serviceUrl = "http://yourpath.com/";
}
})();
It has been discussed whether it is a good or bad practice, and I think it is a bad practice, because we must avoid this kind of code and set a proper configuration. But to be honest I really don't know the proper answer and why is it not recommended and what is the correct way to implement this.
can someone explain it the pros and cons of the above practice?