Scala 编码风格和约定? [英] Scala coding styles and conventions?

查看:46
本文介绍了Scala 编码风格和约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为 Scala 离简单太远了,就像它的语法一样.例如 Martin Odersky 在他的书中写了这个方法:

I think Scala goes too far from simplicity, like its syntax. For example Martin Odersky wrote the method in his book :

def calculate(s: String): Int =
  if (cache.contains(s))
    cache(s)
  else {
    val acc = new ChecksumAccumulator
    for (c <- s)
      acc.add(c.toByte)
    val cs = acc.checksum()
    cache += (s -> cs)
    cs
  }

如果方法增长,阅读代码变得非常痛苦,我无法匹配大括号,无法在IDE中折叠方法.是否有任何 Scala 编码约定?我觉得表达一个简单的方法太灵活了:

If the methods grows, it becomes very painful to read the code, I can't match curly braces, can't fold the method in IDE. Is there any Scala coding conventions out there? I feel it's too flexible to express a simple method:

def add(b: Byte): Unit = {
  sum += b
}

def add(b: Byte): Unit = sum += b

def add(b: Byte) { sum += b }

推荐答案

如果方法增长,阅读代码会变得非常痛苦".我认为部分答案是方法不应该增长.函数式编程风格就是有很多小方法.计算方法已经大了.

"If the method grows it becomes very painful to read the code". I think part of the answer is that methods should not grow. The functional programing style is to have many small methods.The calculate method is already on the large side.

要回答有关 Scala 编程风格指南的更一般性问题:这是一个代表性示例.

To answer the more general questions about style guides for Scala programing: Here's a representative example.

这篇关于Scala 编码风格和约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