The bad usage of "wave brackets" that Java professionals avoid depends on the rules, so let's read the rules.

Java Advent Calendar 2020 This is the 14th day article. We maintain and develop ERP package software. Click here for main business I was planning to write a practical article when OOME happened more seriously, but I thought about it after seeing an article like the title.

"** What is the" bad "use of" wave brackets "that Java professionals avoid? Aiming for more appropriate programming ** "

"No" is too power word, maybe fishing? Or, I thought about it, even feeling worried that it would be erased soon.

Conclusion

You can also find out by looking at standard conventions and style guides elsewhere. The following three.

1. Object Club Java Coding Standard

http://objectclub.jp/community/codingstandard/CodingStd.pdf

The coding style conforms to the JDK source of Sun Microsystems, Inc. Indente The session is basically the same as the C language style of K & R, but the definition of classes and methods is opened. Write the first "{" without line breaks.

  1. Google https://google.github.io/styleguide/javaguide.html#s4.1-braces Quote:

Examples.java


// This is acceptable
void doNothing() {}

// This is equally acceptable
void doNothingElse() {
}

// This is not acceptable: No concise empty blocks in a multi-block statement
try {
  doSomething();
} catch (Exception e) {}
  1. Cookpad https://github.com/cookpad/styleguide/blob/master/java.ja.md#braces Quote:

[MUST] Do not start the line with curly braces if there is a token immediately before. Place the curly braces on the same line as the token.

Examples.java


// good
class MyClass {
    int func() {
        if (something) {
            // ...
        } else if (somethingElse) {
            // ...
        } else {
            // ...
        }
    }
}

// bad
class MyClass
{
    int func()
    {
        if (something)
        {
            // ...
        }
        else if (somethingElse)
        {
            // ...
        }
        else
        {
            // ...
        }
    }
}

[MUST] Add curly braces to conditional statements.

Examples.java


// good
if (condition) {
    body();
}

// bad
if (condition)
    body();

So, if you think you're a "programming beginner", of course you'll discover it by reading the conventions! is.

Beginning of original article

It is said that the big difference depending on the programming language is how to use "wave brackets" (also called "braces" and "middle brackets"). No, it's a language specification whether to use curly braces or not, so as a user (coding side), I use it but just follow anything. In Delphi, it's begin ~ end.

It's mainly on Twitter, so there are some things I don't really like, but there are various opinions. https://ceron.jp/url/techtarget.itmedia.co.jp/tt/news/2012/12/news01.html

--You can force the writing style by using the code style. ――It depends on the coding standard. It cannot be compiled unless the parentheses are supported. ――I think it's a prejudice. --Religious war ...

etc.

For example

The following is stated to be an example of a beginner. Certainly not a major.

Beginner.java


public void flag()
{
  boolean flg = true;
  int i = 10;
  if (flg == true)
  {
    for (int i = 0; i < 10; i++)
    {
      System.out.print("flag is true");
    }
  }
  flg = false;
}

On the other hand

The approach taken by an experienced engineer is to put a curly brace at the end of the first line of the code block.

In order for the above source code to pass a general code review, it would need to be formatted as follows (some say it should be written as "flag" instead of "flag == true"):

Written casually (Some say you should write" flag "instead of" flag == true ") has nothing to do with the parentheses.

"Good" usage

Then, how to write is as follows.

Better.java


public void flag() {
  boolean flg = true;
  int i = 10;
  if (flg == true) {
    for (int i = 0; i < 10; i++) {
      System.out.print("flag is true");
    }
  }
  flg = false;
}

However, "whether or not there are right-handed parentheses corresponding to all left-handed parentheses" is judged by the compiler, so it sounds like either one is fine. It's up to the team to "buy insanely from other engineers in the development team."

So, I have found that I can conclude, look at standard rules and style guides from other places. A project with a minimum style guide is a good project.

That's it!

reference

I wrote it with reference to the following. About etiquette to be careful when publishing articles on Qiita ~ Let's stop copying the contents of the net and books without permission ~

Recommended Posts

The bad usage of "wave brackets" that Java professionals avoid depends on the rules, so let's read the rules.
[Read Effective Java] Chapter 2 Item 5 "Avoid the creation of unnecessary objects"