Question

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?

Was it helpful?

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.

OTHER TIPS

What does it mean the something is Hardcoded

When you write something in code and need the code to be changed in order to change the value then it is called hard code. If we load contents from any source like database then you do not need the code to be changed. There is possibility that some of content wont change like column heading then they must be written in code (hard coded).

Hard coding (also, hard-coding or hardcoding) refers to the software development practice of embedding what may, perhaps only in retrospect, be regarded as input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input, Reference

  1. Hardcoding is when you directly give a value instead of using a variable. Variables can be re-used at different locations in your code. So it is better not to hard code.

  2. View is suggested instead of directly providing the string as it is easy to localize & internationalize.

  3. Also you don't need to re-compile the code into DLL. You just have to upload the view (aspx, ascx, cshtml etc) and things start working.

  4. If you do not wish to use view, you can use string resource and use the resource id instead of hard coding.

One could build his/her own ViewEngine and insert it in the MVC pipeline. This would allow for the HTML content to be be determined only after the execution.

When someone codes the return statements with a string, there is no possibility to modify what the HTML content that is returned by that method, because it is not changeable.

It simply means you are printing the result or using some value, without getting it from database or from user input.

So suppose you are using person name like this. string personName = [getting value from database] string personName = [getting value from user input]

Than it is ok.

But if you are using it like this

string personName = "Smit"

Than you are just hardcoding the value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top