Timeline for How can I make this tax calculator cleaner?
Current License: CC BY-SA 4.0
6 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 29, 2020 at 14:24 | comment | added | Samwise |
IMO an exception is when you need to manipulate i in some way, since I find an expression involving (X[i] and X[i+1] ) clearer than one involving (x and X[i+1] ), according to the "make alike look alike" principle -- but I understand some have strong feelings about this. :)
|
|
Apr 29, 2020 at 8:24 | comment | added | PiCTo |
Although there's no point in editing this answer, given @MaartenFabré's comment, in general, for i in range(len(X)): x = X[i] is better expressed as for i, x in enumerate(X): .
|
|
Apr 28, 2020 at 10:18 | comment | added | Maarten Fabré |
instead of looping over the index (for i in range(len(BRACKET_TAX_RATES)) ), I would zip the brackets and rates in reverse order, and iterate over those. Doing it in reverse allows you to simplify the edge case. also refactor this part into a separate function: def tax_calculation(gross_income, caps, rates): tax = 0; for cap, rate in zip(reversed(caps), reversed(rates), ): income_in_bracket = gross_income - cap; tax += income_in_bracket * rate; gross_income = min(gross_income, cap); return tax
|
|
Apr 28, 2020 at 0:44 | comment | added | Samwise |
Yep, that's the idea! The assert is to make sure that if you change the number of brackets in one place you remember to do it in other places; you might end up with a subtle bug otherwise.
|
|
Apr 28, 2020 at 0:41 | comment | added | Erik | Thanks Samwise! I see the changes, and please correct me if I'm wrong, but if the tax rates were to change than I would only need to change the BRACKET_TAX_RATES[ ] without having to make changes throughout the app. Thank you for your time and advice! | |
Apr 27, 2020 at 22:55 | history | answered | Samwise | CC BY-SA 4.0 |