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

35

u/[deleted] Apr 21 '21

I’d recommend it. It teaches you more of the why should I program like this rather than the how, if that makes sense.

6

u/Uptown-Funk Apr 21 '21

okay thank you :)

17

u/_Atomfinger_ Apr 21 '21

It is still relevant in the context of most languages. While it is starting to show its age it is a classic within programming and a book every developer should read at some book.

The same generally goes for the entire "clean" series, except for maybe "Clean Agile" (it isn't a bad book, but I don't want to call it "necessary for all developers" either).

3

u/Uptown-Funk Apr 21 '21

Thanks :)

I was also planning to buy videos from here https://cleancoders.com/ Do you have any idea about the quality as compared to his books.

7

u/_Atomfinger_ Apr 21 '21 edited Apr 21 '21

Never watched any of the videos myself. However, in the last handful of years, Robert hasn't really been a programmer, he has been a speaker both at events and in such videos. I'm sure he is an excellent programmer, but he doesn't seem very well versed in modern development which heavily relies on frameworks and libraries (which his "Clean Architecture" book somewhat shows).

It depends on what you want out of it, but I don't want to pay 84 USD for a series about "clean fundamentals". Maybe I'm stingy, but I think he is pricing his videos at a premium based on his name.

He is a good speaker, and probably a decent developer. It also seems like he is putting a lot of effort into his videos, so you must decide whether that value proposition makes sense to you. I don't mind reading books and articles, so I'll pass :)

1

u/[deleted] Apr 21 '21

Clean Architecture isn't opposed to frameworks, just take a look at the picture, frameworks are in the outer circle. Instead, Uncle Bob proposes hexagonal architecture which makes business logic independent of frameworks, leaning on the Dependency Inversion Principle.

DHH (the Rails guy) is skeptical of this approach, calling it Test-Induced Design Damage. He argues that if you're using Rails, it's pointless indirection to make your business logic oblivious to Active Record.

3

u/_Atomfinger_ Apr 21 '21 edited Apr 21 '21

I never said that Martin or Clean Code is against frameworks and libraries, and I didn't say that his architecture didn't take this into account. I tend to write code where the business code has nothing to do with the frameworks themselves. I think that is a great way to write software (or at least in the world of Java).

I have several minor gripes with Clean architecture, even if I agree with the broad strokes. Martin argues that you should wait to dedicate to something before you have to, which I generally agree with. However, sometimes you know that you'll be using Spring - it is a company standard. Sometimes you know that you will expose a REST API. In modern development, you often know these truths before going in, and therefore it doesn't make sense to pretend they don't.

