3 ms·
This example may be overly simplistic for the point I want to make, but the most common sort of false-factoring I see is when this… def f(x): regular_thi
by gorgoiler 4y ago
This example may be overly simplistic for the point I want to make, but the most common sort of false-factoring I see is when this…
def f(x):
regular_thing(x)
f(v)
…becomes this:
def f(x):
if condition(x):
special_thing(x)
regular_thing(x)
f(v) # in the old code
f(w) # in some new code
Whereas I posit that the correct way to handle the special thing is as follows:
def f(x):
regular_thing(x)
f(v)
if condition(w):
special_thing(w)
f(w)
When junior programmers insist on handling conditional execution paths further and further down the stack you end up with a plethora of optional flags being passed down through each function call.
I found one piece of code where a team had given up and were passing the same **kwargs glob down three of four levels of function calls, with each function doing special conditional steps based on what it saw in the kwargs, effectively a global set of flags controlling logic up and down the stack.
It’s a great example of why long functions are a bad code smell.
- toxik 4y agodef do(**kwargs): subdo1(**kwargs) ... What could be more general?
- deleted 4y ago[deleted]
- gpderetta 4y agoVery much yes. Behavioural flags are the worst kind of abstraction and a strong signal that the wrong abstraction was chosen. The reality is that while abstraction is critical for keeping complexity under control, it is also hard. Refactoring common looking code will usually not lead to the correct abstraction. As a recovering DRY addict, it is a lesson that took me a long time to learn. edit: another common bad refactor/abstraction: def fun1(...): do_the_thing(...) def fun2(...): prepare_the_thing(...) fun1(...) def fun3(...): prepare_a_bit_more(...) fun2(...) No other functions call fun1 and fun2. Often fun1 and fun2 are badly named because they are just steps in implementing the behaviour of fun3. Usually I inline back these cases as it greatly simplify understanding the code.
- evntdrvn 4y agoFor people that want to have that style, I like to show them how to convert them into local functions if the language has that feature. At least that way they can't be accidentally misused (as easily... :)