Jump to content

hard coded definition


hisoka

Recommended Posts

I do not know the meaning of the word "hard coded" . I read many definitions about it in the internet but my brain is still unable to grasp its meaning . Could someone tell me what is the meaning of "hard coded" in a simple way that my brain can understand ?

Link to comment
Share on other sites

Hardcoded refers to data that is embedded directly within the code rather than coming from an external source. Usually hardcoded information is bad because updating the value means modifying code which is generally more risky and time consuming than having all the necessary data in a place specifically intended to store data.

 

This is a hardcoded string:

$str = "Hardcoded string";

Here's a string that's not hardcoded:

$str = get_string_from_the_file();

In both cases $str ends up containing the same string, but in the first case the string was embedded directly in the code.

  • Like 1
Link to comment
Share on other sites

but a normal program is hard coded too

 

Take a look at this :

 

<?php
$x = 10 ;
$y = 20;
echo $x + $y ;
?>

 

This is a tiny simple program in which all the variables are embedded directly in the code and any modification in one variable creates a modification in the whole program or in the result of the program . Thus , a normal program can be a hard coded program . So what is the difference between a normal and a hard coded program ?

Edited by hisoka
Link to comment
Share on other sites

That program is useless because it can be replaced with this:

30

Programs like that are just made for educational purposes to explain variable assignment and operators.

 

The program could be made useful if you did not hardcode the values, for example:

<?php
$x = $_GET['x'];
$y = $_GET['y'];
echo $x + $y;
?>

Now you can send data to it with a form or by changing the query string, allowing the user to change the result.

Link to comment
Share on other sites

All what you did is evaluating and criticizing the little code above which is , absolutely , not what I asked for . My question was what is the difference between a normal program and a hard coded program? if we take in consideration that even a normal program has a data that is directly embedded in it like the little program above shows . Please confine yourself to the question and answer exactly what I am asking for

Link to comment
Share on other sites

All what you did is evaluating and criticizing the little code above which is , absolutely , not what I asked for . My question was what is the difference between a normal program and a hard coded program? if we take in consideration that even a normal program has a data that is directly embedded in it like the little program above shows . Please confine yourself to the question and answer exactly what I am asking for

I would just add that you should be more mindful of what users on the forum are trying to teach you. I understand that example may not have been immediately obvious to you, but the point was still fundamental, because in fact the example was a direct answer to your question. (using values from user input vs fixed / hardcoded / embedded / etc in the code itself). There was a better way to respond such as, "that reply is not clear to me could you expand further, I don't get xyz..". In so much that you would like people to continue helping you.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...