Resources for Java coding style?

I think coding style reading rules are the best way to learn a language and its features. I tried to find a good document for Java and immediately got some questions.

First, what is the convention for very long lines? Writing in Java and C / C ++ leads me to a lot of confusion on this issue.

Secondly, what is the guideline for creating custom exception classes? I usually throw an existing exception, and not create my own. Is regular exception throwing created?

+4
source share
7 answers

Take a look at the official Code for Java ™ programming language .

+5
source

I would start with Sun / Oracle Java standards.

There is no 100% hard and fast standard for character widths. I would say somewhere between 80-120 characters. Larger, wider screens would not bother me.

I would agree that standard exceptions are generally good enough for me. I can create a special subclass of exception for a particular business value, but this is a little and far away. I would say that the last style for Java would be to prefer unlimited exceptions, more similar to C #.

+2
source

As a brief description of 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.

+2
source

Efficient Java by Joshua Bloch is vital. It goes beyond the syntax recommendations offered by Oracle.

Paragraph 60 - "Use standard exceptions", but paragraph 61 - "Exclude exceptions corresponding to abstractions." Sometimes user exceptions are thrown, and sometimes not.

+2
source

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

However, the documentation on the Sun / Oracle website is over 12 years old. Although the main language has not changed, the technology is changing rapidly, which means that we are working with this technology.

I would suggest using what works best for you and your team. As long as your organization has a certain agreed standard, which is considered the main one, you should be in order.

+1
source
  • Line lengths were most often a problem when people used text editors 80 characters wide. Today, most developers have large high-resolution screens, so an 80-character wrapper in Java can actually be bad if it forces others to scroll down and have spaces to the right. Pay attention to who can look at the code you write and wrap it in the appropriate length.

  • My view for Custom Exception classes is to use everything that already exists, as far as it makes sense. However, there are times when you need an exception class that is not there, so if that makes sense, feel free to create one.

0
source

Source: https://habr.com/ru/post/1334016/


All Articles