本文总结了一些 Scala 开发工具的最佳实践。

1 IDE

1.1 选择 IntelliJ IDEA 或 VS Code

我曾在微软工作几年,离开微软后有一次有人看到我不会使用 Visual Studio 的单步调试非常吃惊。是的,在微软的几年里我从来没有用过单步调试,因为无法单步执行一个分布式的多线程软件。To IDE or not to IDE 视频让我在 IntelliJ IDEA 和 VS Code 之间有了明确的选择。程序员 90% 的时间是在设计和阅读程序,一个方便的代码展示工具是最核心的功能。其它的类似单步调试这样的功能对专业程序员用处不大,原因是程序的所有逻辑在程序员脑中需要非常清楚,而且专业程序员开发的都是多线程分布式软件,单步 Debug 这种功能几乎没有用处。当然,阅读代码需要方便的展示代码相关信息和跳转非常必要,这方面 VS Code 和 Metals 基本满足了开发需求。另外就是 VS Code 对 Web 开发的强大支持。熟练掌握一个 类似 VS Code 这样的 Editor 应该够了。熟练的意思是可以不加思考的使用快捷键复制二行代码或执行 mult-cursor(多光标)编辑这样的功能。

1.2 VS Code 配置

  • Scala Metals: it is a language server that uses an embedded Bloop build server for sbt build.

2 代码标准

2.1 Scalafmt

Scalafmt is integrated with sbt and VS code. It comes with Scala Metals. Just set "editor.formatOnSave": true.

You can turn the format on and off using comments. // format on and // format off.

In the project root, create .scalazfmt.conf. The mandatory setting is the version: version = "2.3.0". Please use the latest stable version nubmer.

2.2 Scalafix

Scalafix is a refactoring and linting tool for Scala. A good feature of Scalafix is that it is easy to write custom linter and rewrite rules.

To use it, first configure the sbt plugin in project/plugins.sbt: addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.11")

2.3 Wartremover

WartRemover is a scala linter.

Add the following to your project/plugins.sbt: addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.4.3").

configure the linter in your build.sbt. By default, all errors and warnings are turned off. To turn on all checks that are currently considered stable, use: wartremoverErrors ++= Warts.unsafe.

To turn on all available errors (some have false positives), use: wartremoverErrors ++= Warts.all.

To exclude a specific piece of code from one or more checks, use the SuppressWarnings annotation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@SuppressWarnings(Array("org.wartremover.warts.Var", "org.wartremover.warts.Null"))
var foo = null

// suppress all warts
@SuppressWarnings(Array("org.wartremover.warts.All"))
var bar = null
To exclude a file or directory from all checks, use wartremoverExcluded in your build.sbt file:

wartremoverExcluded += baseDirectory.value / "src" / "main" / "scala" / "SomeFile.scala"
wartremoverExcluded += sourceManaged.value

3 sbt

3.1 配置

3.2 插件

3.3 编译开关

4 git 流程

4.1 gitignore

4.2 基于微服务的流程