スタイルガイドを整備しても
“any style guide written in English is
either so brief that it’s ambiguous, or so
long that no one reads it.”
Bob Nystrom
The Hardest Program I've Ever Written
http://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/
Slide 8
Slide 8 text
エディタの設定を共有する?
人によって使っているエディタや
IDEは違う!
Slide 9
Slide 9 text
Code Formatter!!
“Spend more time discussing important
issues in code review and less time on
code style. Scalafmt formats code so that
it looks consistent between people on
your team.”
scalafmt.org
maxColumn (default: 80)
// before
class ExtendTest extends A with B with C with D
// after (maxCoulumn=20)
class ExtendTest
extends A
with B
with C
with D
※必ず指定した値以内に収まるわけではないことに注意
continuationIndent.callSite (default: 2)
// before
function(
foo: Type,
bar: Type
)
// after
function(
foo: Type,
bar: Type
)
Slide 18
Slide 18 text
continuationIndent.defnSite (default: 4)
// before
def function(
foo: Type,
bar: Type
)
// after
def function(
foo: Type,
bar: Type
)
Slide 19
Slide 19 text
continuationIndent.extendSite (default: 4)
// before
class UserProfile
extends Profile
with SomeTrait
// after
class UserProfile
extends Profile
with SomeTrait
Slide 20
Slide 20 text
align
「=」「<-」「%%」「//」などを縦方向で揃える設定
- none
- some (default)
- more
- most
align.tokens で詳細を設定できるが、上記の4種類で十分
Slide 21
Slide 21 text
align = none
// before
x match {
case "foo" => 1 // comment
case "hoge" => 123 // comment
case "foobar" => 12345 // comment
}
// after
x match {
case "foo" => 1 // comment
case "hoge" => 123 // comment
case "foobar" => 12345 // comment
}
Slide 22
Slide 22 text
align = some (default)
// before
x match {
case "foo" => 1 // comment
case "hoge" => 123 // comment
case "foobar" => 12345 // comment
}
// after (case match の => のみ align する)
x match {
case "foo" => 1 // comment
case "hoge" => 123 // comment
case "foobar" => 12345 // comment
}
Slide 23
Slide 23 text
align = more
// before
x match {
case "foo" => 1 -> 1 // comment
case "hoge" => 123 -> 123 // comment
case "foobar" => 12345 -> 12345 // comment
}
// after (// や -> も)
x match {
case "foo" => 1 -> 1 // comment
case "hoge" => 123 -> 123 // comment
case "foobar" => 12345 -> 12345 // comment
}
Slide 24
Slide 24 text
align = more
// before
val foo = 1
val hoge = 2
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"com.tanishiking" %% "scalaunfmt" % "0.0.1"
)
// after (= や sbtの % も)
val foo = 1
val hoge = 2
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value,
"com.tanishiking" %% "scalaunfmt" % "0.0.1"
)
Slide 25
Slide 25 text
align = most
// before
for {
x <- List()
yyy = 2
} yield {
// ...
}
// after (<- と = をもalign)
for {
x <- List()
yyy = 2
} yield {
// ...
}
RewriteRules - AvoidInfix
// before
foo toList map { x =>
x should have length 5
}
// after
foo.toList.map { x =>
(x should have).length(5)
}
Slide 28
Slide 28 text
RewriteRules - RedundantBraces
// before
val x: Int => String = {
case 2 => { "two" }
case _ => { "other" }
}
// after
val x: Int => String = {
case 2 => "two"
case _ => "other"
}
Slide 29
Slide 29 text
RewriteRules - RedundantParens
// before
for {
a <- b
if ((b ++ b).length >= 2)
} yield a
// after
for {
a <- b
if (b ++ b).length >= 2
} yield a
Slide 30
Slide 30 text
RewriteRules - SortModifiers
// before
final lazy private implicit val x = 42
lazy final implicit private val y = 42
// after
implicit final private lazy val x = 42
implicit final private lazy val y = 42
sortModifiers.order = [“implicit”, “final” … ] で順番の設定も可能
Slide 31
Slide 31 text
RewriteRules - SortImports
// before
import a.b.{c, a, b}, k.{
g, f
}
// after
import a.b.{a, b, c}, k.{f, g}