Tuesday, 7 July 2015

What is called Function Programming

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 List and Vector before reaching for the mutable ArrayBuffer. The contents of an ArrayBuffer are mutable
  • Prefer immutable variables. That is, prefer val to var.
There are at least two major benefits to using immutable variables (val) and immutable collections:
  • They represent a form of defensive coding, keeping your data from being changed accidentally.
  • They’re easier to reason about.
 4. Expression Oriented Programming

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.
In scala, some control blocks are also expressions. This means that if the control were to branch, each of these branches must evaluate to a value as well.

 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 var field in a class or method doesn’t have an initial default value, initialize it with Option instead of null.
  • When a method doesn’t produce the intended result, you may be tempted to return null. Use an Option or Try instead.
  • If you’re working with a Java library that returns null, convert it to an Option, 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 if statements to check for null values.
  • 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 a None instead of a Some[T]. This is a much better approach than returning null from a method that is expected to return an object.

No comments:

Post a Comment