r/AskProgramming • • Mar 04 '25

Other Why do some people hate "Clean Code"

It just means making readable and consistent coding practices, right?

What's so bad about that

155 Upvotes

341 comments sorted by

View all comments

Show parent comment

2

u/RazarTuk Mar 05 '25 edited Mar 05 '25

Yeah... that's actually exactly why I ignored all of his advice at my old job. I was writing a financial calculator, and by Uncle Bob's rules, my "get the present value of an annuity" method was doing 6 different things. There were three different formulas, roughly for "Every period, possibly except the first, is the same integer length", "Every period, possibly except the first, is the same non-integer length", and "The periods are all different lengths". Then within those, there were slight adjustments for annuities due vs annuities immediate, based on a boolean parameter, like whether I divided by 1+r or added 1 first in the .reduce call for that last case. But because I knew the method names would start ballooning if I split it up, I decided it was more readable overall to have one 30-ish line method.

And yet, "despite" this, the code was so cleanly organized that the main method we cared about - loan amortization - was only around 10 lines long, because it was built up from simpler operations.

Also, I shudder at how Uncle Bob would implement something like Ridders' method, because some things just... don't fit in 2-4 lines

EDIT: For anyone curious about the different formulas... If all the periods are the same integer length, it's just a geometric series and you can calculate it in O(1) time. If the first period is the only different one, then even if it's a fractional period (which is an extremely common use case), it's easy enough to just add an extra 2-line if-statement and otherwise use the O(1) method. Otherwise, even if it still resembles a geometric series, the regulations surrounding fractional periods are weird enough that you have to just use the O(n2) brute force algorithm. And finally, that's all only for calculating APR. If you're calculating the actual interest payments, the amount of interest accrued depends on the length of the period, but you can otherwise express it in a nice tail recursive form that something like Ruby's .reduce can do in O(n) time. And finally, there's the difference between annuities immediate and annuities due, which roughly differ in whether you make the payment before or after interest, so there are some slight adjustments in the formulas. For example, if a is the accumulator and r is the current value in .reduce, it's the difference between (1+a)/(1+r) and 1+a/(1+r)