Blog - Title

Programming in a Functional Style - VB

Programming in a Functional Style - VB

  • Comments 0

[Table of Contents] [Next Topic]

We're now ready to talk more about some of the core FP concepts.

Declarative vs. Imperative Code

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
Imperative languages are state based.  Object-oriented programming languages such as C#, C++, and Java have extensive capabilities to maintain and modify state.  To extract and transform data from a data source in an imperative language, you typically need to iterate through the data, write code to filter the data, and write more code to create the data in your desired result set.  In many cases, you create intermediate result sets.

In contrast, declarative languages are transformative in nature.  Instead of coding algorithms that extract data and produce result sets, writing data transformation code in LINQ is more akin to describing the results of each successive transformation, without using state to manage the transformation.  In many cases, if a purely lazy approach will work, code written in the functional style works in a streaming fashion.

This is a much more powerful approach.  There are significantly more opportunities for bugs when keeping extensive state.  Another major advantage of the declarative approach is that the resulting code may be more comprehensible at a glance.  In contrast, to figure out the functionality of a block of involved imperative code might involve detailed perusing.  You have to understand the state that is created at each step of the process.  Of course, a bad programmer can still write bad declarative code, and a good programmer can write clear, commented imperative code.

Another advantage of the declarative approach is that the resulting code is typically smaller.

Sometimes when you are transforming data, for performance, you may materialize intermediate results.  A typical scenario is one where you have a massive amount of source data, and you transform it into another form that allows for easy, quick access.  For instance, you may transform a big collection of word documents into a much smaller sorted dictionary that is used in further transformations.  But in the functional approach, once you have created and populated the dictionary, you don't modify the contents of it further.

[Table of Contents] [Next Topic] [Blog Map]

Leave a Comment
  • Please add 5 and 1 and type the answer here:
  • Post