Introduction to the FP Tutorial for Visual Basic

[Table of Contents] [Next Topic]

Some time ago, I was talking to some members of a team that used LINQ and LINQ to XML in one particular area of their code.  They said that the code written using a conventional approach to XML was in the order of 6000 lines of code.  When re-written using LINQ and LINQ to XML, it was around 800 lines of code.  It was faster to code, and it was easier to debug.  And there is some correlation between lines of code and bugs regardless of the type of code, so reducing lines of code means better software.

This blog is inactive.
New blog:

Blog TOC
In the early part of 2008, I wrote a tutorial on Query Composition using Functional Programming Techniques in C# 3.0.  In that tutorial, I explained some of the basics of programming in the functional style using C#.  Visual Basic 9.0 also has the same facilities for functional programming.  This tutorial presents the same concepts and examples using VB 9.0.  I've targeted this tutorial towards VB developers who have no functional programming experience.

I’ve created a separate tutorial for Visual Basic developers because there are a few differences in semantics between the languages, and it is awkward to explain each language in a single topic.  In addition, interspersing VB and C# code reduces the readability of the tutorial.  I elected to not use VB’s integrated XML axes, because the axes (while cool) are not relevant to the points that I’m making.  Once you understand the functional approach, using the integrated XML axes is straightforward.

One thing that I want to say at this point:  This stuff is easy.  It's also really fun.  You don't have to read academic papers to learn about, enjoy, and benefit from functional programming in VB 9.0.  There are about half a dozen concepts you need to learn, each one easy.  Concepts like just a new way to write a method (that has no name), or a new way to write a method in a module.  Then you put them all together, and the result is more than the sum of the parts.

Some traditional OOP developers that I have talked to have had various reactions, including complete resistance to learning about functional programming, and wondering why they would want to use it.  However, my message is that there IS something to get here.  In the last 18 months, I have written a fair number of programs, almost all of them in the pure functional style.  My experience is that functional programming does reduce code line count for certain types of problems.  They are easier to code and debug.  They are easier to maintain.

A few years ago, I took a class in mountaineering.  I learned all about lots of stuff, including figure 8 knots, belay devices, and prussic knots, which is a knot that is commonly used to ascend a rope if you don't have a mechanical device (a jumar ascender).  At one point, we had to take a test where we had to start at the bottom of a rope and ascend the rope to the top using our prussic knots.  The instructors tied a knot in the rope that we were ascending, so we had to learn the techniques of bypassing a knot or obstruction in the rope that we were ascending, all the while staying tied securely to the rope, and maintaining good safety.  One of the students asked the question, "Why do we have to learn this particular technique?  Is it really likely that we will end up in the wild, and need to ascend a rope and get around a knot?"  The instructor's reply was enlightening.  She said, "It isn't really that you are going to encounter this exact situation.  However, the skills that you learn doing this exercise are the skills that you will apply in many other situations.  If you can do this properly, you can do many other necessary technical climbing activities properly."

The example presented in this tutorial shows you how to write code to extract the text of paragraphs of an Open XML document, along with the associated style of each paragraph.  You may not have a particular need to do so (or you may).  However, just as learning to ascend a rope using prussic knots, if you work through the example, and understand the code in each step of the tutorial, you will gain a set of skills that you can combine and recombine in many other situations to solve very different problems.  Learning about functional programming made me a better and faster coder.

The topics in this tutorial are presented in order.  Each topic either builds upon a previous one, or presents new material.  Eventually, we pull it all together into some involved query expressions, so as new material is presented, if you don't understand its applicability, be patient.  All will be made clear eventually.  And if you understand a topic such as lambda expressions or extension methods, feel free to skip ahead.

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

Skip to main content