5 ms·
Carefully consider the following description of a function: The function adds 5 to i, and if j is less than three, the function sets it to 2. Does it describe
by marginalia_nu 4y ago
Carefully consider the following description of a function:
The function adds 5 to i, and if j is less than three, the function sets it to 2.
Does it describe this code?
function f() {
i = i + 5;
if (j < 3) {
j = 2; // <--
}
}
... or does it describe this code?
function f() {
i = i + 5;
if (j < 3) {
i = 2; // <--
}
}
The answer is yes, and this is ultimately why we use programming languages rather than natural language to instruct computers.
- lupire 4y agoCode can have ambiguous scope too, except the compiler has a decision making process that you might not fully understand. (Closures are famous for this problem). In the English you can recognize the ambiguous parse and reject it.
- marginalia_nu 4y agoThe meaning of code is defined by rules. The only ambiguity exists in human understanding of code. Natural language doesn't have such rules. My example above isn't an example of incorrect English, there isn't a correct interpretation. The grammar is correct and both interpretations are correct.