nus-cs2103-AY2324S1 / forum

10 stars 0 forks source link

Java Coding Standard: Blanks between logical units #553

Closed papataco14 closed 11 months ago

papataco14 commented 11 months ago

Hi prof, I am confused as to what constitutes a "logical unit". I was under the impression that the coding standards were objective, but this rule seems to be ambiguous.

Here is the relevant section from the textbook regarding this:

☆☆ Logical units within a block should be separated by one blank line.

// Create a new identity matrix
Matrix4x4 matrix = new Matrix4x4();

// Precompute angles for efficiency
double cosAngle = Math.cos(angle);
double sinAngle = Math.sin(angle);

// Specify matrix as a rotation transformation
matrix.setElement(1, 1,  cosAngle);
matrix.setElement(1, 2,  sinAngle);
matrix.setElement(2, 1, -sinAngle);
matrix.setElement(2, 2,  cosAngle);

// Apply rotation
transformation.multiply(matrix);

Rationale: Enhances readability by introducing white space between logical units. Each block is often introduced by a comment as indicated in the example above.


The example given in the textbook makes sense, but it's hard to judge whether to apply the rule in other contexts. For example, in the mock quiz:

image

In addition to the other violations, should there be a blank line between the line 2 (declaration of a variable) and line 3 (for loop)? If there is, how should we indicate it for such questions? which line is the error in? If there isn't, why?

Here is the code snippet for ease of reference:

public void read_lines() {
    int FILE_SIZE = 0;
    for (int i = 0; i < 10; i++) {
        read("reading " + FILE_SIZE); // read
    }
}
damithc commented 11 months ago

@papataco14 this particular coding standard rule (i.e., not using a blank line to separate code blocks) will not be used in exam questions because it is rather subjective. However, you should still identify two or more consecutive blank lines as a coding standard violation.

papataco14 commented 11 months ago

Thank you for the clarification! I checked online and it seems that this rule is left to the discretion of developers and teams. Link