Enterprise Software

Modularization through functional programming

Functional programming, which is often viewed as academic, has gone mainstream. Tony Patton examines functional programming's key features to give you an idea of what it has to offer.

When my mind finally registered the details of functional programming, I dismissed it as what I had learned in college, but that is only partially correct. While I did learn the concepts of functions to reduce code and improve code management, it was paired with procedural techniques that provided step-by-step instructions. Functions were called along the way, thus a hybrid approach via languages like C, Basic, and Pascal — and it is easily achieved with Java, C#, Visual Basic, and so forth. Here's a look at the key features of functional programming.

Functionality

While this isn't an exhaustive list, functional programming can be broken down into main features like provability and modularity, which lends itself to simplifying debugging and testing. Provability is more an academic feature, as it promotes the idea that a function can be mathematically proven. This is an impractical exercise, but it is worth noting. The lack of global variables and state can be partially attributed to this feature.

Modularity is self-explanatory, as each group of code as a function is a module by itself. It could be used by different applications due to its independence. Different modules are glued together to build an application. This modularity leads to the simplification of testing and debugging. Each function is a test case subject — you only need to simulate the function input and work with that. Furthermore, debugging is straightforward, as code is contained in functions, which should be easier to trace problems.

Data immutability is another aspect of functional programming. The basic idea is data is not shared, which is implied due to the concept of a function as an island. Developers do not have to worry about data problems since data is not shared and, thus, no need to worry about state. Data can be passed to/from functions via pointers.

Another possible feature is concurrency. The separation inherent in functional programming lends itself to concurrent processing. This allows developers to take advantage of multi-core processors.

JavaScript supports functional programming techniques, which should be adopted since repetitive JavaScript code is messy and can add to a download time. With that said, it is a good language for demonstrating simple concepts. JavaScript provides features, but it is not a purely functional language. The following self-contained function sums the contents of an array. It is called, and the results are displayed via the alert function.

function AddValues(a) {

var res = 0;

for (i=0;i<a.length;i++) {

res += a[i];

}

return res;

}

alert AddValues([1,2,3,4,5,6,7,8]);

You could roll the alert call into the function by passing a function:

function AddValues(a, func) {

var res = 0;

for (i=0;i<a.length;i++) {

res += a[i];

}

func(res);

}

A good example of real-world functional programming is provided by the behemoth Google and MapReduce, which essentially makes them so scalable. The scalability is a direct result of a key feature of functionality programming — code isolation. That is, functions are isolated, thus no danger from other code when implementing concurrent processing. The CouchDB database platform uses MapReduce.

While you can force some languages to be functional using certain features, there are languages that are purely functional; good examples are Haskell, Erlang, Clojure, and F#. The F# language had a clear presence at this year's Microsoft TechEd conference, but I couldn't find many developers actually using it.

Scrummy

One feature of functional programming that seems to be missed is how easily it lends itself to the scrum approach to development. The separation of functionality seems to clearly map to scrum. The development of separate functions can be easily assigned to developers for a sprint — the functions break up the work for you. The functions are not dependent on other code, so developers can easily work independently.

A hybrid approach

No mechanic uses one tool for every job, so why should a developer use one technique for every application? This is exactly what many developers do as a near fanatical adherence to an approach like object-oriented development occurs. Fortunately, this is personal preference and not the rule.

Languages provide the options, and we can easily use what we need. A hybrid approach is often used as different parts of a project will require a different approach. A good example is using object-oriented programming for the user interface with a functional approach applied to the business layer. The final results depend upon the situation.

Nothing is ever out of style

The buzz over object-oriented programming has been around for years; pundits applaud its application, but good developers know there is more than one way to solve a problem and while one approach can work, another may be a better fit. This is true of functional programming, as many scenarios lend themselves to a functional approach that provides the necessary functionality and processing. Be sure to have an open mind when developing a solution and keep the end result in mind.

Also read: Functional programming techniques can improve software security and Where functional programming fits in.

What is your preferred development methodology? Do you utilize functionality programming techniques in your daily work? Share your thoughts with the TechRepublic community.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks