Reputation: 28252
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?
Upvotes: 5
Views: 5334
Reputation: 86411
"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.
Upvotes: 2
Reputation: 23629
Line lengths used to be much more of an issue when people used 80 character wide text editors. Today most developers have large high resolution wide screens so wrapping at 80 characters in Java might actually be a disservice if it forces others to scroll down more and have whitespace on the right. Take note of who might be looking at the code you write and wrap it at an appropriate length.
My view for Custom Exception classes is to use whatever already exists as far as it makes sense. However, there are times that you need an exception class that does not exist, so if it makes sense don't hesitate to create one.
Upvotes: 0
Reputation: 199224
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.
Upvotes: 2
Reputation: 34038
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.
Upvotes: 1
Reputation: 308763
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#.
Upvotes: 2
Reputation: 91319
Take a look at the official Code Conventions for the Java TM Programming Language.
Upvotes: 5