Java 编码风格的资源?

发布于 2024-10-10 01:28:00 字数 200 浏览 0 评论 0原文

我发现阅读编码风格指南是了解一种语言及其特性的最佳方式。我一直在努力寻找一份关于 Java 的好文档,并立即得到一些问题的答案。

首先,排很长队的惯例是什么?使用 Java 和 C/C++ 进行编写使我在这一点上产生了很大的困惑。

其次,创建自定义异常类的准则是什么?我通常最终会抛出一个现有的异常,而不是创建自己的异常。通常会强制执行自定义异常创建吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

奶茶白久 2024-10-17 01:28:00

我将从 Sun/Oracle Java 编码标准开始。

对于行的字符宽度没有 100% 硬性和快速的标准。我想说 80-120 个字符之间。更大、更宽的屏幕会让我不那么担心这个。

我同意标准例外通常对我来说已经足够好了。我可能会出于特殊的业务意义而自定义异常子类,但这些都是很少见的。我想说 Java 的最新风格是更喜欢未经检查的异常,更像 C#。

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

摘星┃星的人 2024-10-17 01:28:00
  1. http://www.oracle.com/technetwork/java/codeconv-138413.html
  2. 换行为 80 - 110 个字符
  3. 扔掉现有的非常棒。您应该只创建自定义,当
    现有的异常并不完全是
    匹配特殊情况(
    通常是业务规则)

作为编码约定的简介:

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 

    }
  }
}

等等。

  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.

向日葵 2024-10-17 01:28:00

Joshua Bloch 的《Effective Java》是一本重要的读物。它超出了 Oracle 提供的语法指南。

第 60 项是“支持使用标准异常”,但第 61 项是“抛出适合抽象的异常”。有时需要自定义异常,有时则不需要。

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

谁与争疯 2024-10-17 01:28:00

可能重复的问题。您可以在这里找到答案:https://stackoverflow.com/questions/1334204/official-java -code-guidelines-conventions

但是,Sun/Oracle 网站上的文档已有 12 年以上的历史。尽管核心语言没有改变,但技术变化很快,这意味着我们使用该技术的方式发生了变化。

我建议使用最适合您和您的团队的方法。只要您的组织内存在一些被认为是主流的商定标准,您就应该没问题。

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.

回眸一笑 2024-10-17 01:28:00
  1. 当人们使用 80 个字符宽的文本编辑器时,行长度曾经是一个更大的问题。如今,大多数开发人员都拥有大型高分辨率宽屏幕,因此如果 Java 中的 80 个字符换行会迫使其他人向下滚动更多内容并在右侧留有空白,那么实际上可能会造成损害。记下谁可能会查看您编写的代码,并以适当的长度换行。

  2. 我对自定义异常类的看法是使用任何已经存在的,只要它有意义。但是,有时您需要一个不存在的异常类,因此,如果有意义,请毫不犹豫地创建一个。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文