[Blog Map] [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: EricWhite.com/blogBlog TOCIn the fall of 2006, I wrote a tutorial on Query Composition using Functional Programming Techniques in C# 3.0. In that tutorial, I tried to expose my learning process as I discovered for myself the transition in approach to writing code in the functional programming style. I wrote the tutorial very shortly after I had (some of) the necessary functional programming epiphanies. I wanted to communicate the ideas behind functional programming while the transition from object-oriented programming to functional programming was fresh in my mind. It was fun to write.
This is an updated version of that tutorial, containing my new insights into coding in the functional style using C#. For those who read my previous tutorial, there is new material here. As with the previous tutorial, I'm going to target this tutorial to C# developers who have no functional programming experience, i.e. the typical object-oriented coder.
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 C# 3.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 static method for a class. 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.
At the time I wrote the tutorial, I wasn't actually sure how my development as a functional programmer would proceed. Would I continue to like it? What else would I learn about this style of development? Well, I've learned quite a bit. I made a couple of theoretical mistakes in the previous version of this tutorial, and they are corrected in this tutorial. I also made a couple of implementation mistakes, also fixed. Some of the stuff in that tutorial isn't something that you use often, so I'm relegating that material to an appendix (not yet written). I have learned new coding approaches to solving problems in the FP style. And I have also developed a code formatting style that works well for me.
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 fully 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 this 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.
This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries....
This is a tutorial on using Functional Programming (FP) techniques for constructing LINQ queries. It is certainly possible to write simple LINQ queries without using these techniques, but as soon as you start writing more complicated queries, you need