My assignment asks me to access a test.txt
document, so the file name has to be hard coded to my C drive. I have no idea what hardcoding means. Can somebody please help me with this?
问题:
回答1:
"hard coding" means putting something into your source code. If you are not hard coding, then you do something like prompting the user for the data, or allow the user to put the data on the command line, or something like that.
So, to hard code the location of the file as being on the C: drive, you would just put the pathname of the file all together in your source code.
Here is an example.
int main()
{
const char *filename = "C:\\myfile.txt";
printf("Filename is: %s\n", filename);
}
The file name is "hard coded" as: C:\myfile.txt
The reason the backslash is doubled is because backslashes are special in C strings.
回答2:
"Hard Coding" means something that you want to embeded with your program or any project that can not be changed directly. For example if you are using a database server, then you must hardcode to connect your database with your project and that can not be changed by user. Because you have hard coded.
回答3:
Scenario
In a college there are many students doing different courses, and after an examination we have to prepare a marks card showing grade. I can calculate grade two ways
1. I can write some code like this
if(totalMark <= 100 && totalMark > 90) { grade = "A+"; }
else if(totalMark <= 90 && totalMark > 80) { grade = "A"; }
else if(totalMark <= 80 && totalMark > 70) { grade = "B"; }
else if(totalMark <= 70 && totalMark > 60) { grade = "C"; }
2. You can ask user to enter grade definition some where and save that data
Something like storing into a database table
In the first case the grade is common for all the courses and if the rule changes the code needs to be changed. But for second case we are giving user the provision to enter grade based on their requirement. So the code will be not be changed when the grade rules changes.
That's the important thing when you give more provision for users to define business logic. The first case is nothing but Hard Coding.
So in your question if you ask the user to enter the path of the file at the start, then you can remove the hard coded path in your code.
回答4:
the opposite ,antonym of Hard coding is soft coding , Hard coding vs soft coding so I think for a better understanding of this is better to read both meaning
In feature design, softcoding has other meanings.
- Hardcoding: feature is coded to the system not allowing for configuration;
- Parametric: feature is configurable via table driven, or properties files with limited parametric values ;
- Softcoding: feature uses “engines” that derive results based on any number of parametric values (i.e. business rules in BRE); rules are coded but exist as parameters in system, written in script form Hard Coding
Example :
// "hello world" is a hardcoded value
string firstName = "hello world";
https://softwareengineering.stackexchange.com/questions/67982/is-it-ever-a-good-idea-to-hardcode-values-into-our-applications
// vs NOT using hardcoded data value
Console.WriteLine("first name :");
string firstName = Console.ReadLine();
//another example
float areaOfCircle( int radius)
{
float area = 0;
area = 3.14*radius*radius; //3.14 is a [hardcoded value][1]
return area; }
https://www.quora.com/What-does-hard-coded-something-mean-in-computer-programming-context
1.“Hard coding” is a well-known antipattern against which most web development books warns us right in the preface. Hard coding is the unfortunate practice in which we store configuration or input data, such as a file path or a remote host name, in the source code rather than obtaining it from a configuration file, a database, a user input, or another external source.
The main problem with hard code is that it only works properly in a certain environment, and at any time the conditions change, we need to modify the source code, usually in multiple separate places.
- Soft Coding If we try very hard to avoid the pitfall of hard coding, we can easily run into another antipattern called “soft coding”, which is its exact opposite.
In soft coding, we put things that should be in the source code into external sources, for example we store business logic in the database. The most common reason why we do so, is the fear that business rules will change in the future, therefore we will need to rewrite the code.
In extreme cases, a soft coded program can become so abstract and convoluted that it is almost impossible to comprehend it (especially for new team members), and extremely hard to maintain and debug.
I have found some interesting explanation here , you can met Hard coding in different environments , IDE or programming languages . You mention a situation and I want to add something else
1. Hard coding (also hard-coding or hardcoding) is the software development practice of embedding an 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.
2. hardcoded string “row three”, should use @string resource
It is a very common android studio error/warning .
[Hard_coding][1] ;
[how-to-solve-this-issue-of-hardcoded-string][2];
[hardcoded-string-row-three-should-use-string-resource][3];
[1]: https://en.wikipedia.org/wiki/Hard_coding
[2]: https://stackoverflow.com/questions/34130513/how-to-solve-this-issue-of-hardcoded-string/34130992
[3]: https://stackoverflow.com/questions/8743349/hardcoded-string-row-three-should-use-string-resource
https://www.hongkiat.com/blog/code-optimization-coding-antipatterns/ https://en.wikipedia.org/wiki/Softcoding