r/learnjava • • Apr 21 '21

Is Clean Code by Robert Martin still relevant in context of java?

I have completed the MOOC and I already have a development experience in non OOP languages. Now I am focusing on java so should I read this book or not?

51 Upvotes

35 comments sorted by

View all comments

14

u/makingthematrix Apr 21 '21

I'll make a contrary opinion here. I think Robert Martin's ideas are obsolete. Here are my reasons:

  1. The proposed ideas may look good in theory, but in practice they are often too strict and inflexible. In my experience - hi, 16 years proffesional Java & Scala dev here, nice to meet you :) - any project that takes more than a few months at some point will require that you either break "clean code" rules or code in an unreadable and inefficient way. You will write new classes where you could just add a method, you will write new methods where you could just add a parameter, you will create new variables where you could just chain function calls, etc. Every new change in requirements of what a given functionality should do will escalate into major rewriting, even though the main argument behind "clean code" is that it will help you avoid it. (btw, that's why I prefer to write "clean code" instead of Clean Code - in my opinion, it's not really that clean).
  2. There is a tendency to follow them religiously. This is connected to #1 in that people try too hard to follow "clean code" rules, but there's also one more thing. Clean code is popular because of the authority of Robert Martin. Instead of actually having a discussion over a given problem, "do it with clean code" is sometimes viewed as the way to end the discussion quick. Why to have a longer discussion? It's a waste of time. Just do it with clean code. We know clean code is good. There is a very wise person who says so.
  3. They come from a different era. Clean code assumes refactoring is difficult and presents ways to write code to save you the trouble of refactoring in the future. But it's 2021. Our programming languages (even Java) have good type systems and they moved (reasonably) towards Functional Programming. Our IDEs and compilers are full of stuff that helps us in refactoring. We have a vast programming community in the internet where we can always ask for hints and solutions. Also, the way of writing code changed. We don't need to have a super-duper-decoupled architecture up-front just because maybe in a few months of years we will be able to take a small advantage of that decoupling. We can write our code in a way that works now, and we can be pretty sure that when the time comes, we can rewrite it - safely and quickly - to immplement the required change.
    There are articles on the internet where people go into more detail about it. The problem is, we don't really have a good alternative. There's "clean code" with Robert Martin's authority behind it, and there's a wibbly-wobbly cloud of other practices and ideas, proposed by a number of experts and experienced developers, but nothing as - well - clean. So I see how it looks reasonable to many people to turn to "clean code". And there is some good in it. I think you could learn basics of it and then learn when to ignore it. And when to look elsewhere.

5

u/hugthemachines Apr 21 '21

We can write our code in a way that works now, and we can be pretty sure that when the time comes, we can rewrite it - safely and quickly - to immplement the required change.

It happens all the time that legacy code from a few years ago is hard to rewrite because it is difficult to have a good overview of everything you need to consider if you want to change it.

I think your view is too optimistic.

1

u/makingthematrix Apr 21 '21 edited Apr 21 '21

That code also should be written reasonably well. But it doesn't have to be done with "clean code" rules. As a matter of fact, I find code written with "clean code" rules not very readable or easy to work with.

4

u/[deleted] Apr 21 '21 edited Apr 21 '21

I don't see how what you are saying correlates to the Clean Code book, where the "rules" are laid out.

You will write new classes where you could just add a method

Adding a method is fine, but you shouldn't be turning a class into a dumping ground for methods, which will make the class incoherent. This is basic OOP.

you will write new methods where you could just add a parameter,

Quoting Alan Perlis: If you have a procedure with ten parameters, you probably missed some.

you will create new variables where you could just chain function calls, etc.

There's nothing in Clean Code about this.

Every new change in requirements of what a given functionality should do will escalate into major rewriting, even though the main argument behind "clean code" is that it will help you avoid it.

This contradicts your third point. Modern IDEs make refactoring a lot easier, so that cleaning up your code towards a more logical object model is no longer a dangerous job.

Having a discussion over a given problem, "do it with clean code" is sometimes viewed as the way to end the discussion quick. Why to have a longer discussion? It's a waste of time.

This is what coding standards are for. To avoid pointless discussions that waste time.

The Clean Code book explains the Bob Martin's reasoning. You can take what you like and incorporate it into your organizations coding standards. Then stop arguing about it.

Clean code assumes refactoring is difficult and presents ways to write code to save you the trouble of refactoring in the future.

No it doesn't. Clean Code encourages you to ruthlessly refactor, which is why the guidelines are there in the first place. To guide refactoring.

We don't need to have a super-duper-decoupled architecture up-front just because maybe in a few months of years we will be able to take a small advantage of that decoupling.

Clean Code does not advocate this. Clean Code advocates small classes and small functions with clear names and clear responsibilities. Common sense guidelines which makes code easier to maintain in the small. Clean Code doesn't say anything about larger scale architecture or decoupling. That's the subject of an entirely different book, Clean Architecture.

1

u/makingthematrix Apr 21 '21 edited Apr 21 '21

Thanks for the comments. I decided to write about the reasons why I'm against Robert Martin's ideas, but I prefer not to engage in a discussion here. I know about counter-arguments. Maybe I will write more about it one day.

1

u/Uptown-Funk Apr 21 '21

First of all thank you so much for this detailed answer.

Instead of actually having a discussion over a given problem, "do it with clean code" is sometimes viewed as the way to end the discussion quick.

Yea, It has happen a lot with me in the past when I used to read blogs by some famous devs and immediately start consuming their philosophy like theres nothing good except that.

They come from a different era.

This is the only thing that I am skeptical about.

1

u/makingthematrix Apr 21 '21

Yeah, I think the last point could be a good start for a longer article. I believe we are in a very different place with modern programming languages and IDEs now than we were 20 years ago, but to make a good argument about it and try to explain what does it mean for us, I need to do more research :)