nus-cs2030 / 2223-s1

MIT License
1 stars 0 forks source link

Understanding criteria for magic number error to occur #79

Open OngJunLe opened 2 years ago

OngJunLe commented 2 years ago

Hi, I've read #38 regarding how to fix magic number error, but was wondering if I could further clarify when this error actually occurs? Referring to the 2 code snippets below, I'm not sure why I experienced magic number error for every single number in the former, but not the latter. It also seems cumbersome to have to continually declare constants for generic mathematical calculations.

image

image

SungMatt commented 2 years ago

Checkstyle checks for numbers other than -1, 0, 1, 2. Most of the flags come from here!

Justinn-Paul commented 2 years ago

Checkstyle checks for numbers other than -1, 0, 1, 2. Most of the flags come from here!

sorry could I ask does this mean if I want to cube something using Math.pow I would also encounter magic number error?

danqiye1 commented 2 years ago

If you want to use any other numbers, you can set them as a constant at the start of a class. Make sure you name the constants in a meaningful way.

SungMatt commented 2 years ago

@Justinn-Paul You should encounter the error! The way CS2030 has set up Checkstyle does not exclude such checks. If you're using Checkstyle in your own free time, you can choose to modify it in the checks.xml file to exclude functions in field declarations.

zhuyifu commented 2 years ago

i think to be safe, just declare static final for all numbers except 0 and 1