java - Java中通用括号之间没有空格的推理

标签 java whitespace checkstyle readability code-readability

看着 GenericWhitespaceCheck在 Checkstyle 文档中,

Left angle bracket (<):

  • should be preceded with whitespace only in generic methods definitions.
  • should not be preceded with whitespace when it is precede method name or following type name.
  • should not be followed with whitespace in all cases.

Right angle bracket (>):

  • should not be preceded with whitespace in all cases.
  • should be followed with whitespace in almost all cases, except diamond operators and when preceding method name.


我不确定我是否完全理解背后的原因 <后面不应该跟一个空格以及为什么 >不应在 1 之前。

换句话说,为什么是 Map<String> session 结束 Map < String > ?

这仅仅是因为随着参数数量和深度的增加,没有空格的版本更具可读性。

喜欢,Map<String, List<String>>Map < String, List < String > > 更具可读性?

另外作为一个一般问题,是否有一些存储库/指南可以解释 Checkstyle 约定背后的原因?

最佳答案

简介an early tutorial on generics (从 2004 年开始)说(强调我的):

This tutorial is aimed at introducing you to generics. You may be familiar with similar constructs from other languages, most notably C++ templates. If so, you’ll soon see that there are both similarities and important differences. If you are not familiar with look-a-alike constructs from elsewhere, all the better; you can start afresh, without unlearning any misconceptions.



这是承认 Java 泛型看起来像 C++ 模板。 C++ 模板通常也会省略开头后面的空格 < , 以及收盘前 > .

围绕 Java 泛型的约定将遵循它们在早期教程中的编写方式。

关于java - Java中通用括号之间没有空格的推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59503986/

相关文章:

java - 使用 Checkstyles 防止准备好的语句泄漏

java - JdbcTemplate 插入成功但数据库中没有行

java - 完成 future : whenCompleteAsync() does not let me re-throw an Exception

java - 如何在 Spring 中使用 @Configuration 属性前缀和点属性来映射属性文件?

xml - 使用 FOP 在生成的 PDF 中保留空格

java - 我知道如何从字符串中删除元音,但如何从字符串中删除某些空格

javascript - 在 Sonar 中显示 jsHint 和 cssLint 结果

findbugs - 在增量构建中检查样式、PMD、FindBugs

java - SQLite Java和sqlite4java

javascript - Alfresco javascript 搜索不区分大小写