Welcome to MSDN Blogs Sign in | Join | Help
Programming in a Functional Style

[Table of Contents]  [Next Topic]

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

Declarative vs. Imperative Code

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]

 

Posted: Tuesday, October 03, 2006 11:43 PM by EricWhite
Filed under:

Comments

barrkel said:

I'd write this:

Sequence.Range(1, 10).ForEach(i => Console.WriteLine(i));

as this:

Sequence.Range(1, 10).ForEach(Console.WriteLine);

... personally... :)

# January 10, 2007 3:15 PM

dcab said:

Agreed with barrkel.

And I'd use Action<T> instead of introducing duplicated VoidFunc<T0>(T0 a0);

# January 11, 2007 7:48 AM

Chua Wen Ching said:

Hi,

I am using Visual Studio 2008 Beta 2, I can't seem to detect this:

Sequence (I can't find a namespace for it too)

Is this built by you or?

Any help?

Thanks.

# September 27, 2007 8:52 AM

mattias w said:

One of the big problems when doing functional prigramming in C# are the data structures in C#/.NET, they are not side-effect free!

In functional programming, you are used to be able to add a element to a list or tree, but the old part of the tree can still be used elsewhere (unchanged!).

I haven't decided if I should use F# more, or if I should start using the data structures of F# in C#. The reason for continuing to use C# is tools like R#, and that it is easier to let other programmers enhance the program.

# November 7, 2007 2:31 AM

EricWhite said:

WRT side-effects and C#/.NET, this is absolutely true. C# relies on the discipline of the developer to write code that has no side-effects. However, this allows approaches where you write code that is locally impure, but globally pure, and this results in far more readable code.

I've been contemplating what it would look like to write a C# compiler that would enforce purity for methods that are private to a class. Of course, this brings problems with things like partial classes - how do you enforce purity for all private classes.

# November 7, 2007 7:53 AM

Joshua Allen [msft] said:

In your code sample, you need to change Sequence. to Enumerable. in order to run and compile on VS2008 RTM.

# December 25, 2007 6:31 PM

DotNetNerds blog said:

LINQ extensions .nogen ideer til hvad vi mangler?

# October 31, 2008 8:04 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker