O
O
October2019-10-30 12:01:15
Java
October, 2019-10-30 12:01:15

In what cases, it is possible to omit parentheses in the if construct and will it be correct?

Good afternoon guys!
Tell me, is the code below aesthetically correct? Or does the aesthetics of writing code say that you should always put brackets? Or again, it's all a matter of taste?

public class TestII {

  public static void main(String[] args) {
    int a = 10;
    int b = 11;
    if (a < b)
      System.out.println("True.");
    else
      System.out.println("False");
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danila, 2019-10-30
@gcs_dk

By convention, parentheses should always be:
Note:if statements always use braces {}. Avoid the following error-prone form:
if (condition) //AVOID! THIS OMITS THE BRACES {}!statement;
proof: https://www.oracle.com/technetwork/java/codeconven...
IMHO:
But there may be exceptions:
- personal project
- agreements within the team.
And these exceptions work until someone else starts working or reading with this code - in this case, it is advisable to adhere to the standards.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question