formate JAVA_spring-javaformat

本文分享了在开发Spring Boot时确保代码一致性的实用技巧,包括如何排除特定检查、禁用代码块的格式化、设置换行限制、优化空白处理、注释规范以及变量声明的final使用策略。通过遵循这些实践,可以提高代码可读性和维护性。
摘要由CSDN通过智能技术生成

Tips

Formatting and Checkstyle alone are not enough to produce truly consistent code.

Here are some tips that we’ve found useful when developing Spring Boot.

Excluding specific checks

If you want most SpringChecks but need to exclude one or two, you can do something like this in your checkstyle.xml:

/p>

"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"

"https://checkstyle.org/dtds/configuration_1_3.dtd">

Disabling formatting for blocks of code

Some code isn’t particularly amenable to automatic formatting.

For example, Spring Security configurations often work better when manually formatted.

If you need to disable formatting for a specific block of code you can enclose it in a @formatter:off / @formatter:on set:

// @formatter:off

... code not be formatted

// @formatter:on

Wrapping

The source formatter uses 120 chars for wrapping. This aims to strike a balance between

making use of available horizontal space in your IDE and avoiding unwanted additional

wrapping when viewing code on GitHub and the like.

If you’re used to longer lines, 120 chars can take some getting used to. Specifically, if

you have many nesting levels things can start to look quite bad. Generally, if you see

code bunched up to the right of your screen you should take that as a signal to use the

“extract method” refactor. Extracting small private methods will improve formatting and

it helps when reading the code and debugging.

Whitespace

Keeping whitespace lines out of method bodies can help make the code easier to scan.

If blank lines are only included between methods it becomes easier to see the overall structure of the class.

If you find you need whitespace inside your method, consider if extracting a private method might give a better result.

Comments

Try to add javadoc for each public method and constant.

Private methods shouldn’t generally need javadoc, unless it provides a natural place to document unusual behavior.

The checkstyle rules will enforce that all public classes have javadoc.

They will also ensure that @author tags are well formed.

Final

Private members should be final whenever possible.

Local variable and parameters should generally not be explicitly declared as final since it adds so much noise.

Read-down methods, fields and parameters

Methods don’t need to be organized by scope.

There’s no need to group all private, protected and public methods together.

Instead try to make your code easy to read when scanning the file from top to bottom.

In other words, try to have methods only reference method further down in the file.

Keep private methods as close to the thing that calls them as possible.

It’s also recommend that you try to keep consistent ordering with fields and constructor parameters.

For example:

class Name {

private final String first;

private final String last;

public Name(String first, String last) {

this.first = first;

this.last = last;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: "unresolved plugin: 'io.spring.javaformat:spring-javaformat-maven-plugin:0.0." 这是Maven构建工具在解析插件时遇到的问题。具体来说,这个错误信息表示Maven无法找到所需的插件 'io.spring.javaformat:spring-javaformat-maven-plugin' 的版本 '0.0.*'。 解决这个问题的方法有几种。首先,可以尝试更新Maven仓库的索引,以确保最新版本的插件可用。可以使用以下命令执行此操作: `mvn clean install` 如果索引更新后仍然无法解决问题,可以尝试手动添加插件的依赖到项目的pom.xml文件中。可以在 `<build><plugins>` 标签下添加以下代码: ``` <plugin> <groupId>io.spring.javaformat</groupId> <artifactId>spring-javaformat-maven-plugin</artifactId> <version>0.0.*</version> </plugin> ``` 然后再次运行Maven构建命令,看是否能够成功解析该插件。 如果上述方法均无效,这可能是由于插件的版本号错误或不存在。可以在Maven仓库的网站上搜索该插件,并查看可用的版本号。然后在pom.xml文件中将插件的版本号更改为正确的版本。 如果所有尝试均未能解决问题,可能需要考虑使用其他可替代的插件或工具来达到相同的目的。可以在互联网上搜索相关的插件或工具,以寻找解决方案。 总之,要解决"Maven unresolved plugin: 'io.spring.javaformat:spring-javaformat-maven-plugin:0.0."错误,可以尝试更新仓库索引、手动添加插件依赖、检查插件版本号是否正确以及尝试其他插件或工具。 ### 回答2: unresolved plugin: 'io.spring.javaformat:spring-javaformat-maven-plugin:0.0.' 是一个插件未解决的错误信息。这通常意味着在项目配置中引用了一个未能正确下载或找到的 Maven 插件。 要解决这个问题,你可以尝试以下几个步骤: 1. 检查插件配置:确认你已正确配置了该插件的相关信息,包括插件的版本号以及 Maven 插件仓库的地址。 2. 确认插件可用性:在 Maven 插件仓库或其他可信赖的来源中搜索该插件的最新版本,并确保该插件是有效且可用的。 3. 检查网络连接:确认你的网络连接是否正常,因为下载和引用插件需要有效的网络连接。 4. 清理 Maven 缓存:如果问题仍然存在,尝试清理 Maven 的本地缓存。你可以在命令行中运行 `mvn dependency:purge-local-repository` 命令来清理缓存。 5. 更新 Maven 版本:如果你使用的是较旧的 Maven 版本,尝试将 Maven 更新到最新版本。新版本的 Maven 可能会修复一些已知的问题,并提供更好的兼容性。 如果在尝试上述解决方法后问题仍然存在,你可能需要进一步检查你的项目配置文件,确保没有其他冲突或错误与该插件相关联。此外,你还可以查看该插件的文档和讨论论坛,看是否有其他开发者遇到了类似的问题,并从中获取更多的解决思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值