Difference between hard coding and non-hard coding (soft coding)

In a computer program, hardcoding refers to a method of replacing a variable variable with a fixed value. Using this method, it becomes very difficult to change the word variable after compilation. Therefore, in most programming languages, it is possible to define a fixed value as a tag, and then use this special tag in place of the variable name. When the tag name is changed, the variable name does not change. In this way, when the program is compiled, all variables are no longer fixed values, which makes it easier to achieve the purpose of changing variables.

So in general, hard-coded methods should be avoided:

E.g:

java example:

Hardcoded: if(a==2): return false:

Not hardcoded: if(a==b):return false:

Hardcoding usually means: writing the value as a constant, not a variable

 

C++ example

hardcode:

for(int I = 0; I<120; i++){

       ...

}

Not hardcoded:

#define MAX_USER_CNT 120

for (int i=0; i<MAX_USER_CNT; i++){

       ...

}

The 120 here is digitally "hardcoded", which not only makes the program hard to read, but also hard to maintain. If you want to modify 120, just modify all the related 120 in the program. Numerical "hard-coding" should be declared as a macro, so that the program is not only readable, but can be changed entirely.

 

Soft coding is a little more complicated than hard coding, and it is more thoughtful for later considerations. Soft coding is a design, while hard coding is nothing but a concrete implementation. Software development not only requires the realization of a software, but also requires that the software can be well modified and easily extended, so some design skills are needed in it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325019410&siteId=291194637