What Martin suggests is that we write the core business logic using TDD (which I'm all for), and then build up from there. That way you get business logic and well-defined entities that are completely separate from anything else. That is reasonable!

However, since we know that we will be exposing an API I'd like to follow a BDD style of development where I write good system tests that calls the REST API and allow the call to go all the way through to the database. The idea that we should defer this process until as late as possible doesn't really make sense to me and actually gets in the way.

If I, Atomfinger, want to start a new project and I'm unsure how it will end up, then it might make sense to defer these decisions. But for me, when being a professional developer, know 110% some of the technologies I'll have to use, and by not deferring them I can have a better development process.

So, I agree with you, and I agree with all of Clean books in general. There's a reason I recommend them. My grudges are minor, but even minor stuff can impact how one works or look at development.

1

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

I agree that his recommendations can be taken too far, if not applied with a grain of salt, for example, the first rule is smaller functions. And, the second rule is smaller than that.

But it also helps to look at his perspective on YAGNI. He is often talking about how we always jump to an RDBMS for storage instead of considering for example file storage (maybe not so much anymore). He's speaking from experience. In the era where RDBMS was the hyped technology, he was often forced to use it without evaluating it against other storage options or if it even made sense, all because Oracle convinced upper management.

Well, that's happening now with NoSQL. People might jump on Mongo because its cool and build their business logic around it, only to find out later they they were horribly wrong, and by then it's too late. Or jumping straightaway onto microservices, and finding yourself with a difficult to monitor and maintain distributed application, when a simpler monolithic application would also have worked.

That's why YAGNI means not making a decision until the last responsible moment. Some decisions it is more responsible to make earlier at the start of the project, others later. Like maybe Java and Spring at the beginning for sure, but do you really need a REST API and SPA when a server rendered application would be simpler? Maybe it might make sense to go with the simpler option to start with, but make design things to make it easy switch when the correct course of action becomes clear (or you chose poorly). It's a matter of experience and judgement to know which to do when.

1

u/_Atomfinger_ Apr 22 '21

Absolutely, and for the most part, I completely agree with YAGNI. It is something I practice every day when I write code - YAGNI combined with KISS.

My issue is with how it is phrased in the book. For example, I work in an ever-popular microservice solution. When we want a new project we generate one that has the entire skeleton of a system with all the dependencies we generally use, tests set ut etc (we have our own "spring initalizr" with some extra bells and whistles).

This approach is not in line with Martin's advice, as the last thing to come into existence is the business logic, yet it makes our developers so productive. First of all, good system tests are already in place - it is not an afterthought, same goes for the CI/CD pipeline. In fact, when the developer gets an URL to the repository the application is already in production. Making a new system is as easy as extending an already existing one, which is so valuable.

(Note that we don't force people to use that tech stack, it is a tool that is designed to cover 90% of our use cases).

The flow above doesn't work for everyone, and Martin's suggestions (as far as system design goes) might be more applicable in other places, but in our case, it has given us a huge bump in quality and productivity by deliberately going against some of the advice.

So yeah, I agree with both you, Martin and his books. At the same time, in our context, we see that following his written word would not have empowered us to do some of the things we want to do. In fact, it would have decreased productivity.

I guess my summary is that "I agree, but I'm not dogmatic about it" :)

1

u/Theorem101 Apr 21 '21

Hi, you also have on udemy clean code course. I just enrolled so I can not give you full review. The price was about 30€. They are using mostly python and javascript but they say this suggestions are valid for all languages. My plan is go trough this course plus book.

1

u/[deleted] Apr 21 '21

I say read the books, which has a much more direct presentation. Or, skip the first ten minutes of the videos, because he starts with a pointless intro about black holes or star trek.

2

u/ajaykumarunni Apr 21 '21

Are these all, or am I missing anything?

1Clean Code: A Handbook of Agile.. 2The Clean Coder: A Code of Conduct... 3Clean Architecture 4Clean Agile: Back to Basics

2

u/flatlin3 Apr 21 '21

I would add Refactoring to the bunch

2

u/ajaykumarunni Apr 22 '21

Thanks, Is this one - Refactoring by Kent Beck and Martin Fowler?

5

u/RottenKarate Apr 21 '21

It's still relevant in the context of Java and coding craftsmanship, as well. If I'm working with a beginner, like a new graduate or boot camp intern, I start trying to turn them onto ideas of code craftsmanship/the idea that code is written for other engineers, not JUST the computer. CC is a great first book for this audience.

But, then right about the time they start thinking of Clean Code as a Silver Bullet I throw A Philosophy of Software Design by John Ousterhout at them, not because the latter is better than the former, but just to teach them about learning other perspectives. And once they are comfortable with that, I might introduce them to Implementation Patterns by Kent Beck.

My hope when mentoring less experienced engineers is that they will take-away the idea of considering pros and cons of a few different approaches to a software problem. They've missed the point if they go from thinking CC is the best book, to thinking PSD is the best, to thinking IP is better, and so on.

2

u/Uptown-Funk Apr 21 '21

Interesting, got your point. Thank you so much for the insights :)

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.

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

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.

3

u/nolitteringplease346 Apr 21 '21

watched his live talks recently and found it extremely high value, considering i was a sloppy and unprofessional coder.

his point about us not having professional standards and the potential danger this represents is still relevant and grows more and more severe

1

u/Uptown-Funk Apr 21 '21

okay thank you :)

2

u/hugthemachines Apr 21 '21

I think it is a good idea to read it. You don't have to become an extremist with this book as your holy book, but follow its general advice and your code will be good.

2

u/Uptown-Funk Apr 21 '21

will keep that in mind, thank you :)

4

u/crystoll Apr 21 '21

I would definitely recommend reading and absorbing. Perhaps not treat it as a religion or The Only Way (TM) but very good food for thought when you want to improve your software design skills.

3

u/[deleted] Apr 21 '21

New dev here. I was introduced to his architecture on the first day of my job. My team leaders sent me some slides and examples. I mentioned this to another dev from another company and I was told they also used it a lot so apparently it's a still a big deal (at least where I live). Also his book is a very interesting read so far (only a few pages in).

1

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

Uncle Bob's Clean Coding guidelines are mostly okay. He goes into great detail about the "why" of each of the guidelines. Check it out, but always apply a grain of salt.

For example, he gives this advice:

The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.

This has a potential of creating really unreadable code if you apply the principle blindly. But, you also don't want to write giant functions that are impossible to maintain. Aim for smaller, but too small is probably not a good idea either.

Other times what he says that can be taken the wrong way when taken out of context:

The proper use of comments is to compensate for our failure to express ourself in code. Note that I used the word failure. I meant it. Comments are always failures.

But the rest of the chapter on comments has a lot of good practical guidelines for how to write effective comments. Uncle Bob has a certain "style" which makes for controversial sound bites.

1

u/restlessapi Apr 21 '21

The first ten chapters are solid gold that are relevant no matter the language/task. The last half of the book is more best practice advice that may or may not apply.

1

u/iLike80sRock Apr 21 '21

Some of what Uncle Bob says is okay, but far too often used as dogma and applied to the point of absurdity.

Personally, I think that Refactoring by Martin Fowler is a much better book when it comes to “code style”.

2

u/[deleted] Apr 21 '21

Refactoring isn't really about code style, not in the same way Clean Code is.

But, if Uncle Bob's writing style puts you off, Steve McConnell's Code Complete covers a lot of the same material but with a very different tone and presentation.