Live data from Hacker News

“Clean code” isn't actually clean

sdegutis.com

41–50 of 54 posts

Re: “Clean code” isn't actually clean

#41
I don't understand the negativity in the top voted comments here! It's almost as though I read a completely different article than the one being commented on.

The comments here are being dismissive and then reiterating what the post said! Here's the summary from the post :

> So I stopped worrying about whether my code is perfect. And I just accepted that if I can't see any immediate flaws with the code, and if all the tests pass (whether automated or manual), then it's fine. And I trusted that if I ever come across a bug, I can fix it.

Why, specifically, is that a bad thing?

Re: “Clean code” isn't actually clean

#42

Most guru's take an 'all or nothing' approach, this goes beyond programming, look at food-guru's "eat only carbs, never eat refined sugar" etc. This is for me the same as "always do TDD, never use static methods". People without experience often look at guru's because they can not yet think for themselves. I'm a developer in my 40s, have done a lot of projects in a lot of different companies. Having this experience g…

Fully agree. But to gain experience and intuition, sometimes you have to work close to one extreme, and the other extreme, to know in what situation the middle ground is. And to get a real feel what the benefits and drawbacks are.

Everything is always a trade-off. And it's the situation that can tell you which trade-offs you are prepared to make.

I like the phrase that your code, tests, etc, need to be "good enough". Because "good enough" is, you know, good enough. That way you can spend time on what matters the most.

Re: “Clean code” isn't actually clean

#43
post #23
post #15

This just seems like poor justification for laziness. You need to keep a project clean. This means profound refactors when concepts for the product change. Some rules are: - Keep it readable - Don't over engineering - Easy to remove - Good tests (complete, min stubbing, interface oriented. Never refactor and change tests at the same time. A lot of "veterans" are just resented with new paradigms and too egotistical to…

I experienced while writing a lot of tests, the code suddenly became - more readable - less over-engineered - replaceable - easier to maintain since it's easier to write tests for "clean code".

Exactly, even if you don't write tests for a component, it pays off to think about how it could be made testable. You can still choose to add tests later on.

Re: “Clean code” isn't actually clean

#44
post #9

Earlier quoted context omitted.

Replace `least amount of code` with `least amount of complexity` and I completely agree!

This has been my "go-to" definition for a while, though I'd add a caveat: complexity can be subjective, based on the reader's particular experience or background.

Whenever I use FRP, I'm worried that the code is super maintainable and readable for me, but a hell for a less experienced coder.

Re: “Clean code” isn't actually clean

#45
post #15

This just seems like poor justification for laziness. You need to keep a project clean. This means profound refactors when concepts for the product change. Some rules are: - Keep it readable - Don't over engineering - Easy to remove - Good tests (complete, min stubbing, interface oriented. Never refactor and change tests at the same time. A lot of "veterans" are just resented with new paradigms and too egotistical to…

> A lot of "veterans" are just resented with new paradigms and too egotistical to stay relevant.

Sometimes, your new paradigm is actually something that they tried 10 years ago.

> In the end, you can sum up all rules into one: Make it easy to maintain

That is only one of the many facets of programming that you have to balance: performance, memory consumption, ... .

If you want to push your system performance to the extreme, believe me, "easy to maintain" is not going to happen. But that is the trade-off that you will have to make at that point.

Veterans know that everything is a trade-off.

Let me give you one extreme: you are writing a throw away prototype. How much time and effort are you going to spend (=waste) on making your code easy to maintain?

I'll give you my veteran answer:

- If you are in 100% control of the project, you know you will throw away the prototype, and you can throw things quickly together.

- If you have a manager, he will take 1 look at the prototype and say "Wow, it's almost complete! Don't start a new project, just add these features to your prototype and it's done.". So in that case, make the "throw away prototype" easy to maintain.

That is what veterans bring to the table: making trade-offs in specific situations. And they've been through a lot of situations.

Re: “Clean code” isn't actually clean

#46
post #10

On my side projects at home, I can't even remember the last time I had to slow down or got stuck on maintenance/integration type stuff (as opposed to new functionality or algorithms, which don't rely on existing code). And I have a couple of fairly big ones, it's not all "Todo list" stuff. It's happened constantly at work though, throughout multiple jobs. Sometimes I've been so stuck I've got basically nothing done f…

I like NPM Lego a lot, especially if the legos are tiny and (battle-)tested.

At the same time if I can replicate the gist of lodash.omit in 5 lines I will.

Re: “Clean code” isn't actually clean

#47

Earlier quoted context omitted.

Do you have examples of things you learned on the courses that you thought made your code more difficult to understand and modify? After that, did you completely switch back to the code style you were using before the courses? Did you cherry pick some things and not others? Just to make my view plain, for me 'clean code' was the most important book I've read in my career, and what I learned from it has (in my view) m…

