{"id":185,"date":"2008-07-08T20:01:00","date_gmt":"2008-07-08T18:01:00","guid":{"rendered":"http:\/\/www.thelins.se\/johan\/blog\/2008\/07\/standard-template-library-readable\/"},"modified":"2008-07-08T20:01:00","modified_gmt":"2008-07-08T18:01:00","slug":"standard-template-library-readable","status":"publish","type":"post","link":"https:\/\/www.thelins.se\/johan\/blog\/2008\/07\/standard-template-library-readable\/","title":{"rendered":">Standard Template Library &#8211; readable?"},"content":{"rendered":"<p>>Since I called some STL code readable, I&#8217;ve recieved numerous mails and comments. Fellow Qt\/KDE-er dhaumann <a href=\"http:\/\/dhaumann.blogspot.com\/2008\/07\/c-template-magic.html\">added his 0.02 EUR to the pile<\/a> and showed the nicety of template specialization.<\/p>\n<p>Today I thought I&#8217;d follow up on some more comments.<\/p>\n<p>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&#8217;ve even compiled a <a href=\"http:\/\/www.digitalfanatics.org\/index.php?title=CompilerErrors\">small list of error messages<\/a> and what they really mean in plain English, but this does not &#8211; of course &#8211; help in ugly template cases. (Yes, the css of digitalfanatics.org is ugly and somewhat broken&#8230;)<\/p>\n<p>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 &#8220;interesting&#8221; transformation functions.<\/p>\n<p>  &#8220;<span style=\"font-style: italic;\">For every item in the source list, add 42 to it and replace it in the source list.<\/span>&#8220;<\/p>\n<p>  &#8220;<span style=\"font-style: italic;\">For every item in the source list, add 42 to it and append it to the end of the destination list.<\/span>&#8220;<\/p>\n<p>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.<\/p>\n<p>Titus Brown pointed out that I do too much on a single line. I have to disagree with this &#8211; looping and doing something on each item is simple enough.<\/p>\n<p>Then I had lots of suggestions that I say that C++ isn&#8217;t functional, but I use a functional style in my example. I&#8217;m not sure that I agree. My impression of the STL is that it can perform a number of functions on lists that I&#8217;m interested in, for instance.<\/p>\n<ul>\n<li>Iterating (for_each)<\/li>\n<li>Filtering (remove_if)<\/li>\n<li>Searching (find_if)<\/li>\n<li>Processing (transform)<\/li>\n<\/ul>\n<p>To make these operations flexible, they accept a functor &#8211; either a predicate or an actual transformation operation. This isn&#8217;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.<\/p>\n<p>Anyway, some fun thought that have poped up while discussing this topic. What if we combine these operations with the <a href=\"http:\/\/doc.trolltech.com\/main-snapshot\/threads.html#qtconcurrent-intro\">QtConcurrent framework<\/a>. I have not tried the framework myself, but I was in Munich and listened to Morten&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>>Since I called some STL code readable, I&#8217;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&#8217;d follow up on some more comments. First of all, my personal opinion is that C++ is just as readable as most &hellip; <a href=\"https:\/\/www.thelins.se\/johan\/blog\/2008\/07\/standard-template-library-readable\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">>Standard Template Library &#8211; readable?<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":0,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thelins.se\/johan\/blog\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}