Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4hardcode

What does it mean the something is Hardcoded ?


I'm following an MVC tutorial of MoviesMVC here , and the author says the following thing :

public string Index() 
{ 
    return "This is my <b>default</b> action..."; 
} 

Currently the Index method returns a string with a message that is hard-coded in the controller class. Change the Index method to return a View object, as shown in the following code:

public ActionResult Index() 
{ 
    return View(); 
}

Well, OK, so he changed the signature of the function . But why does he call it Hardcoded?


Solution

  • When you hard code something, it usually refers to things such as string values and other settings which are part of the compiled code.

    In short, if you want to change what the Index method is returning, you will have to make the change and recompile.

    The alternative is to use configuration files for settings, such as the app.config and web.config files. When it comes to certain strings (which are used as user interfacing messages) you use resource files which allow you to store multi language strings as well.

    What these files allow you to do is to make changes to your application without the need to recompile everything. This is desired (usually) because:

    1. When working in a production environment, it is usually the case where you have code which might tackle user flagged issues. If you need to recompile and redeploy to make a change in a text message (which it can be something quite often, especially during marketing campaigns), having to recompile the code to change a message can result into unwanted changes, or changes that are still in progress to be deployed.

    2. Certain clients might have their own marketing people and would like to give them access to certain site pages. If they can change the text on the go, it usually saves them time having to go through the development company, which usually results in happier clients.