scala best practise

本文总结了Scala编程中应当遵循的最佳实践,包括避免使用return语句、合理管理状态变更、使用Option替代null等建议,旨在帮助开发者写出更高效、更安全的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

随便看看https://github.com/alexandru/scala-best-practices/

以下是我觉得讲得有意义的。


1. avoid using "return" in nested function.

2. avoid using mutable variables in most scenario unless you know that will only be using in single actor/thread.

3. avoid using exception for validation user input, the exception should only indicate the error.

4. avoid using "null", always using Option instead.

5. never use Option.get, this will throw the NoSuchElementException, using getOrElse or fold instead.

6. Use joda-time instead of java Date.

7 avoid using Any or AnyRef or isInstanceOf or asInstanceOf

8. no shared state, if really need it, we might can use Scala-STM for optimistic concurrency control / atomic reference.

9 case class should not nested in other class, otherwise ,the case class with serialize the whole class into it.

10. profiling with YourKit profiler, or VisualVM is also good enough.

11. must use scala blokingcontext on blocking I/O.

12. we can also use Scala-aync for better Await.result style.

13. should use a separate thread pool for blocking I/O such as JDBC operation.

14 avoid hardcode the thread pool or the execution context.

15 should mutable state in actors only with context.become.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值