Hacker News new | ask | show | jobs
by _gabe_ 1029 days ago
There’s so much conflicting advice in this book (like saying functions should be immutable then also saying the ideal function has 0 arguments, apparently mutating the class doesn’t actually make the function mutable to Bob Martin!), and the code examples themselves are horrible (the prime number generator for example).

I’ve heard people say it’s a good book if you just ignore all the bad stuff, but how are you supposed to know what the bad stuff is if you’re a beginner? I think it’s time to stop recommending this.

1 comments

> like saying functions should be immutable then also saying the ideal function has 0 arguments, apparently mutating the class doesn’t actually make the function mutable to Bob Martin!

What does "functions should be immutable" have to do with mutating classes?

I should have been more specific, the way Bob Martin phrases it in the book is functions should have 0 side effects. Then he shows an example at the end of the chapter where the entire example works by creating a giant class full of member functions that mutate the owning class. I (and I think most people) assume that a function with 0 side effects means that if you call the function, the state of the object should be the same before and after the function call (and the rest of the external system should remain unchanged). But, according to the examples in the book, it seems like Bob Martin only considers it a side effect if some external state is modified as a result of the function call.

The best example of a function without side effects would be sin(x). You call the function with an input and it returns a completely new output. The function should be thread safe and easy to isolate because it never touches any outside state.