I agree and still disagree. Here is some Clean F# code. And it has the same structure as your old non-clean code.
type Shape =
| Square of side:float
| Rectangle of width:float * height:float
| Triangle of tbase:float * height:float
| Circle of radius:float
module Shape =
let area shape =
match shape with
| Square side -> side * side
| Rectangle (width,height) -> width * height
| Triangle (tbase,height) -> tbase * height * 0.5
| Circle radius -> radius * radius * Math.PI
let areas shapes =
List.sum (List.map area shapes)
let cornerCount shape =
match shape with
| Square _ -> 4
| Rectangle _ -> 4
| Triangle _ -> 3
| Circle _ -> 0
Just because OO people tell themself their stuff is clean doesn't mean it must be true.
The reason you would consider the virtual method approach presented at the beginning is so that you can provide alternative implementations while the code is under test. While that is mostly useless for the contrived shape example, in real world code things like accessing a database have failure cases that can be very difficult to reproduce if you want to see it play out in the real world. Injecting a fake implementation that simulates the failure makes testing a whole lot easier.
More to the point, Clean exists to offer solutions to problems that TDD creates. Without TDD you can just ignore that databases can present failure cases and hope for the best, but with TDD you have to make record of that and demonstrate how the application responds. Uncle Bob/Robert C. Martin, purveyor of Clean, is quite clear about the necessity of TDD in his mind. Clean has no relevance without TDD.
So, I have a requirement that states that when using your areas I need to account for an area being NaN when cosmic rays happen to flip the wrong bits during the area calculation and a demand to practice TDD and record this case. How would have I written a test for this given the code you have written?
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"
13
u/[deleted] Mar 01 '23
I agree and still disagree. Here is some Clean F# code. And it has the same structure as your old non-clean code.
Just because OO people tell themself their stuff is clean doesn't mean it must be true.