nus-cs2113-AY2324S2 / forum

2 stars 0 forks source link

Clarification on the definition of "Magic Number" #44

Closed Kobot7 closed 1 month ago

Kobot7 commented 1 month ago

In the mock paper question 2, it is said to be correct that "The code has an issue related to magic numbers".

Question #: 2 [1 mark] Suppose you are reviewing the code below. Assume the coding standard is similar to the one you used in the CS2113/T. image

Choose the incorrect options. (select all that apply; no partial points)
A.The method is named inappropriately
B.The header comment is missing some vital information about the method’s behavior
✓C. The commenting intensity inside this method (i.e. excluding the header comment) is a perfect
D.The code has an issue related to magic numbers

In the question, I'm assuming that the supposed "magic number" is actually referring to "ABC avenue" on line 7.

The following lines are taken from the textbook:

When the code has a number that does not explain the meaning of the number, it is called a "magic number" (as in "the number appears as if by magic")...

Similarly, you can have ‘magic’ values of other data types.
return "Error 1432"; // A magic string!
Avoid any magic literals in general, not just magic numbers.

The textbook seems to imply that the terms vary based on the data type. e.g. 2 would be a "magic number", but Error 1432 would be a "magic string". Assuming that this is true, the code in the question has an issue related to "magic strings" or "magic literals", but not "magic numbers"

Is the term "magic number" is supposed to have the equivalent meaning of "magic literals"?

okkhoy commented 1 month ago

"Is the term "magic number" is supposed to have the equivalent meaning of "magic literals"?"

Yes.

In the mock test, I thought I fixed it before releasing. This is my error. The option should be read as: "The code has an issue related to magic literal"

Kobot7 commented 1 month ago

Noted, thank you prof!