1. Referentially Transparent
An expression is referentially transparent (RT) if it can be replaced by its resulting value without changing the behavior of the program.
2. Pure Function
A function
f is
pure if the expression f(x) is referentially transparent for all
referentially transparent values x.we can make these statements about pure functions:
- A pure function is given one or more input parameters.
- Its result is based solely off of those parameters and its algorithm. The algorithm will not be based on any hidden state in the class or object it’s contained in.
- It won’t mutate the parameters it’s given.
- It won’t mutate the state of its class or object.
- It doesn’t perform any I/O operations, such as reading from disk, writing to disk, prompting for input, or reading input.
3. Prefer Immutable Variable
There are two components to “prefer immutability”:
- Prefer immutable collections. For instance, use immutable
sequences like
ListandVectorbefore reaching for the mutableArrayBuffer. The contents of anArrayBufferare mutable - Prefer immutable variables. That is, prefer
valtovar.
val) and immutable
collections:- They represent a form of defensive coding, keeping your data from being changed accidentally.
- They’re easier to reason about.
Statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all.
there are several benefits to EOP:
- The code is easier to reason about. Inputs go in, a result is returned, and there are no side effects.
- The code is easier to test.
- Combined with Scala’s syntax, EOP also results in concise, expressive code.
- Although it has only been hinted at in these examples, expressions can often be executed in any order. This subtle feature lets you execute expressions in parallel, which can be a big help when you’re trying to take advantage of modern multicore CPUs.
5. Eliminate null Value
There are several common situations where you may be tempted to use
null values, so this recipe demonstrates how
not to use null values in those
situations:- When a
varfield in a class or method doesn’t have an initial default value, initialize it withOptioninstead ofnull. - When a method doesn’t produce the intended result, you may be
tempted to return
null. Use anOptionorTryinstead. - If you’re working with a Java library that returns
null, convert it to anOption, or something else.
It’s a best practice to never call
get on an
Option. The preferred approaches
are to use getOrElse, a match
expression, or foreach.Following these guidelines leads to these benefits:
- You’ll eliminate
NullPointerExceptions. - Your code will be safer.
- You won’t have to write
ifstatements to check fornullvalues. - Adding an
Option[T]return type declaration to a method is a terrific way to indicate that something is happening in the method such that the caller may receive aNoneinstead of aSome[T]. This is a much better approach than returningnullfrom a method that is expected to return an object.
No comments:
Post a Comment