sábado, 19 de marzo de 2016

LINQ Recipe No. 1-4: What are the Benefits of Functional Programming?

Contents

1. Introduction
2. Key Words
3. Problem
4. Solution
5. Discussion
6. Conclusions
7. Literature & Links

1. Introduction

In this fourth LINQ recipe we will be familiarized with the benefits of functional programming paradigm. The comprehension and usage of these benefits -composability, lazy evaluation, immutability, parallelizability, and declarativity-, will allows to take advantage of functional programming, and write robust, stable, and powerful code. For this recipe, a basic explanation of these benefits will be presented to understand their fundamentals.

2. Key Words

  • .NET
  • Composability
  • Functional programming
  • Immutability
  • Lazy evaluation
  • LINQ
  • Parallelizability

3. Problem

What are the benefits of functional programming paradigm?

4. Solution

The benefits of this programming paradigm includes:
  • Composability
  • Lazy evaluation
  • Inmmutability
  • Parallelizability
  • Declarativity

5. Discussion

5.1 Composability

With composability, the programmer is able to partition a problem solution in a functional granular approach. This means that a set of methods or code blocks perform a single and well defined task. Throughout this principle, the creation of solutions for complex problems will be easier: "it's the only way to combat complexity." (Mukherjee, 2014). The solution code will be more maintainable and easy to refactor: thanks to the divide and rule principle.

5.2 Lazy evaluation

Lazy evaluation -or deferred execution-, is a sophisticated mechanism for recovering data using filters. These filters recover data only when some particular condition is met. Suppose you have a big collection of objects stored in a remote data storage and you decide to filter that collection according to some condition: only those objects which fulfill the condition will be shown when they are requested. With this in mind, the program performance is better compared to an imperative solution (where each of operations for recovering data will be evaluated).

5.3 Immutability

Immutability lets us write code free of side of effects. However, the functional programming paradigms does not guarantee us that our code implementation won't have side effects in its implementation. Instead, a set of good practices for functional programming exist to attack undesirable side effects in our program solutions.

According to Mukherjee (2014), side effects such as shared variables could make a program unstable and unreliable: multiple objects could try to access a certain memory area -a variable- at the same time, so this limits the program speed to the variable response time. A more reliable and feasible approach is start a thread with its set of states: this avoids waiting for access to shared variables.

5.4 Parallelizability

LINQ lets us use program elements such us AsParallel() ("ParallelEnumerable.AsParallel Method", 2016) and AsOrdered() ("ParallelEnumerable.AsOrdered Method", 2016) to parallelize code. Given that the code is free of side effects (immutability property) by design, most functional programs are easy to parallize.

[Note: In future LINQ recipes we will explore how the previous mentioned methods are used in C# code.]

5.5 Declarativity

Declarative programming lets us write code into a single line: this is done by using LINQ operators. The declarative code is very expressive and its readability can help us easy understand the purpose of an algorithm.

6. Conclusions

In this recipe we have learned what are the benefits of using functional programming in very general terms. In future recipes we will get deeper in each of those benefits with an extended explanation and comprehension, even with a lot examples to reinforce our understanding.

7. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
ParallelEnumerable.AsParallel Method (System.Linq) (2015, March 17). Retrieved from: https://msdn.microsoft.com/en-us/library/system.linq.parallelenumerable.asparallel%28VS.100%29.aspx?f=255&MSPPError=-2147217396
ParallelEnumerable.AsOrdered(TSource) Method (ParallelQuery(TSource)) (System.Linq) (2015, March 17). Retrieved from:
https://msdn.microsoft.com/library/dd413357(v=vs.100).aspx


V

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.