<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>David R. MacIver - Latest Comments in Functional code != Good code</title><link>http://drmaciver.disqus.com/</link><description></description><atom:link href="https://drmaciver.disqus.com/functional_code_good_code/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 18 Aug 2008 03:27:28 -0000</lastBuildDate><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265737</link><description>&lt;p&gt;You can shoot your foot in any programming style.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Adam Shelley</dc:creator><pubDate>Mon, 18 Aug 2008 03:27:28 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265736</link><description>&lt;p&gt;Daniel:&lt;/p&gt;&lt;p&gt;I wouldn't like to do it that way because the structural types implementation sucks. :-)&lt;/p&gt;&lt;p&gt;The way I'd like this to work is with a decent numeric hierarchy in Scala. Unfortunately we don't have one.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">david</dc:creator><pubDate>Fri, 15 Aug 2008 13:22:50 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265735</link><description>&lt;p&gt;WRT the implementation of sum, I would *like* to do it this way (doesn't compile):&lt;/p&gt;&lt;p&gt;type T = { def +[A &amp;lt;% T](o: A): T }&lt;br&gt;def sum(nums: Iterable[T]) = nums.reduceLeft[T](_+_)&lt;/p&gt;&lt;p&gt;Scala doesn't support self-referencing types (classes are fine, types are not).  I'm sure there's some deep theoretical reason why this is, but it does get a little annoying for situations like this one.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Spiewak</dc:creator><pubDate>Fri, 15 Aug 2008 13:19:35 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265734</link><description>&lt;p&gt;David,&lt;/p&gt;&lt;p&gt;I don't believe that your example demonstrates what you're attempting to demonstrate.  Functional programing IMHO is more than replacing for loops with map/fold/recursion etc.  The key benefit is referential transparency.  Or avoiding *visible* side effects.  Both the functional and the non-functional examples could be wrapped up in a function and both functions would be referentially transparent, and therefore would both be employing the functional programming paradigm.  In fact if you were to look under the covers of the foldLeft function you may find out that it mutates some variable somewhere.  Likewise if you look at the code that is generated by ghc (a haskell compiler) you'll see that some of the code it generates is also imperative.  That doesn't make haskell an imperative language, because whether or not there is mutation happening under the covers is irrelevant in haskell all functions are referentially transparent and the haskell programmer can pretend that no mutation is taking place ever.  It's probably possible to come up with some examples where an imperative approach is better than a functional approach, but to really show that you're going to have to use more than a single line of code.  You'll have to have multiple functions, where at least one of them modifies some global, or object variable, and that causes at least one function to return different results given the same input.  I think demonstration can be done, but it will take more work.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Evan</dc:creator><pubDate>Fri, 15 Aug 2008 10:14:42 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265733</link><description>&lt;p&gt;I'm not actually saying anything about imperative vs. functional. I'm saying "If you're going to write functional code, do it right". Exactly the same point could be made about imperative code, but it seems a little more obvious in that context because people are more familiar with it.&lt;/p&gt;&lt;p&gt;RE the implementation of sum, I don't really care how the sum method is implemented to be honest. If Iterable had a reduce method (as opposed to reduceLeft and reduceRight) I might advocate sum(x : Collection[Int]) = if (x.isEmpty) 0 else x.reduce(_+_) to prevent from introducing artificial order dependencies. But that's neither here nor there.&lt;/p&gt;&lt;p&gt;As far as factoring into small methods go: Obviously you have to find a middle ground. There are various rules of thumb as to when to do this and when not. I'm certainly not advocating def plusOne(x : Int) = x + 1. In fact, I'm not really advocating anything at all. This post is more of a cautionary tale than an instruction set to follow.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">david</dc:creator><pubDate>Fri, 15 Aug 2008 09:14:10 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265732</link><description>&lt;p&gt;Ok, now how would you write the sum method:  imperative or functional?&lt;/p&gt;&lt;p&gt;I don't think the issues your addressing is really an imperative versus functional.  It's an issue of "how small of common expression is too small to factor into another method/function?"&lt;/p&gt;&lt;p&gt;I think you're saying: "If the meaning of the expression is unclear and the meaning of the name will be clear, make it a named method/function."&lt;/p&gt;&lt;p&gt;That makes sense to me, until I end up with 20,000 methods with names longer than the expressions they contain...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Erik Engbrecht</dc:creator><pubDate>Fri, 15 Aug 2008 09:00:31 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265731</link><description>&lt;p&gt;Daniel:&lt;/p&gt;&lt;p&gt;Certainly for trivial computations I find that the functional style works better. For non-trivial ones it's often better, but occasionally you have to vastly reorder your thinking in order to get results that are only somewhat better (or even worse) than the imperative version.&lt;/p&gt;&lt;p&gt;Tony:&lt;/p&gt;&lt;p&gt;Unfortunately, Scala's numeric hierarchy sucks profoundly, so there's not a good way to abstract between different types of numbers. Even getting sumFrom to work properly is a bit of a nuisance. In principle this is just an implementation detail though, and it should be possible to write a sum method that does the right thing based on type (It's even mostly possible to retrofit this onto the hierarchy without changing the library implementation or language, but no one has done so).&lt;/p&gt;&lt;p&gt;Collin, Muharem:&lt;/p&gt;&lt;p&gt;There's a time and a place for stating your axioms and elaborating on the meaning of a term. I didn't feel this was it.&lt;/p&gt;&lt;p&gt;For these purposes I'm talking mostly about code reuse and code complexity relative to the task solved.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">david</dc:creator><pubDate>Fri, 15 Aug 2008 03:46:26 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265730</link><description>&lt;p&gt;Sigh, another discussion about good/bad code without prior definition what is meant by these attributes.&lt;/p&gt;&lt;p&gt;What is "bad" in your eyes may be "good" in mine and vice versa. It usually boils down to your cognitive setup (previous experience, preferences, priorities etc.).&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Muharem Hrnjadovic</dc:creator><pubDate>Fri, 15 Aug 2008 03:11:51 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265729</link><description>&lt;p&gt;There's been a discussion about #sum on the squeak-dev list recently. I don't like unary #sum, because it doesn't work with empty collections in the absence of either a powerful static type system or a sum protocol that has a "universal zero" (to coin a term). In dynamically-typed languages, #sum should instead be #sumFrom:, which is much more directly analogous to your fold. (Note how the appropriate zero is mentioned in the fold.)&lt;/p&gt;&lt;p&gt;myCollection.sumFrom(0);&lt;/p&gt;&lt;p&gt;Can Scala guess an appropriate zero, based on the type of the elements of the collection, for use with .sum()?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tony Garnock-Jones</dc:creator><pubDate>Fri, 15 Aug 2008 02:24:38 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265728</link><description>&lt;p&gt;I've talked quite a bit on what makes "good" code on my site, as well. Functional programming does make code extremely succinct, and yes there are "better" ways to do most code. The question this leaves me asking still is what is good code? Efficiency? Code Reuse? Readability?&lt;/p&gt;&lt;p&gt;I've found that if it "works" it must fit the basic definition of "good". While there is better ways to do most anything these days, good is often good enough.&lt;/p&gt;&lt;p&gt;I enjoyed your article, though. I think you're correct in what you're essentially saying... just because it's FP doesn't mean there's not more than one way to do it.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Collin Cusce</dc:creator><pubDate>Fri, 15 Aug 2008 00:28:47 -0000</pubDate></item><item><title>Re: Functional code != Good code</title><link>http://www.drmaciver.com/2008/08/functional-code-not-equal-good-code/#comment-6265727</link><description>&lt;p&gt;I've seen a fair bit of code which over-uses functions like fold and flatMap.  Heck, I've even *written* code like that!  I think the reason it exists is a lot of people using functional programming in any language (including Scala) are those who learned its precepts under the cruel lash of a more strict language.  Think about it, if you learned how to code in Haskell or even Lisp and then tried to learn Scala, you would end up producing some hideously functional code.  Now, this isn't as bad as it could be in languages like Java using its sorry-excuse-for-a-lambda, but it is still a problem (if nothing else, because non-trivial Scala is much more verbose than the equivalent Haskell or ML, partially due to the differences in type inference algorithms).&lt;/p&gt;&lt;p&gt;I think that your point is spot on: don't marry yourself to a particular methodology just because it solves *some* problems well (or even worse, just because it's "hip").  Even Scala's standard library adheres to this principle, implementing functional constructs (like everyone's favorite persistent collection: List) using imperative algorithms for efficiency.&lt;/p&gt;&lt;p&gt;While I think that it is *rare* that a trivial computation such as the ones you illustrated can be cleaner imperatively than in the functional style, there do exist many examples of a non-trivial nature for which this holds.  The beauty of Scala is that it doesn't restrict you to one or the other, letting you mix and match, getting the best of both worlds.  I suppose that sounds like PR, but it really does solve this problem of divergent methodologies quite nicely.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Daniel Spiewak</dc:creator><pubDate>Thu, 14 Aug 2008 23:49:25 -0000</pubDate></item></channel></rss>