nus-cs2103-AY2223S2 / forum

12 stars 0 forks source link

Breaking up long code lines #82

Closed EvitanRelta closed 1 year ago

EvitanRelta commented 1 year ago

How should the below code be formatted?
Specifically, how much indent, whether the first element should be on the same line, and where should the closing bracket/brace be.

String[] values = new String[]{ "D", .isDone ? "1" : "0", this.description.toString(), this.endTime.toString() };

Based on the coding standard, i assume it should be:

String[] values = new String[]{ "D", 
        this.isDone ? "1" : "0", 
        this.description.toString(), 
        this.endTime.toString() };

But how about egyptian-styled brackets/braces:

String[] values = new String[]{
        "D", 
        this.isDone ? "1" : "0", 
        this.description.toString(), 
        this.endTime.toString()
};

Additionally, how bout the below code? Which one is acceptable?

assertEquals(
        TaskDeadlineTest.getTestTask(),
        TaskDeadlineTest.getTestTask());
assertEquals(TaskDeadlineTest.getTestTask(),
        TaskDeadlineTest.getTestTask());
damithc commented 1 year ago

On a general note, it is advisable to follow the coding standard strictly, without making further stylistic choices. That way, the result will be the same no matter which developer is writing the code. Otherwise we would need to discuss and decide the line wrapping choice every time there is a need to wrap a line.

Jnjy commented 1 year ago

Hi OP, I would like to share a link on Oracle Convention in addition to the Google Java Style Guide provided in the coding standard guide. Hope this will be helpful!

Also, I personally would prefer to follow the coding standard which led to me choosing this option:

assertEquals(TaskDeadlineTest.getTestTask(),
        TaskDeadlineTest.getTestTask());

Reason being that Egyption Style braces may be very confusing if there is a really long method with nested braces involved. An example (might not be the best) I can think of would be:

# option A
new A(
        veryLongParam1,
        new B(
            veryLongParam2, 
        veryLongParam3, 
        veryLongParam4))

vs

# option B
new A(veryLongParam1,
    new B(veryLongParam2, 
        veryLongParam3, 
        veryLongParam4))

Option B seems clearer and more intuitive to me as compared to option A. Would like to know what OP thinks about this though!