Hard code là gì

My assignment asks me to lớn access a test.txt document, so the tệp tin name has to lớn be hard coded khổng lồ my C drive. I have no idea what hardcoding means. Can somebody please help me with this? Bạn vẫn xem: Hardcoded là gì

Bạn đang xem: Hard code là gì

*

*

"hard coding" means putting something into your source code. If you are not hard coding, then you bởi vì something like prompting the user for the data, or allow the user lớn put the data on the command line, or something lượt thích that.Bạn đã xem: Hardcoded là gì

So, lớn hard code the location of the tệp tin as being on the C: drive, you would just put the pathname of the tệp tin all together in your source code.

Here is an example.

int main() const char *filename = "C:\myfile.txt"; printf("Filename is: %s ", filename);The file name is "hard coded" as: C:myfile.txt

nội dung Improve this answer Follow edited Dec 13 "09 at 11:40

Xem thêm:

*

*

*

Examples:

// firstName has a hard-coded value of "hello world"string firstName = "hello world";// firstName has a non-hard-coded provided as inputConsole.WriteLine("first name :");string firstName = Console.ReadLine();A hard-coded constant:

float areaOfCircle(int radius) float area = 0; area = 3.14*radius*radius; // 3.14 is a hard-coded value return area;Additionally, hard-coding & soft-coding could be considered lớn be anti-patterns. Thus, one should strive for balance between hard và soft-coding.

Hard CodingHard 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 đầu vào data, such as a tệp tin 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, và at any time the conditions change, we need to lớn modify the source code, usually in multiple separate places. Soft Coding If we try very hard lớn 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 bởi so, is the fear that business rules will change in the future, therefore we will need to lớn rewrite the code. In extreme cases, a soft coded program can become so abstract and convoluted that it is almost impossible to lớn comprehend it (especially for new team members), và extremely hard to lớn maintain & debug.

Sources và Citations:

1: Quora: What does hard-coded something mean in computer programming context?2: Hongkiat: The 10 Coding Antipatterns You Must Avoid

Further Reading:

Software Engineering SE: Is it ever a good idea to hardcode values into our applications?Wikipedia: HardcodingWikipedia: Soft-coding