2008-07-08

Standard Template Library - readable?

Since I called some STL code readable, I've recieved numerous mails and comments. Fellow Qt/KDE-er dhaumann added his 0.02 EUR to the pile and showed the nicety of template specialization.

Today I thought I'd follow up on some more comments.

First of all, my personal opinion is that C++ is just as readable as most other languages. Even in this case. What can be less readable are the horrendus error messages that one can end up with. I've even compiled a small list of error messages and what they really mean in plain English, but this does not - of course - help in ugly template cases. (Yes, the css of digitalfanatics.org is ugly and somewhat broken...)

Back to readability. Even plain English cannot express the two operations that I suggested in a short way. Both cases results in fairly long sentences. Also, try to formally parse and express such sentences with more "interesting" transformation functions.

"For every item in the source list, add 42 to it and replace it in the source list."

"For every item in the source list, add 42 to it and append it to the end of the destination list."

As the commenter zwabel pointed out, in the put-the-result-in-another-list a simple foreach loop showing the actual operation is good enough and reads better. I agree, but that kind of break my do-everything-on-a-line-thesis.

Titus Brown pointed out that I do too much on a single line. I have to disagree with this - looping and doing something on each item is simple enough.

Then I had lots of suggestions that I say that C++ isn't functional, but I use a functional style in my example. I'm not sure that I agree. My impression of the STL is that it can perform a number of functions on lists that I'm interested in, for instance.
  • Iterating (for_each)
  • Filtering (remove_if)
  • Searching (find_if)
  • Processing (transform)
To make these operations flexible, they accept a functor - either a predicate or an actual transformation operation. This isn't really functional programming in my eyes, even if it can be used for functional programming. To me, it is just a set of ways to perform iterations and common tasks.

Anyway, some fun thought that have poped up while discussing this topic. What if we combine these operations with the QtConcurrent framework. I have not tried the framework myself, but I was in Munich and listened to Morten's presentation of it, and to me it looks like it could be done. What is missing is a nice set of flexible map and filter functions, binders and such goodies that the STL carries.

3 Comments:

At 11:05 PM, Anonymous Anonymous said...

From Stroustrup's interview on slashdot [1]:

"... Through STL, the C++ community may have introduced more people to functional programming techniques and may have applied such techniques to more real-world problems than all previous languages put together. The fact that function objects are not the most flexible "closures" available doesn't detract from the fact that people understand them, like them, and use them."

See those parts of STL are functional; even Stroustup says that.

Also in his IEEE interview [2]:

"Undoubtedly, this code will look strange to someone who is not a C++ programmer and even to C++ programmers who have not yet internalized the latest techniques for using templates and overloading-and that, of course, is part of the purpose of presenting the examples here. However, the bottom line is that these techniques allow you to write efficient, type-safe, and generic code. People familiar with functional languages will note that these techniques are similar to techniques pioneered in those languages."


Now can you can argue that C++ is strictly a non-functional language which is true but STL clearly has functional programming influences.

[1] http://slashdot.org/developers/00/02/25/1034222.shtml
[2] http://www.research.att.com/~bs/ieee_interview.html

 
At 11:43 PM, Blogger Paul said...

Functors are just an implementation of first-class functions. That is functions that are treated as objects, they can be created at run time, stored in data structures, passed to other functions, etc... Lambdas are just another way to specify first-class functions.

functions like
* Iterating (for_each)
* Filtering (remove_if)
* Searching (find_if)
* Processing (transform)

are functions that receive other functions (functors), these are called "higher order functions".

Although C++ does not implement all the features necessary to be called a functional language, the concepts that you are using and appreciate are functional style programming.

Doing functional style programming on a non functional language (like you are doing), is like doing object oriented on a non object oriented language (GTK). It can be done, but the result is not quite as readable as it could be.

Certainly, stl is very nice and can simplify a lot of code. You will just end up reinventing wheels if you do any C++ without stl.

But I don't think your particular example really shows where stl shines, especially when people compare your code to equivalents in functional languages.

 
At 3:45 AM, Anonymous Anonymous said...

"Doing functional style programming on a non functional language (like you are doing), is like doing object oriented on a non object oriented language (GTK)."

It's like using an Exception library in C - it can be done, and it's even pretty cool. But when you compare it to a language with a native implementation, you quickly realize how ugly what you've been working with has been.

 

Post a Comment

<< Home