nus-cs2103-AY2021S1 / forum

20 stars 2 forks source link

Coding standard violation question #487

Closed sudogene closed 3 years ago

sudogene commented 3 years ago

image

In the above code, is it not considered a violation to use magic numbers like 10 in the for loop?

public void read_lines() {              // should be readLines()
    for (int i = 0; i < 10; i++) {      // int NUM_LINES = 10; ?
        read("reading");
    }
}
GeNiaaz commented 3 years ago

I believe it is considered a mistake to use magic numbers, you are not wrong there. But they specifically mentioned "coding standard used in the tP", which as I understand it, is the checkstyle that we all love.

As such, the checkstyle would have flagged line 1 for not following camelCase for methods, however, it is not designed to catch magic literal errors, and hence the mistake caught by the tP standard is line 1.

richardcom commented 3 years ago

I think that maybe magic numbers affect code qualify, but may not affect coding style.

damithc commented 3 years ago

I think that maybe magic numbers affect code qualify, but may not affect coding style.

Yes, more precisely, the coding standard used in the tP has a specific set of rules; it doesn't have any rule about magic literals. It's the code quality guidelines that mention magic numbers. Usually, rules in the coding standard are mostly objective and we apply them without question (ideally), whereas other code quality guidelines are subjective and are applied on a case-by-base basis.

sudogene commented 3 years ago

Sorry now I'm a little confused, there are two entities here: Coding standard and Coding style/quality? I thought they were the same thing

edit: Never mind I just saw #490

damithc commented 3 years ago

Coding standard and code style guide are two terms used interchangeably. Both refer to a specific document the project follows in terms of code style. In our case, this is what you were supposed to follow.

Code quality is a general concept. Our textbook has a chapter containing some guidelines on improving code quality, one of which is to ensure the code is written in a consistent style by choosing and following a coding standard (or a style guide).