scala编码规范

  1. 格式与命名
  2. 语法特性
  3. 编码风格
  4. 高效编码
  5. 编码模式
  6. 测试

格式与命名

1) 代码格式
用两个空格缩进。避免每行长度超过100列。在两个方法、类、对象定义之间使用一个空白行。

2) 优先考虑使用val,而非var。

3) 当引入多个包时,使用花括号:

import jxl.write.{WritableCell, Number, Label}

当引入的包超过6个时,应使用通配符_:

import org.scalatest.events._

4)若方法暴露为接口,则返回类型应该显式声明。例如:

    def execute(conn: Connection): Boolean = {
      executeCommand(conn, sqlStatement) match {
        case Right(result) => result
        case Left(_) => false
      }
    }

5) 集合的命名规范
xs, ys, as, bs等作为某种Sequence对象的名称;
x, y, z, a, b作为sequence元素的名称。
h作为head的名称,t作为tail的名称。

6)避免对简单的表达式采用花括号;

//suggestion
def square(x: Int) = x * x

//avoid
def square(x: Int) = {
     x * x
}

7) 泛型类型参数的命名虽然没有限制,但建议遵循如下规则:
A 代表一个简单的类型,例如List[A]
B, C, D 用于第2、第3、第4等类型。例如:
class List[A] {
def map[B](f: A => B): List[B] = …
}
N 代表数值类型

注意:在Java中,通常以K、V代表Map的key与value,但是在Scala中,更倾向于使用A、B代表Map的key与value。

语法特性

1) 定义隐式类时,应该将构造函数的参数声明为val。

2)使用for表达式;如果需要条件表达式,应将条件表达式写到for comprehension中:

//not good
for (file <- files) {
     if (hasSoundFileExtension(file) && !soundFileIsLong(file)) {
        soundFiles += file
     }
}

//better
for {
     file <- files
     if hasSoundFileExtension(file)
     if !soundFileIsLong(file)
} yield file

通常情况下,我们应优先考虑filter, map, flatMap等操作,而非for comprehension:

//best
files.filter(hasSourceFileExtension).filterNot(soundFileIsLong) 

3) 避免使用isInstanceOf,而是使用模式匹配,尤其是在处理比较复杂的类型判断时,使用模式匹配的可读性更好。

//avoid
if (x.isInstanceOf[Foo]) { do something …

//suggest
def isPerson(x: Any): Boolean = x match {
  case p: Person => true
  case _ => false
} 

4)以下情况使用abstract class,而不是trait:
* 想要创建一个需要构造函数参数的基类
* 代码可能会被Java代码调用

5) 如果希望trait只能被某个类(及其子类)extend,应该使用self type:

trait MyTrait {
   
    this: BaseType =>
}

如果希望对扩展trait的类做更多限制,可以在self type后增加更多对trait的混入:

trait WarpCore {
   
     this: Starship with WarpCoreEjector with FireExtinguisher =>
}

// this works
class Enterprise extends Starship
    with WarpCore
    with WarpCoreEjector
    with FireExtinguisher

// won't compile
class Enterprise extends Starship
     with WarpCore
     with WarpCoreEjector

如果要限制扩展trait的类必须定义相关的方法,可以在self type中定义方法,这称之为structural type(类似动态语言的鸭子类型):

trait WarpCore {
   
     this: {
        def ejectWarpCore(password: String): Boolean
        def startWarpCore: Unit
     } =>
}

class Starship
class Enterprise extends Starship with WarpCore {
   
     def ejectWarpCore(password: String): Boolean = {
          if (password == "password") { println("core ejected"); true } else false }
     def startWarpCore { println("core started") }
}

6) 对于较长的类型名称,在特定上下文中,以不影响阅读性和表达设计意图为前提,建议使用类型别名,它可以帮助程序变得更简短。例如:

class ConcurrentPool[K, V] {
   
   type Queue = ConcurrentLinkedQueue[V]
   type Map   = ConcurrentHashMap[K, Queue]  
}

7) 如果要使用隐式参数,应尽量使用自定义类型作为隐式参数的类型,而避免过于宽泛的类型,如String,Int,Boolean等。

//suggestion
def maxOfList[T](elements: List[T])
         (implicit orderer: T => Ordered[T]): T =
   elements match {
      case List() =>
         throw 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值