Live data from Hacker News

My takeaways from "Clean Code"

medium.com

51–60 of 60 posts

Re: My takeaways from "Clean Code"

#51
post #3

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 shoul…

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?

Re: My takeaways from "Clean Code"

#52

> Zero or one argument is easiest to understand and maintain. > Have No Side Effects Can someone explain how you use zero argument functions that doesn't have side effects? I am trying to wrap my head around these two statements.

I think it is all suggestions, not hard rules. Also, given that the quote allows you to use up to 1 argument, it is actually trivial to do (assuming you are fine with the no side effects part). The trick is that instead of taking 2 parameters, you take 1 parameter, than generate and return a function that takes 1 parameter. Probably not what the author has in mind. Anyway, in the context of the rest of the post, it l…

Or just make the function take two arguments. And then use currying (if your language supports it). A lot of this depends on language.

I definitely agree with you saying its suggestions, not hard rules. Unfortunately the author of the page keeps saying 'always' as if it's hard and fast rules.

Understand why and when first, then use where appropriate.

Re: My takeaways from "Clean Code"

#53

> Zero or one argument is easiest to understand and maintain. > Have No Side Effects Can someone explain how you use zero argument functions that doesn't have side effects? I am trying to wrap my head around these two statements.

A method that acts as a "getter" by querying some sort of state wouldn't take arguments and shouldn't have side effects.

Re: My takeaways from "Clean Code"

#54

Earlier quoted context omitted.

> 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, meaningf…

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” actu…

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"

#55
post #45
post #5

Earlier quoted context omitted.

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.

I tend to comment working around bugs in third party code.

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"

#56
post #23

Earlier quoted context omitted.

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 programmin…

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!

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"

#57

Earlier quoted context omitted.

I think it is all suggestions, not hard rules. Also, given that the quote allows you to use up to 1 argument, it is actually trivial to do (assuming you are fine with the no side effects part). The trick is that instead of taking 2 parameters, you take 1 parameter, than generate and return a function that takes 1 parameter. Probably not what the author has in mind. Anyway, in the context of the rest of the post, it l…

Or just make the function take two arguments. And then use currying (if your language supports it). A lot of this depends on language. I definitely agree with you saying its suggestions, not hard rules. Unfortunately the author of the page keeps saying 'always' as if it's hard and fast rules. Understand why and when first, then use where appropriate.

>Just make the function take two arguments. And then use currying.

Isn't that what I said?

Re: My takeaways from "Clean Code"

#58
post #55
post #45

Earlier quoted context omitted.

I tend to comment working around bugs in third party code.

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.

But you still need to comment about why you wrapped the code in the first place.

Re: My takeaways from "Clean Code"

#59
post #54

Earlier quoted context omitted.

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” actu…

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 f…

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"

#60
post #31
post #16

Earlier quoted context omitted.

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)…

> 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 Woodwor…

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.

Post reply on HN