I agree, Clean Code was the most important book for me too. But I wouldn't follow it blindly. Also, it's less useful for non-Java and much less useful for non-OOP languages (it's ok to have a focus, but one has to keep in mind that sometimes, certain idioms, best practices or design patterns are actually workarounds around the limitations of Java). My favorite example: Overdoing the short functions thing. I've seen t…

I think the short functions thing is a funny one. The benefits of short functions are definitely praised in the book, but they are really more of a side effect of the real goal: to have pieces of logic that do one single thing that can be summed up in a function name and understood as a clear input -> output 'step' at the level of abstraction above, without having to dig in to find out 'how it works' or understand any weird quirks / unexpected side effects etc.

Usually, by their nature, such functions don't end up being very long. This is great, but it remains just a nice side effect of the real goal, and one that is not more important than the real goal. To artificially break up a slightly longer function which is very focused in its behaviour, just for the sake of line count, is probably a mistake.

To be honest, I can't remember how clearly the book makes this distinction, or if it does at all, but I feel like it probably could be made clearer given how common this misinterpretation is.

Re: “Clean code” isn't actually clean

#48
Sometimes the warts are just warts and mistakes, sometimes there is a deep undocumented reason for it. Which indicates a problem as reasons like that should be explicitly documented.

And yes, often clean code actually is finished. Mess is often produced by wrong architecture or rushed development which means not finished if maintainability is in your definition of code being done. What the author probably meant is concise code instead.

Re: “Clean code” isn't actually clean

#49
post #15

This just seems like poor justification for laziness. You need to keep a project clean. This means profound refactors when concepts for the product change. Some rules are: - Keep it readable - Don't over engineering - Easy to remove - Good tests (complete, min stubbing, interface oriented. Never refactor and change tests at the same time. A lot of "veterans" are just resented with new paradigms and too egotistical to…

I'm 33, and while I don't think I'm "at the top of my field", I have experienced quite a bit and am confident that I can both produce and identify high-quality code.

In my mind, the biggest change as I've grown in my career is that I've gone from judging code quality to judging code suitability.

For example - the application I'm working on right now, there is is a javascript file that initializes complex data tables using the jQuery DataTables plugin. It's several hundred lines of redundant code, with several functions that actually do the initialization differently depending on the classes that are applied to the table element. It is unquestionably "low quality" code, but I have no intention of attempting to refactor it. Why? Because there are customers whose UIs are dependent upon the bugs and undocumented assumptions that are baked into that code, and refactoring it would break things from their perspective. If I did that, by the time I dealt with all the bug reports and feature requests from customers, the nice clean code that I'd written would look like the code I have now. Instead, I've written a new JavaScript file to initialize data tables going forward, and all new instances use that. They are slightly visually distinct from the "old-style" versions so users know to expect the slightly different (but now consistent) behavior. I'm very resistant to adding features to the legacy code, and instead, offer to "convert" the legacy tables to the new layout one-by-one when a new feature is requested. Eventually, we may reach a point where we can retire the legacy code - but in all likelihood, that code will be there longer than I will work here. This approach of "walling off" code that has become unwieldy and difficult to modify is one of the things that I look for when determining where a developer is in their career.

> You need to keep a project clean. This means profound refactors when concepts for the product change.

If the purpose of a company were to produce the most excellent software possible, I'd agree with you, but the purpose of a company is to make a profit. If you do a "profound refactor" every time requirements significantly change you're likely to never launch a product at all, much less iterate quickly enough to build a profitable product. You have to learn to deal with cruft and manage its lifecycle, not try futilely to prevent it from ever occurring. Part of that is learning to break things down into discrete components and limit interdependencies so you can refactor each component in isolation, but another part is learning to segregate cruft that has accumulated and keep shipping without creating a mess that slows down how quickly you can iterate in the future.

> It takes effort to maintain a clean project, many will try to say "it's not worth it".

"Worth it" doesn't necessarily mean "worth it from the perspective of the developer". It can also mean "doing this right is going to take longer, and in order to meet our business goals we can't take the time". As long as the long-term impact of these decisions are passed on to the decision-makers on the business side, there are absolutely times when "it's not worth it" to write clean code.

> That's just out of the question, it's your job, it doesn't have to be always pleasant or easy.

Nope. Your job as a developer is to provide more business value than you consume. If you're getting paid $100k you must provide more than $100k of value or you will eventually be out a job, regardless of how clean your code is.

Re: “Clean code” isn't actually clean

#50
post #26
post #22

Earlier quoted context omitted.

This seems to couch the idea that older professions finally "understand it." Look at the houses people made 40 or so years ago. We now have to have disclaimers indicating that the materials they used were... not so wise to be used. Go back further, suddenly you will be left with basically nothing but survivor bias. It will seem like they had it together, but it is just as likely they did not. Pull it in some and you…

Sure, but we've been building houses for a long time. We know a lot more about building houses today (or even 40 years ago) than they did when the practice of house-building was still relatively new. And most of this knowledge didn't come from building houses over and over, but from developing better theories of how buildings stay up, using better materials, etc....

I think we'd both be surprised on how much actually does come from "building houses over and over." My assertion being that much of material science advancement came from required advancements from previous failed houses.
Post reply on HN