5

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 Answers 7

5

Take a look at the official Code Conventions for the Java TM Programming Language.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd also recommend the The Java Language Specification at java.sun.com/docs/books/jls
These conventions are outdated and not available anymore. They were written longs ago.
2

JavaRanch Java Programming Style Guide

Code Conventions for the Java Programming Language

Java Programming Style Guidelines

Comments

2

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#.

1 Comment

Yes, indeed. Exceptions should be exceptional.
2
  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.

1 Comment

4. Not throwing may -- I would argue "often is" ;-) -- be better than not throwing.
2

"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.

Comments

1

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.

Comments

0
  1. 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.

  2. 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.

2 Comments

The main problem with long lines is when you need to print them. Not a really big deal if the printing software used wraps long lines automatically, but may hurt readability if lines are wrapped at random places and are not indented.
This is true, but it has been years since I needed to print code out. Our team does code reviews electronically. If this poster does the same then printing is not a large factor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.