News for hackers. Stuff that matters.
Re:My takeaways from "Clean Code" by mion on Tuesday May 28, 2013 @08:09PM (#5783721)
Nice article! But I have to disagree with these:

- Minimize the number of arguments

- Avoid output arguments

Basically, you're arguing against some principles of functional programming, advocating the use of state. There's no good or wrong with state, I think, there's only a trade-off. Heavy use of state = easier/faster to code, harder to debug/read.

IMHO these kinds of generalizations are pretty dangerous. We should always try to understand the problems lying underneath in order to correctly assess the trade-offs. A quote from Elon Musk that goes something like "don't rely on analogies, try to understand the problem" popped up on HN some other day, but I'm too lazy to go look for it now. :-)

Re:My takeaways from "Clean Code" by halostatue on Tuesday May 28, 2013 @08:24PM (#5783795)
This won’t win me any friends, but I personally think that Clean Code is one of the most dangerous programming books to be released in the last decade.

There’s a lot of good advice in the book, but it is written by and for Java programmers, and it comes with its share of risks if you take its advice. If you’re developing in a dynamically typed OO language like Ruby, it’s almost always bad advice to follow Clean Code. There are performance considerations even with Java to following some of the advice given. With Objective C, the suggestions are just silly. With languages that emphasize some level of functional programming (including Ruby and even C# to a degree), the suggestions are counter-productive and produce code that is unnecessarily obfuscatory.

Yes, it’s good to have short, readable, meaningful methods. Reducing method size too much, though, can result in code that you have to follow through many objects in order to understand the logic. If you follow Martin’s advice enough, you will end up with lots of very short methods on lots of very small objects—and have a hard time understanding just how your program fits together.

If Martin were a better writer, or had expanded his horizons beyond Java when he had written this book, he may have written something profound—still dangerous, but profound. Instead, he wrote Clean Java and either he or his publisher thought it would sell more as Clean Code. My advice is to ignore the book—unless Martin comes out with a second edition that takes what he has learned in the last few years since leaving behind Java and applies it to really make a book that describes clean code.

Re:My takeaways from "Clean Code" by jasonlotito on Tuesday May 28, 2013 @08:58PM (#5783944)
> If you follow Martin’s advice enough, you will end up with lots of very short methods on lots of very small objects—and have a hard time understanding just how your program fits together.

I have never found this to be the case. Quite the contrary, in fact. Far too many objects that do way to much, or functions that do X and Y.

Indeed, I'm puzzled by your statement:

> Yes, it’s good to have short, readable, meaningful methods. Reducing method size too much...

Which directly contracts this:

> If you follow Martin’s advice

I guess the key word is 'enough', which is really just bad wording. "If you don't follow Martin's advice" is better. The qualifier here is you don't make short methods for the sake of having short methods. You make methods as short as they need to be to do that thing they promise to do.

So then we come to it: either you support having your methods do more than they should, doing more than one thing and having side effects, or you support Martin's suggestions. And yes, it really is that black and white. Sure, you could suggest that following his advice "enough" leads you to nothing but one line methods, but that would be silly.

Re:My takeaways from "Clean Code" by halostatue on Tuesday May 28, 2013 @09:28PM (#5784047)
No, it isn’t that black and white. Many of Martin’s suggestions are silly, especially when applied to languages that are more expressive (like Ruby, CoffeeScript) or otherwise have no relationship to Java.

I distinctly remember arguments with people who were reading Clean Code at the time who really believed that one-line methods really were the ideal.

The trick is knowing what does “doing more than they should” actually mean? If you follow Clean Code as I’ve seen some people follow it (including the Ruby dev shop I mentioned elsecomment), you will end up having substantially larger interfaces at the cost of understanding just what it is that your program is supposed to do in the first place.

I’ve dealt with programs written the Clean Code way (an explosion of methods, classes, and objects), as well as programs written with too much going on in one place (e.g., 2+kloc functions). Neither is fun to deal with, but it’s generally easier to assimilate the basic business purpose of the 2+kloc function than something that has a larger interface surface than it needs to have.

Re:My takeaways from "Clean Code" by joshka on Wednesday May 29, 2013 @09:13AM (#5786045)
Your assertions are interesting. I'd like to see some concrete examples of where you've seen this actually play out to make not well understood code.

Having recently read Clean Code as a C# dev, I feel the missing piece in this conversation is that the advice of small methods needs to be taken together with the idea that things should do one thing, and should be composed of items at the same level of abstraction. I feel the idea is hard to defend on its own, but with the other ideas quite easily defended.

I'm also curious about the performance problems you've mentioned above.

Re:My takeaways from "Clean Code" by halostatue on Wednesday May 29, 2013 @11:35PM (#5790756)
I’ll address the performance problems first. Following the advice of Clean Code, you can exponentially increase the size of your object model and the number of methods on those objects without even trying. This can cause memory pressure and increased dispatch times. If you're incautious and don't pay attention to your object creation/destruction cycle in a tight loop, you're in for a world of hurt (no surprise there). The (potentially much) larger object model of many little things that do Just One Thing makes it harder to effectively reason about the system and understand how those object allocation/deallocation cycles can be killing your performance. This is precisely the sort of thinking that leads to FactoryFactoryFactories: some things can't meaningfully be broken down without losing the point of the abstraction.

The problems with Clean Code are legion. It makes axiomatic arguments and then drives them further in the name of purity—which is anathema to shipping code, clean or otherwise. It is deeply rooted in the Java available in 2007–2008 and shows no awareness of just how broken Java was as a programming language at the time in comparison with many other languages. It makes prescriptions of things that are merely good advice.

By all means, simplify your code. That does not mean making lots of little objects and lots of little methods. It means that your code should be as simple as possible and no simpler. Sometimes this means that you’re going to have methods that are a few dozen lines long, because splitting them apart increases the state you need to carry around between those methods. Sometimes it means taking advantage of features in your language that Clean Code (being an Old Java book, despite its grandiose title) exhibits no awareness of (even though, with generics, it could).

It’s been a few years since did this, but to introduce the C# developers at a previous job to the ideas provided by C#’s functional constructs, I wrote a particular piece of code four or five times and wrote a substantial email about it describing the simplifications that the constructs provided at each step of the way, and ended it with the equivalent Ruby code, which was more or less:

   return container.any? { |e|
     e.subcontainer.any? { |f|
       f.boolean and f.subcontainer.any? { |g|
         g.boolean
       }
     }
   }
Written iteratively, it took about 50 lines to achieve this, and required state to know when you had broken out of a particular loop (#any? is short-circuiting; as soon as anything matches, you’re clear). Written with several smaller methods each acting iteratively, it also took about 50 (you lose state tracking because you can break out with a return from each function), and you lose readability at the call-site (you now have to jump to a different function for each item to understand the logic). Written with named delegates, it took about 40, but was harder to understand since the logic was separated from the call-site. Written with anonymous delegates in C#, it took about 30—and was fairly understandable (readily understandable if you already understand anonymous delegates). The equivalent Ruby code was a third of that. The most readable code kept the logic at the call-site and used powerful features in the language; the next-most readable code was the iterative code.

The above code clearly violates the rules of Clean Code; your method at object level D ('container') knows something about objects at level F and G, but making (and naming) methods at levels D, E, F, and G to return truth when you do not need those methods anywhere else? Foolish inconsistency, and Clean Code is full of examples like that where, given a better language (or even a better use of the Java language and generics), you don’t need or want to do those things.

Clean Code is a dangerous book because it takes generally good ideas and applies them as Truth, and many of the people who should be reading and learning from a book like Clean Code simply don’t know any better and fall into even worse habits than if they had never read the book. Given a choice between following complex business logic across lots of little classes and files and a few larger functions, I know which I would choose.

Re:My takeaways from "Clean Code" by paragraft on Tuesday May 28, 2013 @08:42PM (#5783872)
I came here to say something similar, but you took the words out of my mouth. My experience reading through it was it was a mix of good advice with some questionable advice, some advice that was really only good for Java, and some advice that was terrible. The problem is it's pitched at an audience that may not be able to discern the differences between the above. The multiple authors (on a per-chapter basis) doesn't help.

Generally speaking, the earlier chapters were better, but my advice would echo yours: give it a miss, especially if you're not working in Java.

Re:My takeaways from "Clean Code" by rluhar on Tuesday May 28, 2013 @08:42PM (#5783867)
I agree with your premise that Clean Code is not a suitable book for all programmers / and or all languages. But a good amount of "enterprise" software is written in Java. And, in "enterprise land" (where I currently live), there are lots of mediocre, inexperienced or untrained programmers for whom the advice in Clean Code is useful.

I have had to try and clean up functions that were two thousand lines long (in Java), had lots of side effects, and took in seven parameters. Oh boy did I wish the developer had read Clean Code.

I agree with your premise that the book is not relevant to those who work in dynamic or functional programming languages. Frankly, I don't think they are not the intended audience. I think it is a little hyperbolic to call it the "one of the most dangerous programming books released in the last decade".

Re:My takeaways from "Clean Code" by derefr on Tuesday May 28, 2013 @09:32PM (#5784062)
> those who work in dynamic or functional programming languages ... are not the intended audience

I don't understand how a book that professes to be top-down advice about software craftsmanship as a discipline can be intended only for a specific subset of programmers. Shouldn't choosing a language be as much a part of software craftsmanship as how you use that language?

It's like writing a book entitled "Good Woodworking" which implicitly assumes you'll be using only double-clawed hammers[1]. Wouldn't you expect a book called "Good Woodworking" to start with a chapter on good tools?

[1] http://me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-de...

Re:My takeaways from "Clean Code" by halostatue on Wednesday May 29, 2013 @11:37PM (#5790767)
Which is why I’ve said (ever since I read it the first time) that Clean Code wouldn’t annoy me half as much if it were simply called Clean Java.

It wouldn’t sell as many copies that way, though. Probably.

Re:My takeaways from "Clean Code" by mion on Tuesday May 28, 2013 @08:14PM (#5783747)
And also this:

- Comments are fails

Sometimes you rewrite code for performance, making it fast but awful to understand. Comments are very useful in this case.

Re:My takeaways from "Clean Code" by bguthrie on Tuesday May 28, 2013 @08:21PM (#5783779)
Unless your performance optimizations specifically avoid method invocation, you can always extract the nasty bits to a well-named method. This is what is meant by comment avoidance: it is better to isolate the dirty stuff and pick good, specific names for what they do than to write an explanatory comment wherever possible.
Re:My takeaways from "Clean Code" by mion on Tuesday May 28, 2013 @08:45PM (#5783881)
Hmm, true. But a lot of performance optimizations (and the example I had in mind) avoid method invocation. Inside a critical loop, for instance, stacking function calls can make a big difference on a dynamic language. Macros could solve this problem though.
Re:My takeaways from "Clean Code" by mercurial on Wednesday May 29, 2013 @02:06AM (#5784869)
It's good practice to comment when necessary. Sometimes, even a "well named" method will not be expressive enough. Other times, you need to do something non-obvious (eg, work around a bug in another system). Comment when necessary.

I've also found that comment tend to be both easier to maintain (due to the smaller amount of code) and more necessary (due to the higher density of information) when using more powerful languages (eg, Haskell).

Re:My takeaways from "Clean Code" by spc476 on Wednesday May 29, 2013 @12:48AM (#5784648)
I tend to comment working around bugs in third party code.
Re:My takeaways from "Clean Code" by joshka on Wednesday May 29, 2013 @09:19AM (#5786080)
Another way would be to instead wrap the third party code (and hence isolate yourself against such bugs) and use the language of choice to highlight the design decision.
Re:My takeaways from "Clean Code" by spc476 on Wednesday May 29, 2013 @08:18PM (#5790170)
But you still need to comment about why you wrapped the code in the first place.
Re:My takeaways from "Clean Code" by wonderzombie on Tuesday May 28, 2013 @08:40PM (#5783856)
Basically, you're arguing against some principles of functional programming, advocating the use of state.

Is that true? "Minimize the number of arguments" nudges you towards datatypes or maps instead of lists of implicitly related variables. No state there, just keeping functions relatively simple in terms of their inputs.

In the latter case, "avoid output arguments" is absolutely a principle of functional programming; it says use arguments for input and return values for output. You can't do this in Haskell. I don't think you can do it in Clojure, either, or if you can, it's not really idiomatic b/c immutable. I can't speak to other Lisps with any certainty, but I've never seen it as an idiom.

Re:My takeaways from "Clean Code" by mion on Tuesday May 28, 2013 @08:55PM (#5783934)
No state there, just keeping functions relatively simple in terms of their inputs.

Yes, yes! I definitely agree but what I meant is "principles of functional programming outside functional languages". When you understand the trade-offs that these principles bring to the table, you can apply them in OO languages -- in some cases. But here he's banning it for good with that statement!

Re:My takeaways from "Clean Code" by wonderzombie on Wednesday May 29, 2013 @01:08PM (#5787570)
With which statement?

There is one which explicitly encourages side effects -- the one which says "write bar.doFoo() instead of doFoo(bar)" -- but I think it's orthogonal; you can keep your inputs and outputs simple without rewriting your code to have a lot of state.

Re:My takeaways from "Clean Code" by miloshadzic on Tuesday May 28, 2013 @08:23PM (#5783789)
This was my reflex as well but then I remembered that Clean Code serves mostly as advice to OO programmers. In OO programming you can consider an object's attributes to be implicit parameters to a function.
Re:My takeaways from "Clean Code" by retrogradeorbit on Wednesday May 29, 2013 @02:33AM (#5784953)
I agree. Especially with the 'always' part:

"Anyway, it is always a good programming advice to minimize the number of arguments. Zero or one argument is easiest to understand and maintain."

How is one to minimize the number or arguments in a purely functional language with immutable state? How can I do tail recursion without any arguments?