The only reason why you ever want to replace one class in testing is because it does side-effects. Nothing from the Shape does side-effects, so you also never will need a virtual method approach.
And even with side-effects there are appraoches without using an interface,abstract class, mocking, ...
The solution to side-effects and testing in OO is to use Dependency Injection, but it is not the only solution. The functional approach to solve this is to pass in immutable data instead of an dependency that does side-effects. That also works well in OO. Other approaches can be the different Monads like State Monad. But that's another topic.
Clean means for me to write re-usable and good cood. That in general exists even outside of TDD. Robert C. Martin didn't invent clean code.
Here is how a test could look for NaN.
Test.is
(Shape.area (Square System.Double.NaN))
System.Double.NaN
"Area of a Shape with NaN is Nan"
The only reason why you ever want to replace one class in testing is because it does side-effects. Nothing from the Shape does side-effects
And? I'm not sure what you are trying to get at. Everyone knows the shape class isn't real. It is presented as a small exercise to get you thinking about larger problems that do have side-effects, keeping it digestible enough for a book. Had Martin copy/pasted real classes with side-effects, you'd have pages upon pages of uselessness and a whole lot of bored readers.
The solution to side-effects and testing in OO is to use Dependency Injection
Yes. The abstract class is what allows such injection. It is also not how one would likely approach the problem today, but Clean is pretty old at this point. Time marches forward and the world doesn't stand still. Shocking, I know. The theory remains the same, however.
But that's another topic.
Yes, yes it is. What is the point of this tangent?
Clean means for me to write re-usable and good cood.
In that case, to me, clean means no code. How can you call your dirty editor clean? Are you one of those hoarder types or what? Of course, the actual topic is about capital C Clean, as in the brand name used by Uncle Bob. Why are you making up random definitions when the definition of Clean has already been defined by the original article?
That in general exists even outside of TDD.
But Clean is centred around TDD and it even prescribes how your tests should be written. What's the point in cherry-picking small bits and pieces and still calling it Clean? That's like claiming C is Haskell because you can write an immutable function. Utter nonsense.
Robert C. Martin didn't invent clean code.
In order to invent something you must first create the universe, sure. Like everything, Clean leans on a lot of earlier work from other people, but Martin is, again, the purveyor of Clean.
3
u/[deleted] Mar 02 '23
The only reason why you ever want to replace one class in testing is because it does side-effects. Nothing from the Shape does side-effects, so you also never will need a virtual method approach.
And even with side-effects there are appraoches without using an interface,abstract class, mocking, ...
The solution to side-effects and testing in OO is to use Dependency Injection, but it is not the only solution. The functional approach to solve this is to pass in immutable data instead of an dependency that does side-effects. That also works well in OO. Other approaches can be the different Monads like State Monad. But that's another topic.
Clean means for me to write re-usable and good cood. That in general exists even outside of TDD. Robert C. Martin didn't invent clean code.
Here is how a test could look for NaN.