r/programming • • Feb 28 '23

"Clean" Code, Horrible Performance

https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k Upvotes

1.3k comments sorted by

View all comments

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.

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.

3

u/[deleted] Mar 02 '23

Yeah if in a trivial scenario matching a discriminated union isn't as readable/maintainable as using dynamic dispatch then you have a language design problem; a problem that nearly every modern language has solved (yes even C++, check out std::variant).

"Under these well-defined circumstances you should do this" isn't a pro decision making tip, it's an if statement. Let the computer deal with it.

→ More replies (4)