Coding Guidelines
These are coding guidelines strictly for ZIO contributors for ZIO projects and not general conventions to be applied by the Scala community at large.
Additionally, bear in mind that, although we try to enforce these rules to the best of our ability, both via automated rules (scalafmt) and strict reviewing processes, it is both possible to find existing code that does not comply to these rules. If that is the case, we would be extremely grateful if you could make a contribution, by providing a fix to said issue.
Last, but not least, these rules are continuously evolving and as such, refer to them once in a while when in doubt.
Defining classes and traits​
-
Value classes must be final and extend
AnyVal. This is done to avoid allocating runtime objects; -
Method extension classes must be final and extend
AnyVal; -
Avoid overloading standard interfaces. When creating services avoid using the same names as well known standard interfaces. Example: Instead of having a service
Randomwith methodsnextLong(n)andnextInt(n)consider choosing something likenextLongBounded(n)andnextIntBounded(n). -
Sealed traits that are ADTs (Algebraic data types) should extend
ProductandSerializable. This is done to help the compiler infer types; -
Regular traits and sealed trait that do not form ADTs should extend
Serializablebut notProduct; -
Traits should always extend
Serializable. (e.g.ZIO).
Final and private modifiers​
-
All methods on classes / traits are declared
final, by default; -
No methods on objects declared
final, because they arefinalby default; -
No methods on final classes declared
final, because they arefinalby default; -
All classes inside objects should be defined
final, because otherwise they could still be extended; -
In general, classes that are not case classes have their constructors & constructor parameters private. Typically, it is not good practice to expose constructors and constructor parameters but exceptions apply (i.e.
AssertionandTestAnnotation); -
All
valsdeclaredfinal, even in objects orfinal classes, if they are constant expressions and without type annotations; -
Package-private
valsand methods should be declaredfinal.
Refactoring​
-
If a class has all its members
final, the class should be declaredfinalandfinalmember annotations should be removed except constant expressions; -
All type annotations should use the least powerful type alias. This means, that, let us say, a
ZIOeffect that has no dependencies but throws an arbitrary error, should be defined asIO. -
Use
defin place ofvalfor an abstract data member to avoidNullPointerExceptionrisk.
Understanding naming of parameters or values​
ZIO code often uses the following naming conventions, and you might be asked to change method parameters to follow these conventions. This guide can help you understand where the names come from. Naming expectations can be helpful in understanding the role of certain parameters without even glancing at its type signature when reading code or class/method signatures.
-
Partial functions have a shortened name
pf; -
In ZIO implicit parameters are often used as compiler evidences; These evidences help you, as a developer, prove something to the compiler (at compile time), and they have the ability to add constraints to a method; They are typically called
evif there is only one. Orev1,ev2... if more than one; -
Promises are called
p(unless in its own class methods, in that case it is calledthat, like point 8 defines); -
Functions are called
fn,fn1, unless they bear specific meaning:use,release; -
ZIO effects are called
f, unless they bear specific meaning like partially providing environment:r0; -
Iterable are called
in; -
When a parameter type equals own (in a method of a trait) call it
that; -
Be mindful of using by-name parameters. Mind the
Function[0]extra allocation and loss of clean syntax when invoking the method. Loss of syntax means that instead of being able to do something likef.flatMap(ZIO.success)you require to explicitly dof.flatMap(ZIO.success(_)); -
Fold or fold variants initial values are called
zero.
Understanding naming of methods​
ZIO goes to great lengths to define method names that are intuitive to the library user. Naming is hard!!! This section will attempt to provide some guidelines and examples to document, guide and explain naming of methods in ZIO.
-
All operators that return effects should be lazy in all parameters that are not lambdas. Strict parameters are a source of bugs when users inadvertently call side effecting code in arguments to these parameters. Preventing these bugs is the responsibility of the effect system and lazy parameters allow the runtime to properly manage these side effects. There are two exceptions. First, strict parameters should be used when by name parameters would cause duplicate method signatures due to type erasure. Second, operators on high performance concurrent data structures such as
Ref,Queue, andHubshould be strict in their parameters. -
Methods that have the form of
List#zipare calledzip, and have an alias called<*>. The parallel version, if applicable, has the namezipPar, with an alias called<&>; -
Avoid the use of
effectin constructors as this pushes responsibility on users to identify what code is or is not side effecting whereas the effect system should handle this correctly regardless. Instead prefer more specific names such assucceed,attempt, orsuspend. -
The dual of zip, which is trying either a left or right side, producing an Either of the result, should be called
orElseEither, with alias<+>. The simplified variant where both left and right have the same type should be calledorElse, with alias<>; -
Constructors for a data type
Xthat are based on another data typeYshould be placed in the companion objectXand namedfromY. For example,ZIO.fromOption,ZStream.fromEffect; -
Parallel versions of methods should be named the same, but with a
Parsuffix. -
foreachshould be used for operators that effectually iterate over a collection. For example,ZIO.foreach. -
Variants of operators that accept arguments or return results in the context of an effect type should be suffixed by the name of the effect type, for example
mapZIOormapSTM. TheMsuffix should not be used as it is not idiomatic Scala and does not specify what effect type is involved. -
Use the
Discardsuffix for variants of methods that discard their results. For example,foreachDiscard. The_suffix should not be used as it is not idiomatic Scala and does not describe what it does. -
Methods that are necessarily side effecting should be prefixed with
unsafe, for exampleunsafeRun. This does not apply to methods on internal data types that are inherently imperative in nature, for exampleMutableConcurrentQueue.
Type annotations​
ZIO goes to great lengths to take advantage of the scala compiler in varied ways. Type variance is one of them.
The following rules are good to have in mind when adding new types, traits or classes that have either covariant or contravariant types.
-
Generalized ADTs should always have type annotation. (i.e.
final case class Fail[+E](value: E) extends Cause[E]); -
Type alias should always have type annotation. Much like in Generalized ADTs defining type aliases should carry the type annotations (i.e.
type IO[+E, +A] = ZIO[Any, E, A]).
When defining new methods, keep in mind the following rules:
- Accept the most general type possible. For example, if a method accepts a collection, prefer
Iterable[A]toList[A]. - Return the most specific type possible, e.g., prefer
UIO[Unit]toUIO[Any].
Method alphabetization​
In general the following rules should be applied regarding method alphabetization.
To fix forward references of values we recommend the programmer to make them lazy (lazy val).
Operators are any methods that only have non-letter characters (i.e. <*> , <>, *>).
-
Public abstract defs / vals listed first, and alphabetized, with operators appearing before names.
-
Public concrete defs / vals listed second, and alphabetized, with operators appearing before names.
-
Private implementation details listed third, and alphabetized, with operators appearing before names.
Scala documentation​
It is strongly recommended to use scala doc links when referring to other members.
This both makes it easier for users to navigate the documentation and enforces that the references are accurate.
A good example of this are ZIO type aliases that are extremely pervasive in the codebase: Task, RIO, URIO and UIO.
To make it easy for developers to see the implementation scala doc links are used, for example:
/**
* @see See [[zio.ZIO.absolve]]
*/
def absolve[R, A](v: RIO[R, Either[Throwable, A]]): RIO[R, A] =
ZIO.absolve(v)