Resources for Java coding style?

2019-01-25 21:36发布

I find reading coding style guidelines to be the best way to get to know a language and its peculiarities. I've been trying to find a good document for Java, and get some questions answered immediately.

Firstly, what is the convention for very long lines? Writing in both Java and C/C++ leads me to have a great deal of confusion on this point.

Secondly, what is the guideline for creating custom exception classes? I typically end up throwing an existing exception rather than creating my own. Is custom exception creation typically enforced?

7条回答
来,给爷笑一个
2楼-- · 2019-01-25 22:02

I'd start with the Sun/Oracle Java coding standards.

There isn't a 100% hard and fast standard for character width of lines. I'd say somewhere between 80-120 characters. Larger, wider screens would make me less concerned about this.

I would agree that the standard exceptions are usually good enough for me. I might a custom exception subclass for special business significance, but those are few and far between. I'd say that latest style for Java would be to prefer unchecked exceptions, more like C#.

查看更多
女痞
3楼-- · 2019-01-25 22:02

Possible duplicate question. You can find the answer here: https://stackoverflow.com/questions/1334204/official-java-code-guidelines-conventions

However, the documentation on Sun/Oracle's website is older than 12 years. Although the core language hasn't changed, technology changes rapidly, which means the way we work with that technology changes.

I would suggest using what works best for you and your team. As long as there is some agreed-upon standard within your organization that is considered mainstream, you should be okay.

查看更多
乱世女痞
4楼-- · 2019-01-25 22:09
霸刀☆藐视天下
5楼-- · 2019-01-25 22:11

"Effective Java" by Joshua Bloch is vital reading. It goes beyond the syntactic guidelines offered by Oracle.

Item 60 is "Favor the use of standard exceptions," but item 61 is "Throw exceptions appropriate to the abstraction." Sometimes custom exceptions are called for, and sometimes not.

查看更多
SAY GOODBYE
7楼-- · 2019-01-25 22:19
  1. http://www.oracle.com/technetwork/java/codeconv-138413.html
  2. Wrap to 80 - 110 chars
  3. It is great to throw existing. You should only create custom, when existing exception doesn't quite match the exceptional case ( usually a business rule )

As a brief for the coding conventions:

class SomeClassName { //<-- Class with upper case, camel case
   public static final int CONSTANT_SOMETIHNG = 0x2A; // constants all upper case
   private int secretVariable; // variables start with lowercase
   public void someMethod() {  // methods too 

       // opening brace goes in the same line 

       if( cond ) { 
           ...
       } else { 
           ...
       }
       for( .. ) {
       }
       while( ... ) { 
       }
       try { 
       } catch() { 
       } finally {
       }
       // closing brace aligned to the element start 

    }
  }
}

Etc. etc.

查看更多
登录 后发表回答