Hardcoded là gì

My assignment asks me lớn access a test.txt document, so the tệp tin name has khổng lồ be hard coded lớn my C drive sầu. I have sầu no idea what hardcoding means. Can somebody toàn thân please help me with this?



"hard coding" means putting something inkhổng lồ your source code. If you are not hard coding, then you bởi something lượt thích prompting the user for the data, or allow the user to put the data on the comm& line, or something like that.

Bạn đang xem: Hardcoded là gì

So, to hard code the location of the tệp tin 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 ", 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.

Xem thêm: Hướng Dẫn Cài Đặt Win 8.1 Bằng Usb, Cách Cài Win 8

Improve sầu this answer
edited Dec 13 "09 at 11:40

Charles Salvia
48.4k1212 gold badges118118 silver badges138138 bronze badges
answered Dec 13 "09 at 7:04

66.7k1818 gold badges8585 silver badges112112 bronze badges
Add a bình luận |
"Hard Coding" means something that you want to lớn embeded with your program or any project that can not be changed directly. For example if you are using a database VPS, then you must hardcode khổng lồ connect your database with your project & that can not be changed by user.Because you have sầu hard coded.

Improve sầu this answer
answered Jan 18 "15 at 20:08

15111 silver badge22 bronze badges
Add a phản hồi |
The antonym of Hard-Coding is Soft-Coding. For a better understanding of Hard Coding, I will introduce both terms.

Xem thêm: Nhu Nhược Nghĩa Là Gì ? Dấu Hiệu Nhận Biết Người Nhu Nhược

Hard-coding: feature is coded khổng lồ the system not allowing for configuration; Parametric: feature is configurable via table driven, or properties files with limited parametric values ;Soft-coding: feature uses “engines” that derive sầu results based on any number of parametric values (e.g. business rules in BRE); rules are coded but exist as parameters in system, written in script form


// 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<1>:

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 to be anti-patterns<2>. Thus, one should strive sầu for balance between hard và soft-coding.

Hard CodingHard coding” is a well-known antipattern against which most website 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 file path or a remote host name, in the source code rather than obtaining it from a configuration file, a database, a user đầu vào, or another external source. The main problem with hard code is that it only works properly in a certain environment, & 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 lớn avoid the pitfall of hard coding, we can easily run inkhổng lồ another antipattern called “soft coding”, which is its exact opposite. In soft coding, we put things that should be in the source code inkhổng lồ external sources, for example we store business logic in the database. The most comtháng reason why we bởi so, is the fear that business rules will change in the future, therefore we will need khổng lồ rewrite the code. In extreme cases, a soft coded program can become so abstract và convoluted that it is almost impossible lớn comprehend it (especially for new team members), and extremely hard to maintain và 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 lớn hardcode values inkhổng lồ our applications?Wikipedia: HardcodingWikipedia: Soft-coding

Chuyên mục: HOT